How to isolate and display the largest connected component
Ältere Kommentare anzeigen
Hello all,
I am looking to isolate the largest connected component in an image, and then display it by itself. Right now, the code I am using deletes the largest connected component and keeps everything else. I want everything else in the image to be deleted, and the largest component to remain. My code for the isolation is as follows:
CC = bwconncomp(B);
numOfPixels = cellfun(@numel,CC.PixelIdxList);
[unused,indexOfMax] = max(numOfPixels);
B(CC.PixelIdxList{indexOfMax}) = 0;
imshow(B);
2 Kommentare
Jeremy Jernigen
am 27 Jun. 2017
CC = bwconncomp(B);
numOfPixels = cellfun(@numel,CC.PixelIdxList);
[unused,indexOfMax] = max(numOfPixels);
biggest = zeros(size(B));
biggest(CC.PixelIdxList{indexOfMax}) = 1;
imshow(biggest);
Image Analyst
am 27 Jun. 2017
With recent versions of MATLAB, you simply do
biggest = bwareafilt(B);
instead of the 6 lines of code you showed.
Akzeptierte Antwort
Weitere Antworten (3)
Fazal Hassan
am 20 Mär. 2014
0 Stimmen
Sir i have used above code on CT scan of heart in png format .but it give me wrong largest blog area when i change threshod it give an other blob with largest area...in image it should give me AROTA as largest blob but it give me some thing else Need your help plz...why this code is behaving like that Thanks in advance. Engr fazalhassan
2 Kommentare
Image Analyst
am 20 Mär. 2014
Post your image so I can try my method on it.
Neeru
am 8 Mai 2014
Is it possible to use a similar code on a 3D mesh, to find the largest connected component of the mesh? I have two CT scans of the skull before and after a piece of the bone has been removed. I have subtracted them and meshed the result, and I want to extract only the largest component. Thanks, Neeru.
MoonPie1
am 10 Dez. 2015
0 Stimmen
How can i display 4 largest connected component?
10 Kommentare
Image Analyst
am 10 Dez. 2015
Use bwareafilt():
biggest4 = bwareafilt(binaryImage, 4, 'largest');
imshow(biggest4);
MoonPie1
am 10 Dez. 2015
i am unable to use bwareafilt as it is a medical image... please let me know how to delete 30 elements out of 34 and display only 4 largest
Image Analyst
am 10 Dez. 2015
bwareafilt() does not care if the binary image came from a medical image or not. What is the real reason you are unable to use it? Do you have an antique version of MATLAB that did not have it yet?
ezhil K
am 21 Jan. 2019
I have implemented the same.But,I dont get output.It displays error message.What should I do to rectify it?
I get the bellow error messages.
Error using bwpropfilt
Expected input number 1, BW, to be two-dimensional.
Error in bwpropfilt>parse_inputs (line 121)
validateattributes(bw, {'logical'}, {'nonsparse', '2d'}, mfilename, 'BW', 1);
Error in bwpropfilt (line 58)
[bw, I, attrib, p, direction, conn] = parse_inputs(args{:});
Error in bwareafilt (line 34)
bw2 = bwpropfilt(bw, 'area', p, direction, conn);
Error in test (line 3)
biggest4 = bwareafilt(binaryImage, 4, 'largest');
img=imread('test.png');
binaryImage=imbinarize(img);
biggest4 = bwareafilt(binaryImage, 4, 'largest');
imshow(biggest4);

Image Analyst
am 21 Jan. 2019
Your binary image was color. Convert it to binary. This works:
img=imread('test.png');
subplot(2, 1, 1);
imshow(img);
if ndims(img) > 1
img = img(:,:,1); % Take red plane
end
binaryImage = imbinarize(img);
% Get rid of anything touching the edge of the image
% because this will definitely not be a license plate.
binaryImage = imclearborder(binaryImage);
% Extract the biggest 4 blobs
biggest4 = bwareafilt(binaryImage, 4, 'largest');
subplot(2, 1, 2);
imshow(biggest4);

ezhil K
am 21 Jan. 2019
This code works only if I specify it in a seperate script file.But if I use it as a function this returns the following error.Why is it so?
Error using imbinarize
Expected I to be one of these types:
uint8, uint16, uint32, int8, int16, int32, single, double
Instead its type was logical.
Error in imbinarize>validateImage (line 261)
validateattributes(I,supportedClasses,supportedAttribs,mfilename,'I');
Error in imbinarize>parseInputs (line 197)
validateImage(I);
Error in imbinarize (line 133)
[I,isNumericThreshold,options] = parseInputs(I,varargin{:});
Error in Shi_tomashi (line 80)
binaryImage = imbinarize(c);
Image Analyst
am 21 Jan. 2019
I don't know what c really means, but your c is a binary (logical) image, not a gray scale image. It makes no sense at all to call imbinarize() on an image that is already binary, don't you agree?
ezhil K
am 22 Jan. 2019
Yeah,it's my mistake.Thanks for your clarification.I got the output.
ezhil K
am 22 Jan. 2019
I have to crop the plate region automatically.So what should I do to make it possible?
Sultan
am 12 Jul. 2017
0 Stimmen
Hello all,
I am looking to mark the largest connected component in an image with special color say 'red', and then color the other components in black while the empty space I wanted to be colored in white. cheers
1 Kommentar
Image Analyst
am 12 Jul. 2017
Use regionprops to find the largest blob. Then setup a colormap where the row with the largest blob number is red, row 1 is white, and all the other rows are black, then use labeled2rgb() to apply that colormap and produce an RGB image.
[labeledImage, numBlobs] = bwlabel(binaryImage);
props = regionprops(labeledImage, 'Area');
[maxArea, largestIndex] = max([props.Area])
cmap = zeros(numBlobs+1, 3);
cmap(largestIndex, :) = [1, 0, 0];
cmap(1, :) = 0;
rgbImage = labeled2rgb(labeledImage, cmap);
Start your own question and upload your binary image if you can't figure it out.
Kategorien
Mehr zu Image Processing Toolbox finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!