我正在尝试开发一种可以在路上检测车道的程序。我已经尝试了霍夫线变换和概率霍夫线变换。然而,这些都没有得到我想要的结果。
原始图片:
霍夫线变换
概率Hough线变换
似乎对于霍夫线变换,我至少可以检测到整个车道,但不幸的是,该线只是无限地(直到它们离开图片),到达线相互交叉的点,不是一个好的图形车道检测标记。
我也尝试了Probalistic Hough Line变换,用于车道检测的绿线不会像另一个那样无限地变化,但它无法标记和检测整个车道。
我试图在这里复制结果(通过用Python编写)
http://www.transistor.io/revisiting-lane-detection-using-opencv.html
我该怎么做才能解决这个问题?
代码:
import numpy as np
import cv2
from matplotlib import pyplot as plt
from PIL import Image
import imutils
def invert_img(img):
img = (255-img)
return img
def canny(imgray):
imgray = cv2.GaussianBlur(imgray, (5,5), 200)
canny_low = 5
canny_high = 150
thresh = cv2.Canny(imgray,canny_low,canny_high)
return thresh
def filtering(imgray):
thresh = canny(imgray)
minLineLength = 1
maxLineGap = 1
lines = cv2.HoughLines(thresh,1,np.pi/180,0)
#lines = cv2.HoughLinesP(thresh,2,np.pi/180,100,minLineLength,maxLineGap)
print lines.shape
# Code for HoughLinesP
'''
for i in range(0,lines.shape[0]):
for x1,y1,x2,y2 in lines[i]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
'''
# Code for HoughLines
for i in range(0,5):
for rho,theta in lines[i]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
return thresh
img = cv2.imread('images/road_0.bmp')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = imutils.resize(img, height = 500)
imgray = imutils.resize(imgray, height = 500)
thresh = filtering(imgray)
cv2.imshow('original', img)
cv2.imshow('result', thresh)
cv2.waitKey(0)
答案 0 :(得分:0)
酷话题!首先,你为什么要添加高斯模糊?你的源文章根本没有提到。如果我删除它,我会立即得到额外的疯狂线条,我可以用canny_low和canny_high调低音量。关于我能找到的最好的是低= 100和高= 180。
其次,你把文章翻译成Python做得很好。但是,我认为你遗漏了一个至关重要的细节。作者写道:
// Canny algorithm
Mat contours;
Canny(image,contours,50,350);
Mat contoursInv;
threshold(contours,contoursInv,128,255,THRESH_BINARY_INV);
您实现了Canny功能(cv2.canny()),但您没有调用阈值功能。根据{{3}},此函数"对每个数组元素应用固定级别阈值。"我试验了你的代码并想出了以下内容。
#thresh = canny(imgray) # original
edges = canny(imgray) # docs refer to return value as "edges"
retval, dst = cv2.threshold(edges, 128, 255,cv2.THRESH_BINARY_INV)
返回两个值 - retval对我们来说现在并不是特别重要。 dst是阈值处理后的目标2D图像数据阵列。然后,您将更新您对cv2.HoughLines和cv2.HoughLinesP的调用,以替换" thresh"用" dst。"当我这样做时,我得到了更多有趣的行为,虽然我无法找到正确的调整值以使线条运行良好。
所以,希望这会给你一些指示。尝试我的提示,并阅读一两次文章,以仔细检查您是否有与作者相同的程序流程。这似乎是一个有趣的项目,玩得开心!