2016-04-02 704 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

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