我正在尝试使用 openCV 和霍夫变换检测网球场中的线。我想找到水平线和垂直线,以便找到交点并最终检测网球场的角落。
Here the original image.
但我有一些问题。
1)我尝试使用 HoughLineP 。
这里的代码:
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,100,200,apertureSize = 3)
lines = cv2.HoughLinesP(edges, 1, np.pi/2, 6, None, 50, 10);
for line in lines[0]:
pt1 = (line[0],line[1])
pt2 = (line[2],line[3])
cv2.line(img, pt1, pt2, (0,0,255), 2)
cv2.imshow('dst',img)
return res
结果如下:houghLineP 的结果 http://www.hostingpics.net/viewer.php?id=726975houghP.png
2)我尝试使用HoughLines
这是代码
gray=cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,100,200,apertureSize = 3)
#Lignes
lines = cv2.HoughLines(edges,1,np.pi/70,110)
for rho,theta in lines[0]:
if (np.pi/70 <= theta <= np.pi/7) or (2.056 < theta < 4.970) or (1.570 <= theta <= 1.600): #(2,6 <=theta <= 26) or (theta >118 and theta <= 285)
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(res,(x1,y1),(x2,y2),(0,0,255),1)
结果如下:霍夫线的结果 http://www.hostingpics.net/viewer.php?id=282743houghline.png
在第一种情况下,我只有很少的线条,我想过延长它们,但我没有找到...我尝试使用 fitLine 但它只适用于查找轮廓(findContours 方法在这张图片中很糟糕)
在第二种情况下,它工作得很好,但我有很多线,几乎相同,在右下角,我没有任何交集来检测角点......
也许我走错路了...
您有一些想法或可行的东西吗?最后,我只想要与网球场有关的兴趣点。
ps : I did a method which calculate the intersection between the horizontal lines and the vertical lines.
非常感谢,