Reputation: 605
I'm currently working with opencv 3.4 and getting the contours as a list of coordinates from a binary image. My code looks like the following:
_, contours, _ = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE
list_of_contours= []
for contour in contours:
list_of_contours.append(list(map(tuple, (coord[0] for coord in contour))))
It is working as intended but I'm curious if the loop in list(map(tuple, (c[0] for c in contour)))
could be avoided and/or is it even reasonable to do so?
Upvotes: 1
Views: 609
Reputation: 18341
Try this:
cnts = cv2.findContours(gray,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
xcnts = np.vstack((x.reshape(-1,2) for x in cnts))
print(xcnts.shape)
It return (698, 2)
for this image (from opencv - plot contours in an image):
Upvotes: 2