Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 54 ยท Projects

Project: Lane Line Detection

Build a lane line detector: grayscale, blur, Canny edges, a region mask, Hough lines and slope averaging. Includes a live NumPy demo on a synthetic road.

Lane detection is the "hello world" of self-driving perception. The classic version uses no machine learning at all, just a chain of classic OpenCV operations: turn the frame gray, blur it, find edges, keep only the part of the image where the road can be, find straight lines and merge the many short segments into one left and one right lane line.

This lesson walks through that chain. The real cv2 code is shown in read-only blocks because OpenCV cannot run on this site. Everything else is live: a synthetic road drawn with NumPy, our own gradient-based edge detector (a simplified stand-in for Canny), our own Hough accumulator (the idea behind HoughLines), and the slope-averaging step in plain Python, which is exactly the same code you would use with real segments.

From frame to lane lines

Gray and blur Less data, less noise.
Edges Canny finds sharp changes.
Region mask Keep only the road triangle.
Hough lines Vote for straight lines.
1 / 2

The real pipeline (read-only)

Python
import cv2
import numpy as np

def region_of_interest(edges):
    h, w = edges.shape
    triangle = np.array([[(int(0.05 * w), h), (int(0.5 * w), int(0.6 * h)), (int(0.95 * w), h)]],
                        dtype=np.int32)
    mask = np.zeros_like(edges)
    cv2.fillPoly(mask, triangle, 255)
    return cv2.bitwise_and(edges, mask)

def detect_lanes(frame):
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5, 5), 0)
    edges = cv2.Canny(blur, 50, 150)
    roi = region_of_interest(edges)

    segments = cv2.HoughLinesP(roi, rho=1, theta=np.pi / 180, threshold=30,
                               minLineLength=20, maxLineGap=10)
    return average_lines(segments, frame.shape[0])   # defined below

def draw_lanes(frame, lanes):
    out = frame.copy()
    for x1, y1, x2, y2 in lanes:
        cv2.line(out, (x1, y1), (x2, y2), (0, 255, 0), 6)
    return out

Each piece has its own lesson: Blurring and Noise Reduction, Canny Edge Detection, Arithmetic, Bitwise Ops and Masking for the mask and Detecting Lines, Circles and Shapes for the Hough transform. The part that is new in this project is turning the segments into two clean lines.

Real vs stand-in: GaussianBlur, Canny, fillPoly and HoughLinesP are real OpenCV and read-only here. In the live demos a 3x3 blur, a Sobel gradient threshold (Canny without non-maximum suppression and hysteresis), a half-plane polygon test and a standard Hough accumulator play their roles. average_lines is plain Python and runs live exactly as written.

Step 1: a synthetic road, then edges

Our road is 60 rows by 100 columns: a bright sky above the horizon, dark asphalt below, and two white lane stripes running toward a vanishing point near the horizon. We add a little seeded noise so the edges are not perfect.

To find edges we blur, then compute the Sobel gradient (see Gradients, Sobel and Laplacian) and keep pixels where the gradient magnitude is large. This lacks Canny's thinning and hysteresis, but for strong stripes it gives the same kind of output: a thin outline of each stripe. Then the region mask keeps only a trapezoid in front of the car. This matters: the horizon is also a strong edge, and so would be any building, sign or shadow beside the road.

Python Road, edges and region mask

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The printed picture shows two bands converging upward, each made of the two edges of one stripe, and nothing for the horizon (499 of the 636 edge pixels survive the mask). Without the mask, the horizon row would add a long horizontal line that Hough would happily detect.

Step 2: the Hough transform

A Hough transform finds lines by voting. A line can be described by two numbers: rho, its distance from the origin, and theta, the angle of its normal, through the equation x*cos(theta) + y*sin(theta) = rho. Every edge pixel lies on many possible lines, one for each theta. So each pixel votes for all (rho, theta) pairs it could belong to, and cells in this accumulator that collect many votes correspond to lines that many edge pixels agree on.

cv2.HoughLines returns the best (rho, theta) pairs. cv2.HoughLinesP (the probabilistic version) returns actual segments (x1, y1, x2, y2), which is more practical for roads because the lanes are often dashed.

