Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

An object with the biggest amount of scope in the Binary image

Asked by Felix on 12 Sep 2013

Hello, I have a binary image with n white objects (not connected). How can I leave only the object with the biggest amount of scope in the picture I can do it by - bwareaopen, but in this way I have to know approximate size of an object. sorry for my english. thnx



3 Answers

Answer by Image Analyst on 13 Sep 2013
Accepted answer

Try this:

grayImage = imread('coins.png');
binaryImage = grayImage > 100;
binaryImage = imfill(binaryImage, 'holes'); % Fill holes.
[labeledImage, numObjects] = bwlabel(binaryImage);
stats = regionprops(labeledImage,'Area');
allAreas = [stats.Area]
[~, indexOfLargest] = max(allAreas);
largestBlob = ismember(labeledImage, indexOfLargest)>0;
keeperIndexes = 1:numObjects;
keeperIndexes(indexOfLargest) = [];
binaryImage2 = ismember(labeledImage, keeperIndexes)>0;
title('All Except Largest');

1 Comment

Felix on 17 Sep 2013

thank you! it's good!

Image Analyst
Answer by Adam Filion on 12 Sep 2013
Edited by Adam Filion on 12 Sep 2013

If you have Image Processing Toolbox you can use the function regionprops. It comes out to something like this, where tm is the binary image.

cc = bwconncomp(tm);
stats = regionprops(cc,'Area');
A = [stats.Area];
[~,biggest] = max(A);
tm(labelmatrix(cc)~=biggest) = 0;

You can watch a recorded presentation that steps through a similar example here:


Felix on 12 Sep 2013

Thanks for the help, it close enough to what I asked, however, it erased all the small arias except the closest ones to the object . can you tall me why is that? sorry i can't upload an image, can i send it to your e-mail?

Image Analyst on 13 Sep 2013

Try uploading an image to or

Adam Filion
Answer by Sean de Wolski on 17 Sep 2013

Good timing, I just posted this function a few minutes ago!



Sean de Wolski

Contact us