我正在尝试进行车道检测,代码如下,我在canny边缘检测的o/p上应用了HoughLinesP。所以这个想法是只显示那些线(通常出现在视频上+更可能是一条车道,即通过拾取角度)。我不想使用任何机器学习算法。所以请帮忙..
以下是详细信息:
代码 :
import time
import numpy as np
import matplotlib.pyplot as plt
vid = cv2.VideoCapture('4.mp4')
while True:
#cv2.namedWindow('frame',cv2.WINDOW_NORMAL)
ret, img_color = vid.read()
if not ret:
vid = cv2.VideoCapture('5.mp4')
continue
num_rows, num_cols = img_color.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), 270, 0.56) #3
img_rotated = cv2.warpAffine(img_color, rotation_matrix, (num_cols, num_rows))
height, width = img_rotated.shape[:2]
img_resize = cv2.resize(img_rotated,(int(0.8*width), int(0.8*height)), interpolation = cv2.INTER_CUBIC) #2
img_clone = img_resize[10:842,530:1000].copy()
img_roi = img_resize[10+250:842-200,530:1000]
img_gray = cv2.cvtColor(img_roi,cv2.COLOR_BGR2GRAY) #1
kernel = [ [0,-1,0], [-1,5,-1], [0,-1,0] ]
kernel = np.array(kernel)
img_sharp = cv2.filter2D(img_gray,-1,kernel)
blur = cv2.GaussianBlur(img_sharp,(5,5),0)
img_canny = cv2.Canny(blur,130,170, apertureSize = 3) #4
lines = cv2.HoughLinesP(img_canny, 1, np.pi/180, 60, maxLineGap = 240)
if lines is not None:
print(len(lines))
for line in lines:
x1,y1,x2,y2 = line[0]
cv2.line(img_clone, (x1,y1+250), (x2,y2+250), (0,255,0), 2)
#cv2.line(img_clone, (x1,y1), (x2,y2), (255,255,0), 3)
cv2.imshow('frame',img_clone)
cv2.imshow('frame2', img_canny)
k = cv2.waitKey(35) & 0xFF
if k==27 :
break
vid.release()
cv2.destroyAllWindows()
在 4.mp4 中,您可以看到,在运行此代码后,几秒钟后,一个人进来了,因为 Canny 检测到该区域中有如此多的边缘,因此有很多行,其次我已经固定了我想要的图像区域是动态的,这个想法是在更可能的车道的基础上设置图像区域。还有一串线出现,我想把它缩短成更可能的线。感谢您的阅读。
白板的微信
胡子哥哥
随时随地看视频慕课网APP
相关分类