Skip to content

AdnanSattar/opencv-lane-detection

Repository files navigation

opencv-lane-detection

Lane detection system using OpenCV with camera calibration, perspective transform, polynomial fitting, curvature calculation and accurately detects lane markings in video. This project is ideal for enhancing road safety and autonomous driving applications.

🎬 Demo

Lane Detection Demo

📹 Watch Full Demo Video - Real-time lane detection with curvature calculation and vehicle offset detection

The processed video shows:

  • ✅ Green overlay highlighting the detected lane area
  • ✅ Yellow lines marking the left and right lane boundaries
  • ✅ Text overlay showing curvature radius and vehicle offset

Example Images

Camera Calibration Result: Camera Calibration

Pipeline Visualization: Pipeline

Features

  • Camera Calibration: Automatic camera distortion correction using checkerboard calibration
  • Robust Thresholding: Combined gradient and color space thresholding for lane detection
  • Perspective Transform: Bird's-eye view transformation for better lane line analysis
  • Polynomial Fitting: Second-order polynomial fitting for smooth lane line detection
  • Curvature Calculation: Real-time calculation of lane curvature radius
  • Vehicle Offset: Detection of vehicle position relative to lane center
  • Frame Smoothing: Moving average smoothing across frames for stable detection

Project Structure

.
├── calibrate_camera.py      # Camera calibration script
├── combined_thresh.py       # Combined thresholding functions
├── line_fit.py              # Lane line fitting algorithms
├── line_fit_video.py        # Main video processing script
├── Line.py                  # Line class for frame smoothing
├── perspective_transform.py # Perspective transformation functions
├── gen_example_images.py    # Generate example pipeline images
├── running.py               # Project entry point
├── camera_cal/              # Camera calibration images
├── test_images/             # Test images for validation
├── output_images/           # Output images (calibration results)
└── pyproject.toml           # Project dependencies

Requirements

  • Python 3.8+
  • UV package manager (recommended) or pip
  • OpenCV
  • NumPy
  • Matplotlib
  • MoviePy 2.x

Installation

Using UV (Recommended)

  1. Install UV if you haven't already:

    pip install uv
  2. Clone the repository:

    git clone https://github.com/AdnanSattar/opencv-lane-detection.git
    cd opencv-lane-detection
  3. Install dependencies:

    uv sync

Using pip

pip install numpy opencv-python matplotlib moviepy

Usage

1. Camera Calibration (First Time Only)

Before processing videos, you need to calibrate the camera:

uv run python calibrate_camera.py

This will:

  • Process calibration images from camera_cal/
  • Generate calibrate_camera.p file with camera matrix and distortion coefficients
  • Save an example undistorted image to output_images/

2. Process Video

To process a video file with lane detection:

uv run python line_fit_video.py

This will:

  • Process project_video.mp4
  • Apply lane detection to each frame
  • Output annotated video as out.mp4

Note: Make sure project_video.mp4 exists in the project directory, or modify the input filename in line_fit_video.py.

3. Generate Example Images

To generate example pipeline images from test images:

uv run python gen_example_images.py

This creates example images showing each step of the pipeline in the example_images/ directory.

4. Quick Start

Run the main entry point:

uv run python running.py

Algorithm Pipeline

  1. Camera Calibration: Corrects for camera distortion using checkerboard calibration
  2. Undistortion: Applies calibration to remove distortion from input images
  3. Thresholding:
    • Sobel gradient (x and y directions)
    • Magnitude and direction of gradient
    • HLS color space thresholding (S channel)
    • Combined binary threshold
  4. Perspective Transform: Transforms to bird's-eye view for lane analysis
  5. Lane Detection:
    • Sliding window search (initial detection)
    • Polynomial fitting around previous detection (subsequent frames)
  6. Visualization: Overlays detected lanes, curvature, and vehicle offset on original image

Output

The processed video (out.mp4) includes:

  • Green overlay showing detected lane area
  • Yellow lines marking left and right lane boundaries
  • Radius of curvature (in meters) displayed on each frame
  • Vehicle offset from lane center (in meters) displayed on each frame

Example Output

After running the video processing, you'll get an annotated video (out.mp4) showing:

  • Lane Detection: Green highlighted lane area between detected lane lines
  • Curvature Information: Real-time radius of curvature calculation displayed on each frame
  • Position Information: Vehicle offset from lane center displayed on each frame

Note: The output video out.mp4 is generated after running line_fit_video.py. It contains the original video with lane detection overlays applied to each frame. The video file is not included in the repository due to size, but can be generated by running the processing script.

Technical Details

Camera Calibration

  • Uses 9x6 checkerboard pattern (with some variations)
  • Processes 20 calibration images
  • Generates camera matrix and distortion coefficients

Thresholding Parameters

  • Sobel X gradient: 50-255
  • Magnitude threshold: 50-255
  • Direction threshold: 0.7-1.3 radians
  • HLS S channel: 170-255

Perspective Transform

  • Source points: (200, 720), (1100, 720), (595, 450), (685, 450)
  • Destination points: (300, 720), (980, 720), (300, 0), (980, 0)

Lane Fitting

  • Second-order polynomial: x = A*y² + B*y + C
  • Moving average window: 5 frames
  • Minimum pixels for detection: 10 points

Troubleshooting

Issue: calibrate_camera.p not found

Solution: Run uv run python calibrate_camera.py first to generate the calibration file.

Issue: Empty frames or detection failures

Solution: The code includes error handling for frames where lanes aren't detected. It will use previous frame detections or skip annotation for problematic frames.

Issue: MoviePy import errors

Solution: Make sure you're using MoviePy 2.x. The code uses from moviepy import VideoFileClip and image_transform() method, which are MoviePy 2.x APIs.

Dependencies

  • numpy >= 1.21.0: Numerical operations
  • opencv-python >= 4.5.0: Computer vision operations
  • matplotlib >= 3.4.0: Visualization
  • moviepy >= 1.0.3: Video processing

License

This project is open source and available for educational and research purposes.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments

  • OpenCV community for excellent computer vision tools
  • MoviePy for video processing capabilities

Future Improvements

  • Real-time webcam processing
  • Support for different video resolutions
  • Adaptive thresholding parameters
  • Machine learning-based lane detection
  • Multi-lane detection (3+ lanes)
  • Integration with ROS for autonomous vehicles

About

Lane detection system using OpenCV with camera calibration, perspective transform, polynomial fitting, and curvature calculation. Perfect for autonomous driving applications.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages