车道检测噪声去除(得到太多不必要的线)
Lane detection noise removal (getting too many unnecessary lines)
我正在尝试进行车道检测,代码如下,我将 HoughLinesP 应用于 canny 边缘检测 o/p。所以这个想法是只显示那些线(通常出现在视频中+更可能是一条车道,即通过拾取角度)。我不想使用任何机器学习算法。所以请帮忙..
详情如下:
代码:
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()
Here's link to videos I'm using
在 4.mp4 中,您可以看到在 运行 这段代码之后,几秒钟后,一个人进来了,因为 canny 在那个区域检测到这么多边缘,所以有这么多行,其次我已经我想要动态的固定图像区域,这个想法是在更可能的车道的基础上设置图像区域。
还有一串线出现,我想把它缩短到更可能的线。感谢您阅读。
你不会得到更好的结果。这只是你问题的本质。您现在必须开始创建您的车道的数学模型,并使用您的霍夫线来更正该模型。
例如您可以使用卡尔曼滤波器跟踪图像某些波段中的车道。然后,当您观察到观察带周围预期角度内的线段时,您可以使用此过滤器的预测步骤。
我正在尝试进行车道检测,代码如下,我将 HoughLinesP 应用于 canny 边缘检测 o/p。所以这个想法是只显示那些线(通常出现在视频中+更可能是一条车道,即通过拾取角度)。我不想使用任何机器学习算法。所以请帮忙..
详情如下:
代码:
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()
Here's link to videos I'm using
在 4.mp4 中,您可以看到在 运行 这段代码之后,几秒钟后,一个人进来了,因为 canny 在那个区域检测到这么多边缘,所以有这么多行,其次我已经我想要动态的固定图像区域,这个想法是在更可能的车道的基础上设置图像区域。 还有一串线出现,我想把它缩短到更可能的线。感谢您阅读。
你不会得到更好的结果。这只是你问题的本质。您现在必须开始创建您的车道的数学模型,并使用您的霍夫线来更正该模型。
例如您可以使用卡尔曼滤波器跟踪图像某些波段中的车道。然后,当您观察到观察带周围预期角度内的线段时,您可以使用此过滤器的预测步骤。