我在使用Python中的OpenCV的HoughLinesP时,发现很难找到这个图片上棋盘格子的线。
为了理解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起作用。 有什么想法吗?
图片在此处:Results