m.hatami
m.hatami

Reputation: 603

how can i access to an array x by giving y?

i have an array of a contour in opencv like this :

  [ 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])

And i need to find x values by giving y i tried this code but i have no idea about numpy where statment

for y in range(deUpTemp[1],deDownTemp[1]):
    print( c[ np.where(c[1,:]==y), 0 ][0] )

Upvotes: 0

Views: 49

Answers (1)

Dr Yuan Shenghai
Dr Yuan Shenghai

Reputation: 1915

Following this 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

Since you are finding the largest one as c then

c[k][1]=y1

You just need to compare,

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

Upvotes: 1

Related Questions