MATLAB Answers

How do I count objects in a binary image without taking into account pixel clusters smaller than a certain number?

6 views (last 30 days)
Gabriel Levis
Gabriel Levis on 9 Jun 2020
I am getting into image processing and am trying to count the number of fruit loops in an image (as attached). I used the color threshholder app to isolate the fruit loops and convert the image to binary, then I inverted the colors so that the background was white and the loops were black, but there was still a little bit of feedback remaining in the image in the form of small clusters of black dots. I tried using the bwareaopen function to remove them but it didn't affect the image. When I used bwlabel to try to count the number of objects, it returned a number much higher than the amount of fruit loops in the bowl, and seemed to have been counting the black dots as well. Was there any step that I might've missed? Also, would there be a more conventional/easier was to go about counting the fruit loops?

Answers (1)

Harikrishnan Balachandran Nair
Edited: Harikrishnan Balachandran Nair on 16 Aug 2021
From my understanding, you are having an issue with counting the number of objects in a binary image. Connected components in a binary image is calculated by finding the pixels of value one, which are connected according to the specified connectivity. Hence the background should be black, and object should be white. Inorder to remove spurious objects from the image , you can use the function 'bwareaopen' as given below :
bw2=bwareaopen(bw,P);
Here, the connected components with less than 'P' pixels are removed from the image.
You can also try extracting objects from the image by specifying the property using the 'bwpropfilt' as given below :
bw2=bwpropfilt(bw,'attrib','range');
Here, the connected components in bw having the property specified in 'attrib' within the specified 'range' are extracted. You can choose the 'attrib' and 'range' according to your application.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by