bwlabel changing connectivity of image

I am trying to use BWLabel to label 1 pixel width line segments. These lines are not straight so I am using a connectivity of 8. When I run bwlabel and view the image of the newly labeled skeleton, it actually is changing layout, size and connectivity of the line segments. The input image is a double. I am using Matlab R2009b.
Am I asking bwlabel to be too sensitive? I am using this as part of a larger code that is trying to find the angle of these segments by using the segment endpoints, so maintaining separation is important.
Below is the code I used to generate the two supplied images
% A = image to be labeled.
figure(1), imshow(A)
A2 = bwlabel(A,8);
figure(2), imshow(A2)

 Accepted Answer

Your A image is not what you think it is. I'd bet that it is not a logical image, but a gray scale image with most pixels at 255 but some pixels at a much darker value. Do this
imhist(A);
and see what it shows. Do you have only 2 bins at 0 and 1? Or do you have bins at 0, 255, and, say, 20 or some small number?
You can also try this
A = (A == 255); % Convert to logical image with 1 only where A = 255.
and then label and see what you get.

5 Comments

I will have to double check tomorrow to be sure, but the image should be logical. It's the result of a skeletonization process using bwmorph commands. I do perform a subtraction (I subtract dilated 'branchpoints' from the 'skeleton' to create the individual line segments), but I am assuming since both were performed with bwmorph that they are still binary images.
Aha! That's your problem. The difference of two logicals is a double. Run this code a few times:
% Make two logical arrays with true in random locations.
m1 = logical(randi(2, [6 6])-1)
m2 = logical(randi(2, [6 6])-1)
diffm = m1 - m2 % This is a double
class(diffm)
% Display it
imshow(diffm);
% Since diffm is a double, imshow will assume diffm
% goes between 0 and 0. The 1's will display as 255.
% Anything from -1 to 0 will display as 0.
% Now label.
bwlabel(diffm)
% Any non-zero things will label including -1.
What is happening is that you are getting a difference array with values -1, 0, and 1. The -1 and 1 will get labeled and show up after labeling. However the -1 won't show up when called with imshow.
To check, call imshow with [] like this:
imshow(A, []);
This will show -1 as 0, 0 as 128, and +1 as 255 since it will scale the whole range of -1 to +1 to the range of 0-255. Once you understand that, all will be clear for you.
Ahhhh, thank you very much. You and Walter Roberson have taught me a lot. I have not had any training in image processing and am learning this on the fly, mostly by reading posts on here. I picked Matlab over other languages because of the image processing toolbox and the user community. Good stuff.
I also utilize 'help ....' all the time.
You were correct. I ran the 'unique' function that Stephen suggested and got [-1 0 1], so I just ran the im2bw command with a high threshold value and it solved the issue.

Sign in to comment.

More Answers (1)

Stephen
Stephen on 31 May 2012
I agree with Image Analyst. Is your image a jpg or has it been compressed? If so, there are little ghosts that lurk as low intensity values on certain pixels. bwlabel uses any value above 0 (or below?) so those sneaky pixels will get marked. Take a look at what a==0 looks like, or type unique(a) to see a list of the unique intensity values.

Categories

Find more on Images 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!