Zbotic Logo Zbotic Logo
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

  • Shop
  • About Us
  • Contact Us
  • Reseller
  • Blogs
020 69134444
1800 209 0998
[email protected]
Help Desk
Facebook Twitter Instagram Linkedin YouTube
Zbotic Logo Zbotic Logo
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

All departments
  • 3D Print Service
  • 3D Printer
  • Batteries & Chargers
  • Development Boards
  • Drone Parts
  • EBike parts
  • Sensor Modules
  • Electronic Components
  • Electronic Modules
  • IoT and Wireless
  • Mechanical Parts and Workbench Tools
  • Motors & Drivers & Pumps & Actuators
  • DIY and Robot Kits
  • Show more
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
Return to previous page
Home Camera & Vision Modules

Lane Detection for Self-Driving Car with OpenCV and Pi

Lane Detection for Self-Driving Car with OpenCV and Pi

March 11, 2026 /Posted byJayesh Jain / 0

Lane detection is the foundational computer vision task behind every self-driving car and autonomous robot. Using OpenCV and a Raspberry Pi camera, you can implement a working lane detection self-driving OpenCV Pi system that identifies road lanes in real time and calculates steering commands. This tutorial covers edge detection, Hough transforms, region-of-interest masking, and closed-loop steering control for India robotics projects.

Table of Contents

  • How Lane Detection Works
  • Hardware Requirements
  • Image Preprocessing Pipeline
  • Hough Line Transform
  • Calculating Steering Angle
  • Building a Test Track in India
  • FAQ

How Lane Detection Works

The classical pipeline: Grayscale conversion, Gaussian blur, Canny edge detection, Region-of-Interest mask, Hough line transform, lane averaging, and steering angle calculation. This runs at 30+ FPS on Raspberry Pi 4 for straight tracks and is ideal for India robotics lab projects.

Hardware Requirements

Arducam OV2640 Wide-Angle Camera Module

160-degree FOV wide-angle camera perfect for lane detection robots. Captures wide road view with minimal distortion. Compatible with Raspberry Pi CSI port.

View Product

Waveshare IMX219-160 Wide Angle Camera

IMX219 sensor with 160-degree FOV, 8MP resolution, ideal for Raspberry Pi 4/5 lane following robots. Excellent low-light performance for indoor tracks.

View Product

Image Preprocessing Pipeline

import cv2, numpy as np
from picamera2 import Picamera2

def preprocess(frame):
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5, 5), 0)
    edges = cv2.Canny(blur, 50, 150)
    h, w = edges.shape
    mask = np.zeros_like(edges)
    poly = np.array([[(0,h),(w,h),(int(w*0.6),int(h*0.4)),(int(w*0.4),int(h*0.4))]], dtype=np.int32)
    cv2.fillPoly(mask, poly, 255)
    return cv2.bitwise_and(edges, mask)

Hough Line Transform for Lane Detection

def detect_lanes(frame):
    edges = preprocess(frame)
    lines = cv2.HoughLinesP(edges, 1, 3.14159/180, 50, minLineLength=40, maxLineGap=100)
    left, right = [], []
    if lines is None: return None, None
    for line in lines:
        x1,y1,x2,y2 = line[0]
        if x2==x1: continue
        s = (y2-y1)/(x2-x1)
        b = y1 - s*x1
        if s  0.5: right.append((s,b))
    h = frame.shape[0]
    def coords(s,b):
        y1=h; y2=int(h*0.4)
        return [int((y1-b)/s),y1,int((y2-b)/s),y2]
    L = coords(*np.mean(left,axis=0)) if left else None
    R = coords(*np.mean(right,axis=0)) if right else None
    return L, R

Calculating Steering Angle

def steer(frame, L, R):
    w = frame.shape[1]
    cx = w // 2
    if L is None and R is None: return 0
    if L and R: centre = (L[0]+R[0])//2
    elif L: centre = L[0]+320
    else: centre = R[0]-320
    return int((centre-cx)/cx*45)

# Main loop
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(main={'size':(640,480),'format':'BGR888'}))
picam2.start()
while True:
    frame = picam2.capture_array()
    L, R = detect_lanes(frame)
    angle = steer(frame, L, R)
    print(f'Steering: {angle} degrees', end='r')
    # send angle to motor driver here
    if cv2.waitKey(1) & 0xFF == ord('q'): break

