道路车道检测程序不能正确检测车道

3

我正在尝试开发一个能够检测道路上车道的程序。我已经尝试过Hough线变换和概率Hough线变换,但是都没有得到我想要的结果。

原始图像:

enter image description here

霍夫线变换

enter image description here enter image description here

概率霍夫变换

enter image description here enter image description here

似乎对于霍夫线变换,我至少可以检测到整个车道,但不幸的是,该线路只会无限延伸(直到它们移出图片),并且各自交点的位置也不是一个良好的图形车道检测标记。
我还尝试了概率霍夫线变换,用于车道检测的绿色线不像另一个方法那样无限延伸,但它无法标记和检测整个车道。
我正在尝试在这里复制结果(通过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)
1个回答

0

酷主题!首先,你为什么要添加高斯模糊?你的源文章根本没有提到这一点。如果我去掉它,我会立即得到更多的疯狂线条,我可以通过设置canny_low和canny_high来降低它。关于最好的设置,我找到了low=100和high=180。

screenshot of algorithm finding road lines

第二点,你在将文章翻译成Python方面做得相当不错。然而,我认为你遗漏了一个关键细节。作者写道:
// Canny algorithm
Mat contours;
Canny(image,contours,50,350);
Mat contoursInv;
threshold(contours,contoursInv,128,255,THRESH_BINARY_INV);

你实现了Canny函数(cv2.canny()),但是你没有调用阈值函数。根据我找到的文档,该函数“对每个数组元素应用固定级别的阈值”。我尝试了你的代码,并得出了以下结论。
#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 是阈值处理后的目标二维图像数据数组。然后,您需要更新调用 cv2.HoughLines 和 cv2.HoughLinesP 的代码,将 "thresh" 替换为 "dst"。当我这样做时,我得到了更多有趣的行为,尽管我无法找到正确的调整值使线条工作良好。

因此,希望这些提示能给您一些指导。尝试我的建议,并再读一两遍文章,以确保您与作者具有相同的程序流程。这似乎是一个有趣的项目,祝您玩得愉快!


谢谢你的建议,但我得到了奇怪的结果,就像这样:http://s13.postimg.org/ok14ocw2v/image.jpg 我可能错误地复制了你的代码,请问你能否编辑你的评论,包括你使用的完整代码?谢谢。 - user3377126
啊,我很抱歉。从我写的内容中可能并不清楚。我发布的这张图片仅仅是我所描述的“第一步”的结果。当我实现“第二步”时,我也得到了大部分填充的完整图片,类似于你的图片。但我不知道为什么会这样。不过,我猜想它应该是正确的轨迹,因为它紧跟着文章。我猜想问题可能出在调整那些值(128, 255, canny_low, canny_high)上。此外,可能还有一些我们尚未添加自原始文章中的额外代码。希望这能有所帮助! - nshiff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接