bwlabel (Bw) function return Wrong number of Connected Objects

4 views (last 30 days)
i use bwlabel(Bw) function to returns n, the number of connected objects found in Bw on the following image :
the n that will be returned should be 4 but actually 261 is return and i don`t know why
when i use the code below on the img simillar to the above but there is no rotation in any object it works fine
but in the case of the above image (there is a rotation in one or more object in the image) it not works correctlly
the code :
inputImg = input("one-rotated.jpg")
c = im2gray(inputImg);
figure , imshow(c) , title("gray Image");
z = 255-c;
q = imfill(z,'holes');
figure , imshow(q) , title("binary Image");
[l, n]=bwlabel(q);

Accepted Answer

DGM
DGM on 1 Jan 2022
Edited: DGM on 1 Jan 2022
Try this:
inputImg = imread('money.png'); % the file has to be read
c = rgb2gray(inputImg); % im2gray() is from where?
imshow(c) , title("gray Image");
%z = 255-c; % this just inverts the image
bw = c<250;
bw = bwareaopen(bw,100); % clean up speckles that get counted as objects
bw = imfill(bw,'holes');
imshow(bw) , title("binary Image");
[l, n] = bwlabel(bw);
n
n = 4

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!