"vislabels" detecting background as an object
1 view (last 30 days)
Show older comments
Abdul Hannan Qureshi
on 18 Apr 2022
Commented: Image Analyst
on 18 Apr 2022
I want to detect number of lines in the image below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/967990/image.png)
However, I am getting following output:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/967995/image.png)
Why it is picking background as object count. Can someone advise me what I am missing?.
"bwconncomp" outcome is shown below.
cc =
struct with fields:
Connectivity: 8
ImageSize: [5023 5036]
NumObjects: 9
PixelIdxList: {1×9 cell}
I am using following simple code:
a = imread ('Image.png');
b = rgb2gray(a);
d=im2bw(b);
vislabels(d);
cc=bwconncomp(d)
0 Comments
Accepted Answer
Image Analyst
on 18 Apr 2022
You need to call imclearborder since your image has a white frame surrounding the black background.
rgbImage = imread ('Image.png');
grayImage = rgbImage(:,:,1); % Take red channel (faster than rgb2gray).
binaryImage = grayImage > 128;
% Get rid of white frame around the lines.
binaryImage = imclearborder(binaryImage);
imshow(binaryImage)
% Count lines
[labeledImage, numLines] = bwlabel(binaryImage);
caption = sprintf('Found %d lines.', numLines);
title(caption, 'FontSize',16)
2 Comments
Image Analyst
on 18 Apr 2022
You're welcome. Can you then click the "Accept this answer" link to let everyone know it's resolved? Thanks in advance. 🙂
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!