Pi Camera CSI FPC Ribbon Cable 15cm

Replacement FPC ribbon cable for Raspberry Pi Camera. 15cm length suitable for robot chassis mounting. Essential spare for your lane-following build.

View Product

Building a Test Track in India

Use white electrical tape on dark floor – available at any hardware store in India for Rs 30-50 per roll. Track width: 30-40cm between inner lane edges. Avoid direct sunlight which creates strong shadows. If tile grout lines cause false detections, increase minLineLength=80 in HoughLinesP. Total material cost for a 2m x 2m test track: under Rs 200.

FAQ

What camera resolution should I use for lane detection?

640×480 at 30 FPS is the sweet spot on Raspberry Pi 4. Higher resolutions reduce frame rate significantly.

Why does my steering angle jump erratically?

Apply temporal smoothing with a rolling average of the last 5-10 steering angles. Also increase HoughLinesP threshold to filter weak detections.

Can this work outdoors on Indian roads?

Indian road markings are often faded or missing. For outdoor use, apply adaptive thresholding before Canny and use HSV colour filtering for yellow/white lanes.

How do I integrate with motor control?

Send the steering angle via serial to an Arduino motor controller, or directly drive PWM on Pi GPIO pins using RPi.GPIO or pigpio libraries.

Shop Camera & Vision Modules

Tags: autonomous vehicle India, Hough transform lanes, lane detection OpenCV, OpenCV lane detection, self driving car Pi
Share Post
  • Facebook
  • Linkedin
  • Whatsapp
Gerber File Explained: Layer F...
blog gerber file explained layer files for pcb fabrication 599311
blog build an audio spectrum analyzer with arduino and leds 599325
Build an Audio Spectrum Analyz...

Related posts

Svg%3E
Read more

Endoscope Camera Module: PCB Inspection and Industrial Use

April 1, 2026 0
An endoscope camera module is an invaluable tool for PCB inspection, industrial equipment maintenance, and quality control tasks where direct... Continue reading
Svg%3E
Read more

Number Plate Recognition System: ESP32-CAM ANPR Project India

April 1, 2026 0
Building a number plate recognition system with ESP32-CAM is an affordable approach to automatic number plate recognition (ANPR) for Indian... Continue reading
Svg%3E
Read more

Machine Vision with OpenCV: Raspberry Pi Object Detection Guide

April 1, 2026 0
Running OpenCV on a Raspberry Pi for object detection opens up countless applications, from industrial quality inspection to smart doorbell... Continue reading
Svg%3E
Read more

Arducam vs Raspberry Pi Camera: Which Camera Module to Choose

April 1, 2026 0
Choosing between Arducam and Raspberry Pi camera modules is one of the first decisions for any vision project. Both connect... Continue reading
Svg%3E
Read more

360-Degree Camera Stitching Project with OpenCV and Pi

March 11, 2026 0
Creating a 360-degree camera using OpenCV image stitching with Raspberry Pi is an ambitious computer vision project that combines multiple... Continue reading

Add comment Cancel reply

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

Facebook Twitter Instagram Pinterest Linkedin Youtube

Get the latest deals and more.

Download on Google Play Download on the App Store

Call us: 020 69134444 / 1800 209 0998

Monday - Saturday 09:30 AM - 06:00 PM
For Technical Supports Email: [email protected]
For Sales / Enquiries Email: [email protected]

  • My Account

    • Cart

    • Wishlist

    • Checkout

    • My Orders

    • Track Order

    • My Account

  • Information

    • FAQs

    • Blogs

    • Career

    • About Us

    • Contact Us

    • Payment Options

  • Policies

    • Privacy Policy

    • Terms & Conditions

    • GST Input Tax Credit

    • Shipping Return Policy

    • E-Waste Collection Points

    • Our Sitemap

© Zbotic.in is registered trademark of Moxie Supply Pvt Ltd – All Rights Reserved
Login
Use Phone Number
Use Email Address
Not a member yet? Register Now
Reset Password
Use Phone Number
Use Email Address
Register
Already a member? Login Now