Lane Detection OpenCV Python

Lane Detection opencv python

Self Driving Car is one of AI’s most innovative technologies. Self Driving Cars use lane detection OpenCV features to detect lane 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
Lane Detection opencv python
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 package for lanes Detection.

Installing OpenCV in Python

pip install opencv-python

No Need to Install NumPy now, as it’s already bundled with 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 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 problem.

Lane Detection GitHub

Source Code of Lanes Detection –

More Projects

Face Recognition

Divyanshu Shekhar

Finding the shape of an image in OpenCV Python

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

height = img.shape[0]
width = img.shape[1]

Drawing Line on an image in OpenCV Python

cv.line(blank_image, (x1, y1), (x2, y2), (0, 255, 0), 2)

Blank image using NumPy in Python

blank_image = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)

Copying an image in OpenCV using NumPy Python

copy_img = np.copy(img)

Tags: , , ,

2 Responses

  1. zUsVwh says:

    343567 747796Oh my goodness! a amazing write-up dude. Thanks a good deal Nonetheless We are experiencing trouble with ur rss . Do not know why Not able to sign up to it. Possibly there is anybody finding identical rss dilemma? Anyone who knows kindly respond. Thnkx 359896

Leave a Reply

Your email address will not be published. Required fields are marked *