How can I clean the data that bwconncomp provides?

I am trying to use bwconncomp to find the number of particles in an image, but sometimes the output from bwconncomp is totally wrong (I get results both significantly higher and lower than what the actual values should be). Is there a way to fine tune this, such as take into consideration the area of the particles?
Other attempts I've used include using the regionprops image property, but this doesn't account for the shape of the objects. I know that changing the connectivity from 8-connected to 4-connected would help when the output is too low, but this doesn't solve the issue of when Matlab counts more objects than should actually exist in the image.

2 Comments

I suggest attaching an example image and the code you are using that gives the wrong result for that image.
Hannah: Here is another chance for you to read the posting guidelines you blew past when you've been posting so far:
Make it easy for us to help you, not hard. We really can't do anything with what you've told us, and given us (no posted image or code).

Sign in to comment.

 Accepted Answer

Hi Hannah,
I believe you could uitilize the PixelIdxList information that bwconncomp outputs as part of a struct. This list is a 1-by-N cell array where the k-th element in the cell array is a vector containing the linear indices of the pixels in the k-th object. You can use this to choose those objects whose size satisfy a certain threshold. A small example is given below which shows how to remove objects that have number of pixels less than a set threshold:
a= zeros(3,3,3); %Initialize a 3-D array with zeros.
a(1,1,1) = 1; % Set values to 1 to demonstrate connected components
a(1,1,2) = 1;
a(3,3,2) = 1;
CC6 = bwconncomp(a,6); %Compute the connected components. This will have 2 elements including the isolated 1.
res = cellfun(@size,CC6.PixelIdxList,'UniformOutput',false);
% Remove the elements from the cell array which have size lesser than 2.
res( cellfun(@ (x) length(x) <2, CC6.PixelIdxList) ) = [ ];
Hope this helps!

4 Comments

Hello Uday,
Thank you for the help using bwconncomp. I attempted to apply your code to my solution, but I am confused. However, I found another Matlab function called bwareaopen that removes parts of the image where the area is below a certain threshold. This helped half of my problem.
Now, I am wondering if there is a way to split objects into multiple objects, using either the method you described above, or something like bwareafilt.
I'd like to do something along the following lines:
if areaOfObject > thresholdArea,
split object into (areaOfObject/thresholdArea) components.
Thanks again for your help!
Hi Hannah, my code tries to remove objects with pixels less than a given threshold but using an existing function like bwareaopen is certainly cleaner. Coming to your next question, An object here is a connected set of pixels, so how would you define splitting an object? Could you elaborate on what you're trying to Achieve?
In the case of my code, I am trying to get each object to represent one particle. However, I think some particles are so close together that they are considered one object in Matlab. I would like to break up clusters of particles, if that makes sense.
Just a guess, but you might need to look up some morphological operators like imopen <https://www.mathworks.com/help/images/ref/imopen.html imopen > to prepare your image before you apply area thresholding functions. You could try asking a separate question with your real image or a part of it, that way you might get better responses. Let me know here when you post the question with your image. I will definitely check it out.

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!