Skip to content Skip to sidebar Skip to footer

Counting Objects In Binary Images

I am working with binary images in which there are a lot of small blobs. I would like to count the number of blobs and have found out that contours are commonly used to do that. Ho

Solution 1:

Once you have the contours, you can use cv2.contourArea() and cv2.arclength() functions to get area and perimeter respectively. For example, let us say you want to find the area and perimeter of the first contour. The code will be something like this:

contour_area = cv2.contourArea(contours[0])
cont_perimeter = cv2.arcLength(contours[0], True)

You can also use these functions to sort the found contours. For example, to sort your contours based on area,

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

Here, 'reverse=True' sorts and puts it in a descending order -from largest to smallest. Now when you access sorted_contours[0], that is your largest contour found. You can also use other parameters to sort them. Refer here for documentation on some contour features like moments, area, perimeter, etc. that you can extract. Hope this helps!


Post a Comment for "Counting Objects In Binary Images"