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

binary imageimage processingImage Processing Toolboxregionprops

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

Best Answer

Try this:
grayImage = imread('coins.png');
subplot(2,2,1);
imshow(grayImage);
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;
subplot(2,2,2);
imshow(largestBlob);
title('Largest');
keeperIndexes = 1:numObjects;
keeperIndexes(indexOfLargest) = [];
binaryImage2 = ismember(labeledImage, keeperIndexes)>0;
subplot(2,2,3);
imshow(binaryImage2);
title('All Except Largest');