bwlabel changes the number of pixels in blobs
1 次查看(过去 30 天)
显示 更早的评论
Hey
I have a logical image containing 3 components as seen here:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1482961/image.png)
I then use the bwlabel function on the image and get the following image:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1482966/image.png)
I do not understand why bwlabel adds pixels to one of the components. I use bwlabel with the default settings.
0 个评论
采纳的回答
Image Analyst
2023-9-14
Your image is not a binary image. It's an RGB image. bwlabel returns a labeled image as a double, not a binary image as you showed in your second image. So I have no idea what your original image really was and what code you used to produce the labeled image. This is what I did:
rgbImage = imread('bwlabel.png');
class(rgbImage)
size(rgbImage)
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original RGB Image')
axis('on', 'image');
impixelinfo
if size(rgbImage, 3) == 3
fprintf('Converting RGB image to logical/binary image.\n')
rgbImage = logical(rgbImage(:, :, 2));
subplot(2, 2, 2);
imshow(rgbImage, []);
title('Now converted to gray scale Image')
impixelinfo
axis('on', 'image');
end
% Crop off the frame with a gray level of 240.
rgbImage = rgbImage(9:478, 13:834);
% Label the image.
[labeledImage, numBlobs] = bwlabel(rgbImage);
class(labeledImage)
fprintf('Found %d blobs in the binary image', numBlobs);
subplot(2, 2, 3);
imshow(labeledImage, []);
impixelinfo
title('Labeled RGB Image')
axis('on', 'image');
As you can see from my code above, the labeled image has no more non-zero pixels than your original image.
1 个评论
DGM
2023-9-14
I think it's important to note where exactly the distinction is occurring. When calling bwlabel() with any image that's not logical-class, it will be cast as logical before labeling. This is equivalent to
bw = bw ~= 0;
... which is rarely a good threshold value if you're trying to binarize an image.
The lesson? Make sure your image is satisfactorily binarized before feeding it to bwlabel(), bwconncomp(), etc. Otherwise, bwlabel() will make it binary with complete disregard for the content.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!