车道检测python代码
时间: 2023-04-02 08:05:00 浏览: 66
以下是车道检测的 Python 代码示例:
```python
import cv2
import numpy as np
def region_of_interest(img, vertices):
mask = np.zeros_like(img)
match_mask_color = (255,)
cv2.fillPoly(mask, vertices, match_mask_color)
masked_image = cv2.bitwise_and(img, mask)
return masked_image
def draw_lines(img, lines):
img = np.copy(img)
blank_image = np.zeros((img.shape[], img.shape[1], 3), dtype=np.uint8)
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(blank_image, (x1, y1), (x2, y2), (, 255, ), thickness=3)
img = cv2.addWeighted(img, .8, blank_image, 1, .)
return img
def pipeline(image):
height = image.shape[]
width = image.shape[1]
region_of_interest_vertices = [
(, height),
(width / 2, height / 2),
(width, height),
]
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
cannyed_image = cv2.Canny(gray_image, 100, 200)
cropped_image = region_of_interest(
cannyed_image,
np.array([region_of_interest_vertices], np.int32),
)
lines = cv2.HoughLinesP(
cropped_image,
rho=6,
theta=np.pi / 60,
threshold=160,
lines=np.array([]),
minLineLength=40,
maxLineGap=25
)
image_with_lines = draw_lines(image, lines)
return image_with_lines
cap = cv2.VideoCapture('test.mp4')
while(cap.isOpened()):
ret, frame = cap.read()
frame = pipeline(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
```
希望能对您有所帮助。