GetBoosted
GetBoosted

Reputation: 41

Biggest Nested Contour OpenCV

Im struggling at finding the biggest nested contour in my hierarchy. Can someone help? I just need the biggest shape of the contours and need to check if the second-biggest contour is a child of it. Here is my code:

contours, hierarchy = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    sorted_contour = sorted(contours, key=cv2.contourArea, reverse=True)

    bigcontour1 = sorted_contour[0]
    bigcontour2 = sorted_contour[1]
    

Upvotes: 0

Views: 293

Answers (1)

fmw42
fmw42

Reputation: 53081

Why not just search for the largest external contour in Python/OpenCV?

contours = cv2.findContours(thresh2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

Upvotes: 3

Related Questions