How can I clean the data that bwconncomp provides?
1 view (last 30 days)
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.
Uday Pradhan on 27 May 2021
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!