问题描述
我在OpenCV中遇到了通用霍夫变换(Guil版本)问题。我的代码:
def generalized_Hough():
img = cv2.imread("img.png")
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
template = cv2.imread("template.png")
height,width = template.shape[:2]
edges = cv2.Canny(template,200,250)
ght = cv2.createGeneralizedHoughGuil()
ght.setTemplate(edges)
ght.setMindist(100)
ght.setMinAngle(0)
ght.setMaxAngle(360)
ght.setAngleStep(1)
ght.setLevels(360)
ght.setMinScale(1)
ght.setMaxScale(1.3)
ght.setScaleStep(0.05)
ght.setAngleThresh(100)
ght.setScaleThresh(100)
ght.setPosThresh(100)
ght.setAngleEpsilon(1)
ght.setLevels(360)
ght.setXi(90)
positions = ght.detect(img_gray)[0][0]
for position in positions:
center_col = int(position[0])
center_row = int(position[1])
scale = position[2]
angle = int(position[3])
found_height = int(height * scale)
found_width = int(width * scale)
rectangle = ((center_col,center_row),(found_width,found_height),angle)
Box = cv2.BoxPoints(rectangle)
Box = np.int0(Box)
cv2.drawContours(img,[Box],(0,255),2)
for i in range(-2,3):
for j in range(-2,3):
img[center_row + i,center_col + j] = 0,255
cv2.imwrite("results.png",img)
因此,我读取图像和模板,将模板获取为Canny边缘,然后配置GHT(比例和角度的Guil版本)并进行图检测。问题在于,尽管将角度min,max和step分别设置为0、360和1,结果始终会捕捉到最接近的90度:
[[294. 110. 1. 270.]
[100. 303. 1. 0.]
[561. 312. 1. 0.]
[461. 126. 1. 90.]
[194. 109. 1. 0.]]
图片示例:
模板示例:
结果:
很明显,左上和右上的检测或多或少都在正确的位置和比例中,但是角度是错误的。我该如何解决?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)