How can I extract the largest blob in a binary image
Show older comments
How can I extract the largest blob in a binary image?
Accepted Answer
More Answers (2)
Sean de Wolski
on 1 May 2013
Here's the function I wrote to do this. You can also use the example in the "tips section of the doc for regionprops which explains how to keep blobs based on some criteria.
function Imx = keepMaxObj(X)
%Function to keep only the maximum sized (biggest) object in an image
%SCd 11/30/2010
%
%Updates:
% -02/03/2011: Added ability to handle an image directly
%
%Usage:
% Imx = keepMaxObj(CC);
% Imx = keepMaxObj(V);
%
%Input Arguments:
% -CC: Connected components returned from bwconncomp
% -V: Logical image with parts you want true
%
%Output Arguments:
% -Imx: Logical volume with only the biggest object left true.
%
%See Also: bwconncomp
%
%Error checking:
assert(islogical(X)||isstruct(X),'The first input argument is expected to be a struct or a logical');
if isstruct(X)
CC = X;
parts = {'PixelIdxList','ImageSize'};
assert(all(ismember(parts,fieldnames(CC))),'CC is expected to be the output from bwconncomp');
else
CC = bwconncomp(X);
end
clear X;
%Preallocate and find number of voxels/object
Nvox = zeros(CC.NumObjects,1);
for ii = 1:CC.NumObjects
Nvox(ii) = numel(CC.PixelIdxList{ii});
end
%Find the biggest object's index, warn and save all if there are multiples
[mx,midx] = max(Nvox);
more_than1_max = sum(mx==Nvox);
if more_than1_max > 1
midx = find(mx == Nvox);
warning('Multiple:Maxima', 'There were %i objects with the maximum size.\n They are all left on!',more_than1_max);
end
%Create the final image
Imx = false(CC.ImageSize);
Imx([CC.PixelIdxList{midx}]) = true;
end
7 Comments
leon
on 1 May 2013
Sean de Wolski
on 1 May 2013
Save this code to a file called "keepMaxObj.m". Then call it with whatever the variable name of your image is:
Imx = keepMaxObj(V)
Where V is whatever you've called your image.
leon
on 1 May 2013
Walter Roberson
on 1 May 2013
When you ask to overlap them, do you mean "in the display", or do you mean that you want to create a new array that has the data for the ellipse written into it as well as the other data ?
Image Analyst
on 1 May 2013
Both Sean's function, and the one I gave you (binaryImage = ExtractNLargestBlobs(binaryImage, numberToExtract)) expect the input to be a binary image, meaning a logical (true or false) image. If all you have is the boundary coordinates of your ellipse, then you'd use poly2mask() to create the binary image. How did you get that ellipse you posted above?
leon
on 1 May 2013
Image Analyst
on 2 May 2013
What form is your ellipse in? It looks like an RGB image with some white pixels and some blue pixels - at least the image you posted in the comment to my answer does. Why do you want that blue and white image burned into the image? You can easily do it (but I don't know why you'd want to):
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Extract the individual red, green, and blue color channels
% of the ellipse image.
redEllipseChannel = rgbEllipseImage(:, :, 1);
greenEllipseChannel = rgbEllipseImage(:, :, 2);
blueEllipseChannel = rgbEllipseImage(:, :, 3);
% Get mask
mask = redEllipseChannel > 0 & greenEllipseChannel > 0 & blueEllipseChannel > 0;
% Assign ellipse pixels in the masked region of the ellipse image
% to the original image in the masked region
redChannel(mask) = redEllipseChannel(mask);
greenChannel(mask) = greenEllipseChannel (mask);
blueChannel(mask) = blueEllipseChannel(mask);
% Recombine into RGB image
newRGBimage = cat(3, redChannel, greenChannel, blueChannel);
% Display
imshow(newRGBimage);
Henry Brown
on 5 Jun 2019
0 votes
I came across the bwpropfilt function, which serves this purpose in one line:
BW2 = bwpropfilt(BW, 'Area', n);
BW2 is the new binary image with only n largest objects in BW.
1 Comment
Image Analyst
on 6 Jun 2019
There is a new function to extract only the largest blob -- it's called bwareafilt()
BW = bwareafilt(BW, 1); % Extract largest blob.
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!