所以我试图在棋盘上获得霍夫线,但该算法只检测到一条线。我正在使用 python 2.7 和 opencv 3.0。这是代码:
def applyHoughLineTransform():
image1 = cv2.imread('pictures/board1.png',0)
image2 = cv2.imread('pictures/board2.png',0)
image3 = cv2.imread('pictures/board3.png')
image4 = cv2.imread('pictures/board4.png')
lines1 = cv2.HoughLines(image1,1,math.pi/180.0,5)
lines2 = cv2.HoughLines(image2,1,math.pi/180.0,5)
lines1 = lines1[0]
lines2 = lines2[0]
for rho,theta in lines1:
print ('Rho and theta:',rho,theta)
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))
print (x1,y1)
print (x2,y2)
cv2.line(image3,(x1,y1),(x2,y2),(0,0,255),2)
for rho,theta in lines2:
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(image4,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imwrite('pictures/board1.png',image1)
cv2.imwrite('pictures/board2.png',image2)
cv2.imshow('hough line 1',image3)
cv2.imshow('hough line 2',image4)
这是我执行霍夫线算法的精明边缘图像:
结果如下:
如你所见,相当蹩脚。精明的算法似乎提供了非常好的操作边缘。我不完全确定我做错了什么。我想这与输入 houghLines 函数的参数有关。如果有人能指出我正确的方向(或完全解决我的问题:))我将不胜感激。这是我正在使用的教程网站的链接:http: //opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html