How to isolate and display the largest connected component
이전 댓글 표시
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
Jeremy Jernigen
2017년 6월 27일
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
2017년 6월 27일
With recent versions of MATLAB, you simply do
biggest = bwareafilt(B);
instead of the 6 lines of code you showed.
채택된 답변
추가 답변 (3개)
Fazal Hassan
2014년 3월 20일
0 개 추천
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
Image Analyst
2014년 3월 20일
Post your image so I can try my method on it.
Neeru
2014년 5월 8일
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
2015년 12월 10일
0 개 추천
How can i display 4 largest connected component?
댓글 수: 10
Image Analyst
2015년 12월 10일
Use bwareafilt():
biggest4 = bwareafilt(binaryImage, 4, 'largest');
imshow(biggest4);
MoonPie1
2015년 12월 10일
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
2015년 12월 10일
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
2019년 1월 21일
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
2019년 1월 21일
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
2019년 1월 21일
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
2019년 1월 21일
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
2019년 1월 22일
Yeah,it's my mistake.Thanks for your clarification.I got the output.
ezhil K
2019년 1월 22일
I have to crop the plate region automatically.So what should I do to make it possible?
Sultan
2017년 7월 12일
0 개 추천
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
Image Analyst
2017년 7월 12일
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.
카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!