如何在OpenCV中检测线条?

71

我正在尝试检测停车场中的线,如下所示。

Empty parking lot

我希望得到明显的线条和交叉线的(x,y)位置。然而,结果并不是很理想。

Parking lot with Hough Lines drawn

我猜这是由于两个主要原因:

  1. 一些线非常断裂或缺失。即使人眼可以清楚地识别它们,HoughLine有时也会将一些缺失的线连接在一起,而且有时还会将不必要的线连接在一起,因此我更愿意手动完成。
  2. 有一些重复的线。

工作的一般流程如下所示:

1. 选择一些特定的颜色(白色或黄色)

import cv2
import numpy as np
import matplotlib
from matplotlib.pyplot import imshow
from matplotlib import pyplot as plt

# white color mask
img = cv2.imread(filein)
#converted = convert_hls(img)
image = cv2.cvtColor(img,cv2.COLOR_BGR2HLS)
lower = np.uint8([0, 200, 0])
upper = np.uint8([255, 255, 255])
white_mask = cv2.inRange(image, lower, upper)
# yellow color mask
lower = np.uint8([10, 0,   100])
upper = np.uint8([40, 255, 255])
yellow_mask = cv2.inRange(image, lower, upper)
# combine the mask
mask = cv2.bitwise_or(white_mask, yellow_mask)
result = img.copy()
cv2.imshow("mask",mask) 

二进制图像

2. 重复膨胀和腐蚀操作直到图像不再改变 (参考)

height,width = mask.shape
skel = np.zeros([height,width],dtype=np.uint8)      #[height,width,3]
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
temp_nonzero = np.count_nonzero(mask)
while(np.count_nonzero(mask) != 0 ):
    eroded = cv2.erode(mask,kernel)
    cv2.imshow("eroded",eroded)   
    temp = cv2.dilate(eroded,kernel)
    cv2.imshow("dilate",temp)
    temp = cv2.subtract(mask,temp)
    skel = cv2.bitwise_or(skel,temp)
    mask = eroded.copy()
 
cv2.imshow("skel",skel)
#cv2.waitKey(0)

腐蚀和膨胀后的图像

3. 运用Canny算法过滤线条,并使用HoughLinesP获取线条

edges = cv2.Canny(skel, 50, 150)
cv2.imshow("edges",edges)
lines = cv2.HoughLinesP(edges,1,np.pi/180,40,minLineLength=30,maxLineGap=30)
i = 0
for x1,y1,x2,y2 in lines[0]:
    i+=1
    cv2.line(result,(x1,y1),(x2,y2),(255,0,0),1)
print i

cv2.imshow("res",result)
cv2.waitKey(0)

卡尼边缘检测结果

我想知道为什么在选择了特定颜色后,线条会出现断裂和噪声。我认为我们应该在这一步中做一些事情来使断裂的线变成完整、更少噪声的线。然后再尝试应用某些方法来进行卡尼和霍夫线变换。有任何想法吗?


1
你不需要检测边缘,可以直接在二进制图像上使用 HoughLinesP - alkasm
嘿,看看我的更新答案。我认为它在交叉检测方面有些符合你的要求。 - Saedeas
在主代码中,您只输入了黄线的RGB代码。此外,如果您将其编辑为白色,则可以获得结果。 - anengineer-py
1
所有给出的答案都没有检测到线条,而是检测到了边缘,也就是线条的边缘。对于图片中的每一条线,输出中会得到2条检测到的线。即使原始线宽为1像素,之前的细化/骨架化也无法解决这个问题。如何调整代码,使得只返回这两条线的中心线,而不是两条线? - granular bastard
5个回答

62

这是我的流程,也许能对你有所帮助。

首先,获取灰度图像并进行高斯模糊处理。

img = cv2.imread('src.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)

其次,使用Canny算法进行边缘检测。

low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)

然后,使用HoughLinesP获取直线。 您可以调整参数以获得更好的性能。

rho = 1  # distance resolution in pixels of the Hough grid
theta = np.pi / 180  # angular resolution in radians of the Hough grid
threshold = 15  # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50  # minimum number of pixels making up a line
max_line_gap = 20  # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0  # creating a blank to draw lines on

# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                    min_line_length, max_line_gap)

for line in lines:
    for x1,y1,x2,y2 in line:
    cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)

最后,在您的srcImage上画出线条。

# Draw the lines on the  image
lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)

这是我的最终表演。

最终效果图:

输入图片描述


35
我不确定你在问什么,因为你的帖子中没有问题。
一种很好且强大的检测线段的技术是LSD(线段检测器),自openCV 3以来在openCV中可用。
这是一些简单的基本C++代码,很可能可以轻松转换为Python:
int main(int argc, char* argv[])
{
    cv::Mat input = cv::imread("C:/StackOverflow/Input/parking.png");
    cv::Mat gray;
    cv::cvtColor(input, gray, CV_BGR2GRAY);


    cv::Ptr<cv::LineSegmentDetector> det;
    det = cv::createLineSegmentDetector();



    cv::Mat lines;
    det->detect(gray, lines);

    det->drawSegments(input, lines);

    cv::imshow("input", input);
    cv::waitKey(0);
    return 0;
}

给出这个结果:

enter image description here

比起你的图像(没有重复线条等),哪个更适合进一步处理。

附加信息(感谢@Ivan):

