Skip to content Skip to sidebar Skip to footer

How To Access Opencv Contour Point Indexes In Python?

Is there way to access the contour[i][j] in python? I am struggling to translate this c++ into python, because the data structures are different. It's being hard making comparision

Solution 1:

OpenCV Python findCountours can be used like this

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

That's what you need

pt0 = contour[i][j][0] # that's what you need to replace pt0 = contours[i][j];# pt0 = [x, y], where pt0[0] = x, pt0[1] = y

Post a Comment for "How To Access Opencv Contour Point Indexes In Python?"