Home > Software design >  cv2.contourArea will always return an error
cv2.contourArea will always return an error

Time:06-11

No matter what I try I can't seem to get cv2.contourArea to work properly, it will always return the error:

(-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'contourArea'

My code is trying to find the largest contour in an image and remove the rest with this code:

def find_biggest_contour(image):

# Copy to prevent modification
image = image.copy()

_,contours  = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

#get contour sizes and return the biggest contour
max_area = -1
for i in range(len(contours)):
    area = cv2.contourArea(contours[i])
    if area>max_area:
        biggest_contour = contours[i]
        max_area = area

#create an empty mask
mask = np.zeros(image.shape, np.uint8)

#draw the biggest contour on it
cv2.drawContours(mask, [biggest_contour], -1, 255, -1)

return mask

CodePudding user response:

Maybe it's just the return value order is reversed.

contours, hierarchy = cv2.findContours()
  • Related