Skip to content
All projects

Project · 02

Lane Detection Automation

Computer Vision / Robotics

A robot that spots lane lines and figures out which way the road turns.

Year
2024
Stack
Python · OpenCV · NumPy · Tkinter · HTTP requests
Dashcam view of a highway at sunset with the detected lane lines drawn in cyan and the estimated lane center in red

Overview

This one went through two versions. The first runs on recorded road video — it picks out the lane lines, finds the middle of the lane, guesses if the road is turning left, right, or going straight, and draws all of that back on the video.

The second version does it on a live camera feed from the robot and adds a control panel (Tkinter) that sends movement commands to the robot over HTTP.

  • Recorded and live video processing
  • Yellow lane color filtering
  • Bird's-eye perspective transform
  • Grayscale + Canny edge detection
  • Probabilistic Hough line detection
  • Slope- and position-based lane filtering
  • Lane-center estimation
  • Turn prediction (left / right / straight)
  • Lane overlay projected back onto the frame
  • Tkinter control panel with live video
  • Movement commands sent to the robot over HTTP
  • Account login and per-user command log

Focus

  • Classical computer vision
  • Perspective transforms
  • Edge detection
  • Hough line detection
  • Network-based control

Architecture

The images below are the real output of each step on one frame of the night drive.

The original version had the perspective window hardcoded for one camera and judged every frame on its own, so on new footage the lines would glitch. Now it calibrates the window itself every few frames, only looks for each lane near where it was last frame, and takes a median over the last few detections so a bad frame or two can't make the lines jump. If a lane drops out or wanders off, it's rebuilt from the other one using the lane width.

The demo footage is two dashcam clips (a sunset highway and a night drive), not my original road video. The live robot version runs a simpler take on the camera feed: grayscale, blur, Canny, Hough, and a slope filter.

Source

  • Video / Camera Feed
  1. 01Image Preprocessing
  2. 02Perspective Transformation
  3. 03Edge Detection
  4. 04Hough Line Detection
  5. 05Lane Filtering
  6. 06Lane Geometry
  7. 07Steering / Turn Decision
  8. 08Robot Control Interface
  1. Dashcam frame inside a dim road tunnel with two lane lines and a row of ceiling lights
    01 · Input frameOne raw frame from the night drive, inside a tunnel.
  2. Black mask with the two lane lines showing as thin white streaks
    02 · Yellow color maskPixels in the yellow range get brightened. The tunnel lights tint the lane lines warm, so they pass.
  3. The same tunnel frame with a cyan trapezoid over the lane marking the region that gets warped
    03 · Perspective windowAuto-calibrated from the vanishing point and my own lane's two lines.
  1. Top-down view of the road with two near-vertical lane lines
    04 · Bird's-eye warpThat window warped to a top-down view, so the lanes run straight up.
  2. Thin white edge outlines on black, mostly along the lane lines
    05 · Canny edgesGrayscale, then Canny keeps only the sharp edges.
  3. Top-down road with blue segments on the left lane and on a seam in the middle, and red segments on the right lane
    06 · Hough linesNear-vertical segments, sorted into left (blue) and right (red). The middle blue one is a seam in the road, too far from the lane to be kept.
  4. Top-down road with two cyan lane lines and a red center line between them
    07 · Tracked lanesSmoothed lane lines and the lane center, which drives the turn call.
  1. Cyan lane lines and a red center line in perspective on a black background
    08 · Warped backThe overlay run through the inverse warp, back into the camera's view.
  2. The tunnel frame with cyan lane lines and a red center line drawn on the road
    09 · Final overlayAdded onto the original frame.

The robot

The live version ran on a small tank robot built from a XiaoR Geek TH tank chassis kit (opens in a new tab), with a Raspberry Pi 1 Model B+ (opens in a new tab), a battery pack, and a cheap webcam on top.

The Pi didn't do any of the vision work. It ran a small live API that streamed the webcam feed to my computer, the computer ran the lane detection and worked out the movement commands, and those got sent back to the robot to drive it.

The prototype had the webcam taped straight onto the chassis. For the final robot at the end of the class we 3D printed a stand for it. We were working with really cheap equipment, so the stand cut down the vibration and raised the camera up for a clearer view of the lines.

  • Prototype tank robot on a desk: gold tracked chassis with a Raspberry Pi and a Logitech webcam taped on top
    Prototype
  • Final tank robot from above, with the Raspberry Pi on the chassis and the webcam mounted on a tall black 3D-printed stand
    Final robot
  • Side view of the final tank robot on a classroom floor, with the webcam on top of the 3D-printed stand
    Final robot, side view

What I learned

This is where I learned how to work with images in code. A frame is really just a grid of numbers, so you can filter it, mask it, and warp it however you want, and I got into the core image processing tools for that: color thresholds, edge detection, and Hough transforms.

A lot of it ended up being linear algebra too. The bird's-eye view is a perspective transform matrix, and finding the lanes came down to fitting lines, solving for where they cross, and approximating where the lane really is from a bunch of noisy segments.

Stack

  • Python
  • OpenCV
  • NumPy
  • Tkinter
  • HTTP requests

Contact

Get in touch.

Want to talk about something I've built, or something you're building? Email's the fastest way to reach me.