how to select the object with the largest area?
Asked Answered
A

2

6

I have used bwconvhull to detect a certain part of the image, as you can see in the image, there are number of objects with certain centroids. What I want to do is to detect the object with the largest area (1st big one from the left) and neglect the others.

Which method should I follow?

I will be very thankful for your help. The following is the code (it's very roughly written as I am still working on it. Sorry for any inconvenience in advance)

CH_objects = bwconvhull(c,'objects');
imshow(CH_objects);
title('Objects Convex Hull');
bwarea(CH_objects)
Ilabel = bwlabel(CH_objects,8);
stat = regionprops(Ilabel,'centroid');
imshow(CH_objects);
hold on;
for x = 1:numel(stat)
    plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro');
end

Here is the image.

Alyciaalyda answered 22/12, 2013 at 2:22 Comment(0)
Q
11

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;
Quadrat answered 22/12, 2013 at 3:49 Comment(0)
P
2

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);
Pectoralis answered 22/12, 2013 at 2:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.