我在opencv中有一个轮廓数组,如下所示:
[ x : y ]
_____________
[[264 69]]
[[265 69]]
[[266 69]]
[[266 70]]
[[266 85]]
contours,hierarchy = cv2.findContours(masked_data.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
#find biggest countour based on area
c=max(contours,key=cv2.contourArea)
deUpTemp = tuple(c[c[:, :, 1].argmin()][0])
deDownTemp = tuple(c[c[:, :, 1].argmax()][0])
我需要通过给y查找x值 我尝试了这段代码,但我不知道numpy的声明
for y in range(deUpTemp[1],deDownTemp[1]):
print( c[ np.where(c[1,:]==y), 0 ][0] )
答案 0 :(得分:1)
遵循此link
import cv2
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# contours = [array([[[x1, y1]], ..., [[xn, yn]]]), array([[[x1, y1]], ..., [[xn, yn]]])]
contour = contours[0] # contours[i], where i = index of the contour
# contour = [[[x1, y1]], [[x2, y2]], ..., [[xn, yn]]]
# contour[0] = [[x1, y1]]
# contour[0][0] = [x1, y1]
# contour[0][0][0] = x1
# contour[0][0][1] = y1
因为您找到了最大的c 然后
c[k][1]=y1
您只需要比较
if c[k][1]==y: #k denotes the index of point 1 denotes channel 1 as in y coordinate
print c[k] # if find match, print current c[k] point