How to display the two biggest blobs in terms of area?

1 回表示 (過去 30 日間)
sanyukta
sanyukta 2019 年 7 月 22 日
コメント済み: Image Analyst 2019 年 7 月 23 日
I have a binary image 'i' which I have attached here. From this image, I want to find the two biggest blobs in terms of area. When I tried this code, I am able to find the biggest blob which I have attached and the next biggest blob is removed.
labeledImage=bwlabel(i);
measurements=regionprops(labeledImage,'Area');
allAreas=[measurements.Area];
[biggestArea, indexOfBiggest]=sort(allAreas,'descend')
biggestBlob=ismember(labeledImage,indexOfBiggest(1));
biggestBlob=biggestBlob>0;
figure,imshow(biggestBlob,[]);
I tried to use the bwareafilt() but MATLAB R2013a does not support it. Kindly help me to display the next biggest blob.
Thanking you.
  1 件のコメント
sanyukta
sanyukta 2019 年 7 月 23 日
Thank you so much..It worked..

サインインしてコメントする。

採用された回答

David K.
David K. 2019 年 7 月 22 日
In the line
biggestBlob=ismember(labeledImage,indexOfBiggest(1));
you are finding the biggest by looking at the first index in a sorted array. So to get the second biggest you do
secondBlob=ismember(labeledImage,indexOfBiggest(2));
Then you can combine them simply by doing
twoBiggest = biggestBlob | secondBlob;
This is because both blobs are binary - 1s and 0s - so by doing an OR operation it makes every white part in both images seen in both images.
  1 件のコメント
Image Analyst
Image Analyst 2019 年 7 月 23 日
I think
twoBiggest = ismember(labeledImage, indexOfBiggest(1:2));
would be simpler. It gets you both of the two biggest blobs all in one line of code.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2019 年 7 月 22 日
It'w very bad practice to call your image i (the imaginary variable). Call it binaryImage, mask, or BW instead. You might want to call imclearborder() to get rid of that outer blob, unless you want it.
mask = imclearborder(mask);
I don't believe you need to use
biggestBlob=biggestBlob>0;
because ismember() will return a logical image already.
Other than that, David's solution should work.
  1 件のコメント
sanyukta
sanyukta 2019 年 7 月 23 日
Thank you so much for this valuable suggestion.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品


リリース

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by