Step 1: Import Necessary Libraries
To begin, import the essential libraries needed for lane detection.
```python import cv2 import numpy as np ```
Step 2: Read and Display the Image
Load the road image and display it to get a visual understanding of the data. If you need any assistance with OpenCV assignment, feel free to reach out for expert guidance.
```python image = cv2.imread('road_image.jpg') cv2.imshow('Original Image', image) cv2.waitKey(0) cv2.destroyAllWindows() ```
Step 3: Convert the Image to Grayscale
Convert the loaded image to grayscale to simplify processing.
```python gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow('Grayscale Image', gray_image) cv2.waitKey(0) cv2.destroyAllWindows() ```
Step 4: Apply Gaussian Blur to Reduce Noise
Apply Gaussian Blur to the grayscale image to reduce noise.
```python blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0) cv2.imshow('Blurred Image', blurred_image) cv2.waitKey(0) cv2.destroyAllWindows() ```
Step 5: Apply Canny Edge Detection
Use the Canny edge detection algorithm to detect edges in the image.
```python edges = cv2.Canny(blurred_image, 50, 150) cv2.imshow('Edges', edges) cv2.waitKey(0) cv2.destroyAllWindows() ```
Step 6: Create a Region of Interest (ROI) Mask
Create a mask defining the region of interest (ROI) on the road.
```python height, width = edges.shape roi_vertices = [ (0, height), (width / 2, height / 2), (width, height) ] mask = np.zeros_like(edges) cv2.fillPoly(mask, np.int32([roi_vertices]), 255) cv2.imshow('ROI Mask', mask) cv2.waitKey(0) cv2.destroyAllWindows() ```
Step 7: Apply the ROI Mask to the Edge-Detected Image
Apply the ROI mask to the edge-detected image to isolate the lane regions.
```python masked_edges = cv2.bitwise_and(edges, mask) cv2.imshow('Masked Edges', masked_edges) cv2.waitKey(0) cv2.destroyAllWindows() ```
Step 8: Detect and Draw Hough Lines for Lane Identification
Use the Hough Transform to detect lines representing the road lanes and draw them on the original image.
```python rho = 2 theta = np.pi / 180 threshold = 50 min_line_length = 100 max_line_gap = 5 lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array(), minLineLength=min_line_length, maxLineGap=max_line_gap) line_image = np.copy(image) * 0 for line in lines: for x1, y1, x2, y2 in line: cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 5) lanes_image = cv2.addWeighted(image, 0.8, line_image, 1, 0) cv2.imshow('Lanes Image', lanes_image) cv2.waitKey(0) cv2.destroyAllWindows() ```
With this step-by-step guide, you now have the knowledge and tools to confidently create your own lane detection system using OpenCV in Python. By experimenting with various images and videos, you can further fine-tune your lane detection algorithm and develop robust computer vision applications to tackle real-world challenges on the road. So, dive into the world of computer vision and enjoy the thrill of building intelligent systems that can perceive and navigate the lanes with precision. Happy coding!