0% found this document useful (0 votes)
7 views2 pages

C

The document contains a Python script that implements lane detection in video frames using OpenCV. It defines functions to create a region of interest, detect edges, and identify lane lines using Hough Transform. The script processes a video file, applies lane detection, and saves the output to a new video file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

C

The document contains a Python script that implements lane detection in video frames using OpenCV. It defines functions to create a region of interest, detect edges, and identify lane lines using Hough Transform. The script processes a video file, applies lane detection, and saves the output to a new video file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import cv2

import numpy as np

def region_of_interest(image):
height = image.shape[0]
width = image.shape[1]
top_y = int(height * 0.7) # Adjust this value to change the depth of ROI (30%
from bottom)
polygon = np.array([[
(0, height),
(width // 2, top_y),
(width, height)
]], np.int32)

mask = np.zeros_like(image)
cv2.fillPoly(mask, polygon, (255, 255, 255))
masked_image = cv2.bitwise_and(image, mask)
return masked_image

def detect_lanes(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blurred, 50, 150)
roi_edges = region_of_interest(edges)

lines = cv2.HoughLinesP(roi_edges, 1, np.pi / 180, 50, minLineLength=50,


maxLineGap=100)

height, width = image.shape[:2]


top_y = int(height * 0.7) # Must match ROI function

left_lines = []
right_lines = []

if lines is not None:


for line in lines:
x1, y1, x2, y2 = line[0]
dx = x2 - x1
if dx == 0:
continue # Skip vertical lines
dy = y2 - y1
slope = dy / dx
if abs(slope) < 0.5: # Filter near-horizontal lines
continue
if slope < 0:
left_lines.append(line[0])
else:
right_lines.append(line[0])

def process_lines(lines):
if not lines:
return None
points = []
for x1, y1, x2, y2 in lines:
points.append([x1, y1])
points.append([x2, y2])
points = np.array(points)
if len(points) < 2:
return None
m, b = np.polyfit(points[:,1], points[:,0], 1)
x_bottom = m * height + b
x_top = m * top_y + b
x_bottom = int(np.clip(x_bottom, 0, width))
x_top = int(np.clip(x_top, 0, width))
return [(x_bottom, height), (x_top, top_y)]

left_line = process_lines(left_lines)
right_line = process_lines(right_lines)

if left_line and right_line:


pts = np.array([left_line[0], left_line[1], right_line[1], right_line[0]],
np.int32)
cv2.fillPoly(image, [pts], (0, 255, 0))

return image

# Video processing remains the same


cap = cv2.VideoCapture('E.mp4') # Replace with your video path
fps = cap.get(cv2.CAP_PROP_FPS)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

output_video = cv2.VideoWriter('output_video.mp4', cv2.VideoWriter_fourcc(*'mp4v'),


fps, (frame_width, frame_height))

while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
lane_frame = detect_lanes(frame)
output_video.write(lane_frame)
cv2.imshow('Lane Detection', lane_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
output_video.release()
cv2.destroyAllWindows()

You might also like