“由于原始代码许可证冲突,实现已从OpenCV版本3.4.6到3.4.15和版本4.1.0到4.5.3中删除。在计算NFA代码发布在MIT许可证下后,再次恢复。”


路口?也许您可以按角度和距离(并且可能还有霍夫线)对线段进行分组,如果您知道停车位标记的一般外观,您可以执行透视校正,这将使比较和分组线段更容易。总体而言,应该有一些“格式塔理论”方法,但据我所知,研究尚未达到那个程度...... - Micka
4
LSDDetector在Python OpenCV中已被弃用。 - Anshuman Kumar
1
还没有测试,但EDLines也可能很好:https://github.com/CihanTopal/ED_Lib - Micka
1
从OpenCV版本3.4.6到3.4.15和版本4.1.0到4.5.3中,已经移除了Implementation,原因是原始代码许可证冲突。在计算NFA代码发布了MIT许可证后,又恢复了。 - undefined

25

对于你的问题第一部分已经有了一些很好的答案,但是关于第二部分(如何找到线的交点)我并没有看到太多信息。

我建议你看一下Bentley-Ottmann算法

这里有一些算法的Python实现:这里这里

编辑:使用VeraPoseidon的Houghlines实现和这里链接的第二个库,我成功地得到了检测交点的结果。非常感谢Vera和库作者的工作。绿色的正方形代表检测到的交点。虽然存在一些错误,但对我来说这似乎是一个非常好的起点。看起来你想要检测交点的大部分位置都有多个交点被检测到,因此你可以在图像上运行适当大小的窗口,查找多个交点,并将真实的交点视为激活该窗口的位置。

Bentley-Ottmann applied to Houghlines

以下是我用来产生这个结果的代码:

import cv2
import numpy as np
import isect_segments_bentley_ottmann.poly_point_isect as bot


img = cv2.imread('parking.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)

low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)

rho = 1  # distance resolution in pixels of the Hough grid
theta = np.pi / 180  # angular resolution in radians of the Hough grid
threshold = 15  # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50  # minimum number of pixels making up a line
max_line_gap = 20  # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0  # creating a blank to draw lines on

# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                    min_line_length, max_line_gap)
print(lines)
points = []
for line in lines:
    for x1, y1, x2, y2 in line:
        points.append(((x1 + 0.0, y1 + 0.0), (x2 + 0.0, y2 + 0.0)))
        cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 5)

lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
print(lines_edges.shape)
#cv2.imwrite('line_parking.png', lines_edges)

print points
intersections = bot.isect_segments(points)
print intersections

for inter in intersections:
    a, b = inter
    for i in range(3):
        for j in range(3):
            lines_edges[int(b) + i, int(a) + j] = [0, 255, 0]

cv2.imwrite('line_parking.png', lines_edges)

你可以使用这个代码块作为一种策略,来移除小范围内的多个交叉点:

for idx, inter in enumerate(intersections):
    a, b = inter
    match = 0
    for other_inter in intersections[idx:]:
        if other_inter == inter:
            continue
        c, d = other_inter
        if abs(c-a) < 15 and abs(d-b) < 15:
            match = 1
            intersections[idx] = ((c+a)/2, (d+b)/2)
            intersections.remove(other_inter)

    if match == 0:
        intersections.remove(inter)

输出图像:清理后的输出

但是您需要调整窗口函数。


如何在Node.js中执行相同的过程。 - PvDev
你如何导入isect_segments_bentley_ottmann? - PayamB.
你可以直接下载此处链接的库并将其添加到你的项目中,然后从那里引用它。 - Saedeas
嘿,用于删除重复交点的代码将删除最后一个交点,因为它将交点与自身进行比较。在图片中,您可以看到右下角的交点不在那里。可以通过以下方式进行修复: continue``` 干杯。 - JeroSquartini
好的,注意我已经将其编辑到正确的位置了。 - Saedeas

3

我是初学者。

我得到了一些对于这个问题可能有帮助的东西。

检测图像中线条的简单方法。

输出:

output

以下是在谷歌Colab上执行的代码。

import cv2
import numpy as np
from google.colab.patches import cv2_imshow
!wget  https://istack.dev59.com/sDQLM.webp
#read image 
image = cv2.imread( "/content/sDQLM.png")

#convert to gray
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

#performing binary thresholding
kernel_size = 3
ret,thresh = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)  

#finding contours 
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

#drawing Contours
radius =2
color = (30,255,50)
cv2.drawContours(image, cnts, -1,color , radius)
# cv2.imshow(image) commented as colab don't support cv2.imshow()
cv2_imshow(image)
# cv2.waitKey()


2

如果您调整最大线间隙或侵蚀核的大小会发生什么?另外,您可以找到线之间的距离。您需要浏览一些线对,例如ax1,ay1到ax2,ay2 c.f. bx1,by1到bx2,by2,您可以找到垂直于线b的梯度(-1/线的梯度)的点与线a相交的位置。基本的学校几何和同时方程,类似于:

x = (ay1 - by1) / ((by2 - by1) / (bx2 - bx1) + (ax2 - ax1) / (ay2 - ay1))
# then
y = by1 + x * (by2 - by1) / (bx2 - bx1)

并将x、y与ax1、ay1进行比较。

PS:您可能需要添加一个检查ax1、ay1和bx1、by1之间距离的步骤,因为您的一些线条看起来是其他线条的延续,这些线条可能会被最近点技术消除。


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