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

2 次查看(过去 30 天)
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


Image Analyst
Image Analyst 2013-9-13
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');

更多回答(2 个)

Adam Filion
Adam Filion 2013-9-12
编辑:Adam Filion 2013-9-12
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:
  2 个评论


Sean de Wolski
Sean de Wolski 2013-9-17
Good timing, I just posted this function a few minutes ago!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by