2016-04-02 703 views
2

我使用下面的算法(cornerHarris)检测了此图像的角点(图像中的红点)。现在我想获得该点的坐标。我怎样才能做到这一点?如何使用python获取x,y坐标?

import cv2 
import numpy as np 

filename = 'Square.jpg' 
img = cv2.imread(filename) 
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 

gray = np.float32(gray) 
dst = cv2.cornerHarris(gray,2,3,0.04) 

#result is dilated for marking the corners, not important 
dst = cv2.dilate(dst,None) 

# Threshold for an optimal value, it may vary depending on the image. 
img[dst>0.01*dst.max()]=[0,0,255] 

cv2.imshow('dst',img) 
cv2.waitKey(0) 
cv2.imwrite('CornerSquare.jpg',img) 

enter image description here

+0

我不是太熟悉Python或哈里斯来者,但通过扫描你的代码,我没有看到,是指任何代码,您已经检测到的红点 –

+0

验证码工作中!而我上传的图片就是我通过运行它所得到的。 –

回答

2
coord = np.where(np.all(img == (0, 0, 255), axis=-1)) 
print zip(coord[0], coord[1]) 
+0

我应该把这段代码放在我的代码中?在点上红色或其他地方的颜色之后 –

+0

@ManojAmarasekera上面的代码只是计算图片中每个红点的坐标。因此,它需要放在'img [dst> 0.01 * dst.max()] = [0,0,255]'之后。如果您使用python3,则可能需要调整'zip'行,因为python3将返回一个指针而不是一个列表。 –

+0

你的答案是我所需要的。谢谢! –