Skip to content

Road Lane Detection #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ More information on contributing and the general code of conduct for discussion
| QR Code with logo | [QR code with Logo](https://github.com/DhanushNehru/Python-Scripts/tree/master/QR%20with%20Logo) | QR Code Customization Feature |
| 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! |
| Remove Background | [Remove Background](https://github.com/DhanushNehru/Python-Scripts/tree/master/Remove%20Background) | Removes the background of images. |
| Road-Lane-Detection | [Road-Lane-Detection](https://github.com/NotIncorecc/Python-Scripts/tree/master/Road-Lane-Detection) | Detects the lanes of the road |
| 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. |
| 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. |
| 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. |
Expand Down
18 changes: 18 additions & 0 deletions Road-Lane-Detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Road Lane Detection

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.

## Features

- **Dynamic Mask Calculation**: Adjusts the mask according to the road color.
- **Lane Detection**: Identifies and highlights lanes on the road.

## Requirements

- Python 3.x
- OpenCV

## Acknowledgements

- OpenCV documentation
- Various online tutorials and resources
84 changes: 84 additions & 0 deletions Road-Lane-Detection/dik2_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import cv2
import numpy as np

def canny_edge_detection(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blur, 50, 150)
return edges

def detect_road_region(frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

lower_gray = np.array([0, 0, 50]) # Lower bound for gray
upper_gray = np.array([180, 50, 200]) # Upper bound for gray

mask = cv2.inRange(hsv, lower_gray, upper_gray)

contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

if contours:
largest_contour = max(contours, key=cv2.contourArea)

epsilon = 0.02 * cv2.arcLength(largest_contour, True)
polygon = cv2.approxPolyDP(largest_contour, epsilon, True)

road_mask = np.zeros_like(mask)
if len(polygon) >= 3:
cv2.fillPoly(road_mask, [polygon], 255)

return road_mask
return None

def hough_transform(masked_edges):
lines = cv2.HoughLinesP(masked_edges, 2, np.pi / 180, 100, np.array([]), minLineLength=40, maxLineGap=5)
return lines

def display_lines(frame, lines):
line_image = np.zeros_like(frame)
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
cv2.line(line_image, (x1, y1), (x2, y2), (0, 255, 0), 10)
return line_image

def combine_images(frame, line_image):
combined_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
return combined_image

def process_frame(frame):
edges = canny_edge_detection(frame)

road_mask = detect_road_region(frame)

if road_mask is not None:
masked_edges = cv2.bitwise_and(edges, edges, mask=road_mask)
else:
masked_edges = edges

lines = hough_transform(masked_edges)

line_image = display_lines(frame, lines)

result = combine_images(frame, line_image)

return result

vds = ['lane5.mp4']#add more of your own videos here

cap = cv2.VideoCapture('videos/'+vds[0])
while(cap.isOpened()):
ret, frame = cap.read()
if not ret:
break

result = process_frame(frame)
cv2.imshow('Lane Detection', result)
road_cont_mask = detect_road_region(frame)
cv2.imshow('Lane Detection2', road_cont_mask)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()
1 change: 1 addition & 0 deletions Road-Lane-Detection/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
opencv-python
Binary file added Road-Lane-Detection/videos/lane5.mp4
Binary file not shown.