简体   繁体   English

如何改进 OpenCV Python 脚本中 HoughLinesP() 的结果

[英]How can I improve the results of HoughLinesP() in my OpenCV Python script

I am trying to get all the lines in this image:我正在尝试获取此图像中的所有行:

在此处输入图片说明

This is the code that I'm using:这是我正在使用的代码:

threshold = 30
minLineLength =10
maxLineGap = 10
lines = cv2.HoughLinesP(img,1,np.pi/360, threshold, minLineLength, maxLineGap)

The problem is that I'm getting too many lines (~300):问题是我得到了太多行(~300):

在此处输入图片说明

But if I increase the threshold value it starts to miss some lines:但是如果我增加阈值它开始错过一些行:

在此处输入图片说明

Is there any way of reducing the number of lines while keeping line-detection accurate?有没有办法在保持线路检测准确的同时减少线路数量?

Thanks in advance!提前致谢!

It works (mostly) fine for me in Python/OpenCV.它在 Python/OpenCV 中(大部分)对我来说很好用。 Adjust your HoughP line arguments as appropriate.根据需要调整您的 HoughP 行参数。

I think you need to threshold your image first.我认为您需要先对图像进行阈值处理。 And perhaps thin the white lines.也许细化白线。

Input:输入:

在此处输入图片说明

import cv2
import numpy as np

# read image as color not grayscale
img = cv2.imread("lines.png", cv2.IMREAD_COLOR)

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

# do threshold
thresh = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)[1]

# get hough line segments
threshold = 30
minLineLength =10
maxLineGap = 10
lines = cv2.HoughLinesP(thresh, 1, np.pi/360, threshold, minLineLength, maxLineGap)

# draw lines
results = img.copy()
for [line] in lines:
    print(line)
    x1 = line[0]
    y1 = line[1]
    x2 = line[2]
    y2 = line[3]
    cv2.line(results, (x1,y1), (x2,y2), (0,0,255), 1) 

# show lines
cv2.imshow("lines", results)
cv2.waitKey(0)

# write results
cv2.imwrite("lines_hough.png",results)


Resulting Hough lines in red:结果是红色的霍夫线:

在此处输入图片说明

You get a lot of parallel very close lines that you may want to merge somehow or thin out the list.你会得到很多平行的非常接近的线,你可能想要以某种方式合并或缩小列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM