BW cluster removal based on size (all parameters identified)

7 views (last 30 days)
Hello all,
I have a stack of 512 by 512 labeled binary images ("lab") from which I wish to remove clusters that are smaller than 8 pixel sizes in dimension. The cell array "idx" contains 33 matrices, specifying the indices of clusters that are larger than 8 pixel sizes in each of the 33 slices.
I encounter the error: "Error using ~= Matrix dimensions must agree." with the following code when I attempt my removal.
lab_large = zeros(512,512,33);
for i = 1:33
tmp = lab(:,:,i);
tmp2 = idx{i};
tmp(tmp~=tmp2(:))=0;
tmp(tmp~=0)=1;
lab_large(:,:,i) = tmp;
end
How should I tell MATLAB to maintain clusters identified in "idx" from "lab" and turn everything else into zeros?
I am aware that morphology operations may achieve a similar result with imclose, but I want to minimize altering the shapes of the surviving clusters.
Thank you so much!

Accepted Answer

Image Analyst
Image Analyst on 9 Feb 2016
Edited: Image Analyst on 9 Feb 2016
You can use bwareaopen() to get rid of blobs less than a certain size. If you have a bunch of other criteria, like circularity or solidity or whatever, then you can get rid of blobs by passing the list of indexes into ismember(). See my Image Segmentation Tutorial at http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
binaryImage = bwareaopen(binaryImage, 8); % Only 8 pixels or bigger survive.
or
labeledImage = bwlabel(binaryImage);
indexesToKeep = [2,4,13,33]; % Whatever....
newBinaryImage = ismember(labeledImage, indexesToKeep) > 0;
By the way, in image processing we don't call them "clusters". They're called "blobs" or "connected components".
  1 Comment
Brian
Brian on 10 Feb 2016
Edited: Brian on 10 Feb 2016
Always to the rescue. Thank you much! I will check out your tutorials as well.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!