2017-01-24 67 views
0

我正在嘗試查找給定矩形的每個角的座標。 到目前爲止,我已經能夠使用輪廓函數獲得兩個角點,但是當我查看整個輪廓數組時,有大量點要篩選。我只想找查找等高線的座標

import cv2 
import numpy as np 

#open image 
img = cv2.imread('cnt-coords.jpg') 

#convert to black and white 
bw = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 

#threshold image 
ret, thresh = cv2.threshold(bw,127,255,0) 

#find contours 
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 

#obtain the first 2 points of contours 
cntx1 = contours[0][0] 
cntx = contours[0][1] 

#convert coords to points 
pt1 = (cntx1[0][0],cntx1[0][1]) 
pt2 = (cntx[0][0],cntx[0][1]) 

#draw circles on coordinates 
cv2.circle(img,pt1,5,(0,255,0),-1) 
cv2.circle(img,pt2, 5, (0,255,0),-1) 

#display the image 
cv2.imshow('f',img) 

cv2.waitKey(0) 
cv2.destroyAllWindows() 

我通過什麼contours回到過篩,我和它的點的巨大的數組,這似乎是所有的最極端值(最大值和最小值x和y的值)沿着我的形象的對角點。有什麼方法可以簡化我收到的點,在這種情況下,這個矩形/平行四邊形的角?

test contours

回答

1

findContours功能上輪廓返回像素。您可以嘗試使用OpenCV的approxPolyDP函數來查找輪廓的多邊形逼近。用法和解釋請見here。在你的情況下,它會像下面這樣:

epsilon = cv2.arcLength(contours[0],True) 
approx = cv2.approxPolyDP(contours[0],epsilon,True) 
+0

謝謝 - 使用的約10%,我能夠找到的平行四邊形/矩形的所有四個角的弧長 –