Lines detection with Hough Transform – OpenCV 3.4 with python 3 Tutorial 21

We’re going to learn in this tutorial how to detect the lines of the road in a live video using Opencv with Python.

Before going into the lines road detection, we need to understand using opencv what is a line and what isn’t a line.

Hough lines transform:

The Houg lines transform is an algorythm used to detect straight lines. One of the most important features of this method is that can detect lines even when some part of it is missing. And this comes really useful in the road when we have dashed lines, or when for some reason some part of the line is not visible.

Let’s see an example on a simple geometric figure.

We import the opencv and numpy libraries and we load the image.

import cv2
import numpy as np

img = cv2.imread("lines.png")
1.0 – Lines figure

We then simplyfi the images, loading only the grayscale format and detecting the edges.

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 75, 150)
1.1 Edges

We have above a white image with black lines.
On the edges we apply the lines detection using hough transform:

lines = cv2.HoughLinesP(edges, 1, np.pi/180, 50)

What if this image was a square and simply we can’t see the some part of the lines?
Same case if we are driving on the road and there are dashed lines, or some line is cover by tree leaves, some dirt and so on, how can we still detect the line?

On the houghtransform method have a variable called: maxLineGap. We can change its value to fit our needs. In this case let’s say that the gap between the lines can be up to 250 pixels, so our code will look like this:

lines = cv2.HoughLinesP(edges, 1, np.pi/180, 30, maxLineGap=250)

Detect lines of the road:

To detect the lines of the road the process is really similar to the previous one to detect the line of the rectangle.

Let’s start importing the library and loading our video:

import cv2
import numpy as np

video = cv2.VideoCapture("road_car_view.mp4")

while True:
    ret, orig_frame =
    if not ret:
        video = cv2.VideoCapture("road_car_view.mp4")

    frame = cv2.GaussianBlur(orig_frame, (5, 5), 0)

That is on a frame how it looks the road:

2.1 Frame

The core part of the detection is to correctly extract the lines of the road from all the rest of the images. We can do this applying the hsv color detection. In this way we can detect object by their colors, as the lines of a road can be only yellow or white, we extract the part of the images that contains these two colors only.

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    low_yellow = np.array([18, 94, 140])
    up_yellow = np.array([48, 255, 255])
    mask = cv2.inRange(hsv, low_yellow, up_yellow)
2.2 Mask

The white part corresponds to the yellow colors in the original frame, so we can see the lines and also some part of the yellow buildings.

Once we have the mask we find the edges, we use the hough transform method and the detection is done.

    edges = cv2.Canny(mask, 75, 150)

    lines = cv2.HoughLinesP(edges, 1, np.pi/180, 50, maxLineGap=50)
    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line[0]
            cv2.line(frame, (x1, y1), (x2, y2), (0, 255, 0), 5)

We finally show everything on the screen:

    cv2.imshow("frame", frame)
    cv2.imshow("edges", edges)

    key = cv2.waitKey(1)
    if key == 27:
2.3 Frame lines detected

Learn to build Computer Vision Software easily and efficiently.

This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.

Sign UP for FREE