Self Driving Car is one of AI’s most innovative technologies. Self Driving Cars use lane detection OpenCV features to detect lanes of the roads and they are trained not to drive outside of the lane.

This lane detection can also be implemented in OpenCV python. 

Let’s Code it.

Lane Detection OpenCV Algorithm

  • Capturing and decoding video file frame by frame
  • Conversion of the Image to GrayScale
  • Applying filters to reduce noise in video frames
  • Edge Detection Using Canny Edge detection method
  • Finding the region of interest and working on that part
  • Detecting lanes using Hough line transform

Source | Divyanshu Shekhar-

Programming Language


Python is going to play a very important role in Lane Detection. We are going to use the OpenCV library made for Computer Vision work. This library makes it easy for us to work with images.

Learn Python First, if you want to work with OpenCV. If you are a Beginner, read why should you learn python?
Source | Divyanshu Shekhar Youtube

Python is also used in many advanced fields like data scientist/analyst, Machine Learning, and Artificial Intelligence. So it’s worth learning. Read this, If you wonder how to make face recognition in python?

Lane Detection OpenCV Python Packages

We are going to install Computer Vision, NumPy, and Matplotlib packages for lanes Detection.

Installing OpenCV in Python

pip install opencv-python

No Need to Install NumPy now, as it’s already bundled with the OpenCV library. If you want to make sure that the installation of NumPy has been done on your computer, execute the command :

pip install numpy

For installing the Matplotlib Data Visualization library.

pip install matplotlib

Importing Packages

import cv2 as cv
import numpy as np  
from matplotlib import pyplot as plt  

OpenCV Video Capture

In OpenCV, video is captured using VideoCapture() Function. It takes one parameter – the source of the video. It can either be from the External Camera (Front or Rear Camera) or any previous recorded Video.

Provide 0 for Front Camera and 1 for the Rear Camera. If you want to use pre-recorded video, Provide the path of the source video.

read() function when applied on captured video either from video source or camera, returns retention value i.e bool value (True or False), and the frame.

def videoLanes():
    cap = cv.VideoCapture('./img/Lane.mp4')
        ret, frame =
        frame = lanesDetection(frame)
        cv.imshow('Lanes Detection', frame)
        if cv.waitKey(1) & 0xFF == ord('q'):


We apply image manipulation on every frame of the video and this is working with video in OpenCV Python.

OpenCV Lane Detection Function

shape() function returns height, width, and the dimension of the metrics of the colors of the image.

Next, Vertices of the region of interest are marked. You will have to calculate the region of interest of your source image i.e Video frame if you are using another video as it will have lanes in a different position and this calculation will not work.

The image then is converted to Grayscale using the cvtColor() function. It also takes two parameters.

  1. image
  2. Transformation i.e (RGB to Gray) or (Gray to RGB)

After converting the colored image to grayscale, the Canny edge detection method is applied to detect the edges in the image.

Then the Grayscale image is cropped in the shape of a triangle using the function region_of_interest(). This function takes two parameters:

  1. Canny Edge Detected Image
  2. The Region of Interest Vertices in form of NumPy array.

def lanesDetection(img):

    # print(img.shape)
    height = img.shape[0]
    width = img.shape[1]

    region_of_interest_vertices = [
        (200, height), (width/2, height/1.37), (width-300, height)
    gray_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
    edge = cv.Canny(gray_img, 50, 100, apertureSize=3)
    cropped_image = region_of_interest(
        edge, np.array([region_of_interest_vertices], np.int32))

    lines = cv.HoughLinesP(cropped_image, rho=2, theta=np.pi/180,
                           threshold=50, lines=np.array([]), minLineLength=10, maxLineGap=30)
    image_with_lines = draw_lines(img, lines)
    # plt.imshow(image_with_lines)
    return image_with_lines

When the cropped image is received, lines are detected on that image using HoughLinesP() function.

Lines Detected image is then passed to draw_lines() function that takes two parameters:

  1. The Original Image i.e the Video Frame
  2. Lines Detected Image

draw_lines() function draws lines on the image.

OpenCV Region of Interest (ROI)

This Function defines the region of interest in the image. It takes two parameters the video frame and the vertices of the region of interest.

def region_of_interest(img, vertices):
    mask = np.zeros_like(img)
    # channel_count = img.shape[2]
    match_mask_color = (255)
    cv.fillPoly(mask, vertices, match_mask_color)
    masked_image = cv.bitwise_and(img, mask)
    return masked_image

OpenCV Draw Lines for lane detection

This Function is used to draw lines on a blank image and merge it with the original image’s copy.

def draw_lines(img, lines):
    img = np.copy(img)
    blank_image = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)

    for line in lines:
        for x1, y1, x2, y2 in line:
            cv.line(blank_image, (x1, y1), (x2, y2), (0, 255, 0), 2)

    img = cv.addWeighted(img, 0.8, blank_image, 1, 0.0)
    return img

Lane Detection OpenCV Project

Try Using your own recorded video, find out a region of interest in the video frame, and implement the logic to find out lanes. Play with the values in the parameters and also look at the documentation if you face any problems.

Lane Detection GitHub

Source Code of Lanes Detection –

More Projects

Face Recognition

About Author
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Scroll to Top