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.
📹 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
- 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
.
├── 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
- Python 3.8+
- UV package manager (recommended) or pip
- OpenCV
- NumPy
- Matplotlib
- MoviePy 2.x
-
Install UV if you haven't already:
pip install uv
-
Clone the repository:
git clone https://github.com/AdnanSattar/opencv-lane-detection.git cd opencv-lane-detection -
Install dependencies:
uv sync
pip install numpy opencv-python matplotlib moviepyBefore processing videos, you need to calibrate the camera:
uv run python calibrate_camera.pyThis will:
- Process calibration images from
camera_cal/ - Generate
calibrate_camera.pfile with camera matrix and distortion coefficients - Save an example undistorted image to
output_images/
To process a video file with lane detection:
uv run python line_fit_video.pyThis 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.
To generate example pipeline images from test images:
uv run python gen_example_images.pyThis creates example images showing each step of the pipeline in the example_images/ directory.
Run the main entry point:
uv run python running.py- Camera Calibration: Corrects for camera distortion using checkerboard calibration
- Undistortion: Applies calibration to remove distortion from input images
- Thresholding:
- Sobel gradient (x and y directions)
- Magnitude and direction of gradient
- HLS color space thresholding (S channel)
- Combined binary threshold
- Perspective Transform: Transforms to bird's-eye view for lane analysis
- Lane Detection:
- Sliding window search (initial detection)
- Polynomial fitting around previous detection (subsequent frames)
- Visualization: Overlays detected lanes, curvature, and vehicle offset on original image
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
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.
- Uses 9x6 checkerboard pattern (with some variations)
- Processes 20 calibration images
- Generates camera matrix and distortion coefficients
- Sobel X gradient: 50-255
- Magnitude threshold: 50-255
- Direction threshold: 0.7-1.3 radians
- HLS S channel: 170-255
- Source points: (200, 720), (1100, 720), (595, 450), (685, 450)
- Destination points: (300, 720), (980, 720), (300, 0), (980, 0)
- Second-order polynomial:
x = A*y² + B*y + C - Moving average window: 5 frames
- Minimum pixels for detection: 10 points
Solution: Run uv run python calibrate_camera.py first to generate the calibration file.
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.
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.
numpy >= 1.21.0: Numerical operationsopencv-python >= 4.5.0: Computer vision operationsmatplotlib >= 3.4.0: Visualizationmoviepy >= 1.0.3: Video processing
This project is open source and available for educational and research purposes.
Contributions are welcome! Please feel free to submit a Pull Request.
- OpenCV community for excellent computer vision tools
- MoviePy for video processing capabilities
- 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
