How to identify the nth largest components in binary image stacks
Show older comments
Hello,
I have some code that is programmed to identify the largest blob within a stack of binary images and delete all surrounding blobs, exporting the new image files.
I want to edit to code to identify nth largest blobs and delete everything else. I will probably go as high as 3rd to 5th largest blobs.
Currently the code looks like this:
for i=0:num_of_files
CC(:,i+1) = bwconncomp(BW(:,:,i+1),26);
end
n1 =zeros(1023,997,11);
for i = 0:num_of_files
newimage=zeros(1023,997);
[~, maxcell(i+1)] = max(cellfun(@numel, CC(i+1).PixelIdxList));
newimage(CC(i+1).PixelIdxList{1,maxcell(i+1)}) = 255;
n1 (:,:,i+1)= newimage;
imwrite(newimage,sprintf('%s%d.%s', name_of_output, i, file_type));
How can I edit to take highest 3 blobs into account in each image slice?
Regards, R.
2 Comments
Guillaume
on 8 Aug 2018
Can you explain why you have:
CC(:, i+1) = ...
Instead of
CC(i+1) = ...
?
Can you also explain why you're using a connectivity of 26 when you're passing 2D slices to bwconncomp?
Also, it looks like the number of files/slices is num_of_files+1, so the num_of_files variable name is misleading.
Why isn't n1 declared as
n1 = zeros(1023, 997, num_of_files+1);
or even better
n1 = zeros(size(BW));
?
Note that if you're putting 255 in the image, then n1 should be declared of type uint8:
n1 = zeros(size(BW), 'uint8');
RachelCahalane
on 8 Aug 2018
Accepted Answer
More Answers (0)
Categories
Find more on Region and Image Properties 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!