Number of Object in binary image

10 ビュー (過去 30 日間)
Sadeq Ebrahimi
Sadeq Ebrahimi 2019 年 12 月 15 日
コメント済み: Sadeq Ebrahimi 2019 年 12 月 15 日
Hi, im trying to get the number of objects in an binary image but it return the wrong answer (it should be 6 but it return 31). here is the code and the image:
I=imread('2.jpg');
I_BW=im2bw(I,0.87);
I1=imcomplement(I_BW);
[L,n]=bwlabel(I1);
bw.PNG

採用された回答

Image Analyst
Image Analyst 2019 年 12 月 15 日
Try this:
bw = imread('bw.png');
if ndims(bw) > 1
bw = bw(:,:,1);
end
% Threshold
bw = bw > 128;
subplot(1, 2, 1);
imshow(bw, []);
% Fill holes
bw = imfill(bw, 'holes');
% Get areas
props = regionprops(bw, 'Area');
sortedAreas = sort([props.Area], 'descend')
% Looks like we need to throw away any blobs less than 1000 pixels or so
bw = bwareaopen(bw, 1000);
subplot(1, 2, 2);
imshow(bw);
% Count the blobs
[labeledImage, numBlobs] = bwlabel(bw);
fprintf('Found %d shapes.\n', numBlobs);
  1 件のコメント
Sadeq Ebrahimi
Sadeq Ebrahimi 2019 年 12 月 15 日
Thank you very much!

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

その他の回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 12 月 15 日
編集済み: KALYAN ACHARJYA 2019 年 12 月 15 日
Yes, the correct answer is 31, not 6. Actually it counts the all white pixels, blobs (having 1 white pixel with sourounding by black pixels also), there are so many blobs in 4th objects.
If you are looking for 6 answer only, then you have to remove all those tiny small blobs and apply same (using morphological operation, imdilate or imerode).
May it helps!
  1 件のコメント
Sadeq Ebrahimi
Sadeq Ebrahimi 2019 年 12 月 15 日
Thanks a lot! it was very helpful.

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

Community Treasure Hunt

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

Start Hunting!

Translated by