how to select the object with the largest area?

徘徊边缘 提交于 2019-12-05 08:06:59

use Area and PixelIdxList in regionprops, this means to edit the to the following line:

stat = regionprops(Ilabel,'Centroid','Area','PixelIdxList');

The maximum area and it's struct index is given by

[maxValue,index] = max([stat.Area]);

The linear index of pixels of each area is given by `stat.PixelIdxList', you can use them to delete that given area (I assume this means to assign zeros to it)

YourImage(stat(index).PixelIdxList)=0;

For Neglecting all others except one..
Similar to the above answer. But instead of deleting the large region, I delete all regions except large one.

stat=regionprops(label,'Centroid','Area','PixelIdxList');
[maxValue,index] = max([stat.Area]);
[rw col]=size(stat);
for i=1:rw
    if (i~=index)
       BW(stat(i).PixelIdxList)=0; % Remove all small regions except large area index
    end
end
figure,imshow(BW);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!