Below we run our own accumulator over the edge pixels, pick the strongest peak, suppress its neighbours so the second edge of the same stripe does not win, and pick a second peak. Then we turn each (rho, theta) back into a slope and intercept.

Python A Hough accumulator finds the two lanes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two peaks come out with one negative slope (the left lane) and one positive slope (the right lane): here exactly -1.0 and 1.0, because the accumulator only knows whole degrees (the true slopes are about -1.13 and 1.03). The star overlay runs on or right beside the lane paint. The exact values depend on the noise seed and the 1-degree theta step; a finer step or HoughLinesP would hug the stripes more closely. In a real pipeline the Hough thresholds (threshold, minLineLength, maxLineGap) are the knobs you tune per camera.

Standard Hough on thick painted stripes finds the two edges of each stripe as separate nearly-parallel lines, which is why we suppressed a neighbourhood around the first peak. Real pipelines instead get many short segments from HoughLinesP and merge them, which is the next step.

Step 3: slope averaging

HoughLinesP might return ten or twenty segments: several on each lane, some nearly horizontal junk. We reduce them to one line per side using the slope sign:

  1. Compute each segment's slope (y2 - y1) / (x2 - x1) and intercept b = y1 - slope * x1.
  2. Discard near-horizontal segments, they are not lane lines.
  3. Negative slope goes to the left group, positive slope to the right group (because image y grows downward).
  4. Average slope and intercept in each group, weighted by segment length so long segments count more.
  5. Turn each averaged (slope, intercept) back into end points at two chosen rows: the bottom of the image and a row a bit above the horizon region.

The function below is plain Python, so it works as-is with the output of real cv2.HoughLinesP (reshape the array to a list of (x1, y1, x2, y2)). We test it on hand-made segments that include noise and one horizontal outlier.

Python Merge segments into a left and a right lane

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You now have two segments, (x1, y1, x2, y2) each, ready for cv2.line. The horizontal outlier was ignored automatically. If a side has no segments at all (a faded lane, a gap), the dictionary simply lacks that side; real code keeps the previous frame's value for a few frames so the overlay does not flicker.

Step 4: video, robustness and the limits

Run this on every frame of a video (see Processing Video Frame by Frame), and smooth over time: average the last 5 to 10 frames' slopes and intercepts. Lane lines in neighbouring frames barely move, and smoothing removes jitter.

Classic lane detection works well on clear highways and fails in many situations:

  • Curves. Straight-line Hough cannot follow a curved lane; fit a polynomial to edge points after a bird's-eye perspective warp (Perspective Transforms).
  • Shadows and glare. They create strong edges that survive Canny. Mask by color first (white and yellow in HSV, see Project: Color Object Detector).
  • Fixed region. A hard-coded trapezoid assumes a fixed camera. Tilt the camera and the region is wrong.
  • Worn markings, rain, night. Gradients become weak and noisy. Modern systems use learned models for exactly this reason.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In image coordinates (y grows downward), a lane on the left side of the road typically has which slope?

    Moving right along the left lane you go up the image, and since y decreases upward, dy/dx is negative. The right lane has a positive slope.
  2. 2. Why do we apply a region-of-interest mask before the Hough transform?

    Edges outside the road area vote for lines too. Masking removes those votes before they can win.
  3. 3. What is the main difference between HoughLines and HoughLinesP?

    The standard version gives infinite lines described by rho and theta. The probabilistic version returns segments, which suits dashed lane markings.
  4. 4. Why weight the slope average by segment length?

    A long segment is supported by many edge pixels, while a short one may be noise, so it should have less influence.

Summary

  • The classic pipeline is grayscale, blur, Canny, region mask, HoughLinesP, slope averaging and drawing.
  • The region mask removes strong edges that are not lanes, such as the horizon.
  • Hough turns edge pixels into votes for (rho, theta) lines; peaks in the accumulator are the lines.
  • Split segments by slope sign (negative is left, positive is right in image coordinates), drop near-horizontal ones, and average slope and intercept weighted by length.
  • Smooth over several frames; handle missing lanes by reusing recent values.
  • The live demos used NumPy stand-ins for Canny and Hough; average_lines is plain Python and works with real segments.

Next project: Project: Motion Detector.