我很难在此图像中使用HoughLinesP和OpenCV在Python中找到棋盘上的行。
为了理解HoughLinesP的参数,我提出了以下代码:
import numpy as np
import cv2
from matplotlib import pyplot as plt
from matplotlib import image as image
I = image.imread('chess.jpg')
G = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)
# Canny Edge Detection:
Threshold1 = 150;
Threshold2 = 350;
FilterSize = 5
E = cv2.Canny(G, Threshold1, Threshold2, FilterSize)
Rres = 1
Thetares = 1*np.pi/180
Threshold = 1
minLineLength = 1
maxLineGap = 100
lines = cv2.HoughLinesP(E,Rres,Thetares,Threshold,minLineLength,maxLineGap)
N = lines.shape[0]
for i in range(N):
x1 = lines[i][0][0]
y1 = lines[i][0][1]
x2 = lines[i][0][2]
y2 = lines[i][0][3]
cv2.line(I,(x1,y1),(x2,y2),(255,0,0),2)
plt.figure(),plt.imshow(I),plt.title('Hough Lines'),plt.axis('off')
plt.show()我现在遇到的问题是,这只涉及到一条线。如果我把maxLineGap减少到1,它就会增加数千个。
我理解为什么这可能是,但我如何选择一个合适的参数集,以获得所有这些共线性线合并?我是不是遗漏了什么?
我想让代码保持简单,因为我正在使用它作为这个函数的示例。
提前感谢您的帮助!
更新:这完全适用于HoughLines。
而且似乎没有边缘检测问题,因为Canny工作得很好。
然而,我仍然需要让HoughLinesP工作。有什么主意吗?
图片:结果
发布于 2016-03-04 12:03:13
好的,我终于找到了这个问题,我想我可以和其他人分享这个问题的解决方案。问题是,在HoughLinesP函数中,有一个额外的参数“line”,它是多余的,因为函数的输出是相同的:
Cv2.HoughLinesP(图像,rho,θ,阈值[,线条[,minLineLength,maxLineGap])
这将导致参数以错误的顺序读取时出现问题。为了避免与参数的顺序混淆,最简单的解决方案是在函数中指定它们,如下所示:
lines = cv2.HoughLinesP(E,rho = 1,theta = 1*np.pi/180,threshold = 100,minLineLength = 100,maxLineGap = 50)这完全解决了我的问题,我希望它能帮助别人。
发布于 2020-07-22 07:04:44
样本应用
import cv2
import numpy as np
img = cv2.imread('sudoku.png', cv2.IMREAD_COLOR)
# Convert the image to gray-scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the edges in the image using canny detector
edges = cv2.Canny(gray, 50, 200)
# Detect points that form a line
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=10, maxLineGap=250)
# Draw lines on the image
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 0), 3)
# Show result
img = cv2.resize(img, dsize=(600, 600))
cv2.imshow("Result Image", img)
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()

发布于 2018-09-02 17:20:54
Cv2.HoughLinesP(图像,rho,θ,阈值,np.array ( ),minLineLength=xx,maxLineGap=xx)
这也是可行的。
https://stackoverflow.com/questions/35609719
复制相似问题