Skip to content

Commit 164d09c

Browse files
authored
Merge pull request DhanushNehru#367 from NotIncorecc/master
Road Lane Detection
2 parents b58dff6 + 3e2c824 commit 164d09c

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ More information on contributing and the general code of conduct for discussion
118118
| QR Code with logo | [QR code with Logo](https://github.com/DhanushNehru/Python-Scripts/tree/master/QR%20with%20Logo) | QR Code Customization Feature |
119119
| Random Color Generator | [Random Color Generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Random%20Color%20Generator) | A random color generator that will show you the color and values! |
120120
| Remove Background | [Remove Background](https://github.com/DhanushNehru/Python-Scripts/tree/master/Remove%20Background) | Removes the background of images. |
121+
| Road-Lane-Detection | [Road-Lane-Detection](https://github.com/NotIncorecc/Python-Scripts/tree/master/Road-Lane-Detection) | Detects the lanes of the road |
121122
| Rock Paper Scissor 1 | [Rock Paper Scissor 1](https://github.com/DhanushNehru/Python-Scripts/tree/master/Rock%20Paper%20Scissor%201) | A game of Rock Paper Scissors. |
122123
| Rock Paper Scissor 2 | [Rock Paper Scissor 2](https://github.com/DhanushNehru/Python-Scripts/tree/master/Rock%20Paper%20Scissor%202) | A new version game of Rock Paper Scissors. |
123124
| Run Then Notify | [Run Then Notify](https://github.com/DhanushNehru/Python-Scripts/tree/master/Run%20Then%20Notify) | Runs a slow command and emails you when it completes execution. |

Road-Lane-Detection/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Road Lane Detection
2+
3+
This project is a Python program that uses OpenCV to detect lanes on roads. Unlike other lane detection programs, this one dynamically calculates the mask based on the color of the road, making it more adaptable to different environments.
4+
5+
## Features
6+
7+
- **Dynamic Mask Calculation**: Adjusts the mask according to the road color.
8+
- **Lane Detection**: Identifies and highlights lanes on the road.
9+
10+
## Requirements
11+
12+
- Python 3.x
13+
- OpenCV
14+
15+
## Acknowledgements
16+
17+
- OpenCV documentation
18+
- Various online tutorials and resources

Road-Lane-Detection/dik2_1.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import cv2
2+
import numpy as np
3+
4+
def canny_edge_detection(frame):
5+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
6+
blur = cv2.GaussianBlur(gray, (5, 5), 0)
7+
edges = cv2.Canny(blur, 50, 150)
8+
return edges
9+
10+
def detect_road_region(frame):
11+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
12+
13+
lower_gray = np.array([0, 0, 50]) # Lower bound for gray
14+
upper_gray = np.array([180, 50, 200]) # Upper bound for gray
15+
16+
mask = cv2.inRange(hsv, lower_gray, upper_gray)
17+
18+
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
19+
20+
if contours:
21+
largest_contour = max(contours, key=cv2.contourArea)
22+
23+
epsilon = 0.02 * cv2.arcLength(largest_contour, True)
24+
polygon = cv2.approxPolyDP(largest_contour, epsilon, True)
25+
26+
road_mask = np.zeros_like(mask)
27+
if len(polygon) >= 3:
28+
cv2.fillPoly(road_mask, [polygon], 255)
29+
30+
return road_mask
31+
return None
32+
33+
def hough_transform(masked_edges):
34+
lines = cv2.HoughLinesP(masked_edges, 2, np.pi / 180, 100, np.array([]), minLineLength=40, maxLineGap=5)
35+
return lines
36+
37+
def display_lines(frame, lines):
38+
line_image = np.zeros_like(frame)
39+
if lines is not None:
40+
for line in lines:
41+
x1, y1, x2, y2 = line.reshape(4)
42+
cv2.line(line_image, (x1, y1), (x2, y2), (0, 255, 0), 10)
43+
return line_image
44+
45+
def combine_images(frame, line_image):
46+
combined_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
47+
return combined_image
48+
49+
def process_frame(frame):
50+
edges = canny_edge_detection(frame)
51+
52+
road_mask = detect_road_region(frame)
53+
54+
if road_mask is not None:
55+
masked_edges = cv2.bitwise_and(edges, edges, mask=road_mask)
56+
else:
57+
masked_edges = edges
58+
59+
lines = hough_transform(masked_edges)
60+
61+
line_image = display_lines(frame, lines)
62+
63+
result = combine_images(frame, line_image)
64+
65+
return result
66+
67+
vds = ['lane5.mp4']#add more of your own videos here
68+
69+
cap = cv2.VideoCapture('videos/'+vds[0])
70+
while(cap.isOpened()):
71+
ret, frame = cap.read()
72+
if not ret:
73+
break
74+
75+
result = process_frame(frame)
76+
cv2.imshow('Lane Detection', result)
77+
road_cont_mask = detect_road_region(frame)
78+
cv2.imshow('Lane Detection2', road_cont_mask)
79+
80+
if cv2.waitKey(1) & 0xFF == ord('q'):
81+
break
82+
83+
cap.release()
84+
cv2.destroyAllWindows()

Road-Lane-Detection/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
opencv-python

Road-Lane-Detection/videos/lane5.mp4

766 KB
Binary file not shown.

0 commit comments

Comments
 (0)