incorrect results from bwconncomp

I would like to understand how to correctly use the bwconncomp function, as the results I get do not make sense. Here is my problem. I have a binary image of edges like so:
It has been preprocessed to remove all vertices. I want to identify all the edges individually, so I ran the code:
CC1 = bwconncomp(E1);
It says there are only 13 objects, which looks highly suspect, so I pull up the first object like this:
%create cell array of edge matrices
numPixels1 = length(CC1.PixelIdxList);
edges1 = cell(num_1,1);
for ii = 1:num_1
pix = CC1.PixelIdxList{ii};
[rows,cols] = ind2sub(size(E1),pix);
r = max(rows)-min(rows)+1;
c = max(cols)-min(cols)+1;
mat = zeros(r,c);
rows = rows - min(rows)+1;
cols = cols - min(cols)+1;
inds = sub2ind([r,c],rows,cols);
mat(inds) = 1;
edges1{ii} = mat;
end
%show first element
figure;imshow(edges1{1})
And I get this:
I don't understand how counting connected components would actually connect those components before counting, it seems precisely in oppposition to the goal. Please help to clarify the use of this function.

3 Commenti

Matt J
Matt J il 14 Feb 2019
Modificato: Matt J il 14 Feb 2019
I get 149 objects. It would be advisable to post E1 in a .mat file, so that we know we are all working on the same image.
Sure thing! Just added it
aha. just noticed E1 is not a binary image after all, due to the preprocessing to remove vertices, which has resulted in some -1 entries. After correcting this so that E1 is correctly binary, I get 148 objects. Problem solved!

Accedi per commentare.

 Risposta accettata

Identify the branchpoints with bwmorph() then mask them out.
bpImage = bwmorph(mask, 'branchpoints'); % Find branchpoints in your binary image called "mask"
mask(bpImage) = false; % Erase those branchpoints.

2 Commenti

That worked! Previously I was finding the branchpoints, dilating them to ensure full disconnection, and then subtracting them, but this resulted in some values negative so after I realized this, I had to additionally set all negative values to 0. Your solution is cleaner. (Although I still dilate the mask to ensure all edges disconnected in the 8-connectivity sense.) Thanks!!
Yes, while logical variables CAN be treated as numbers 0 and 1, it's always best to use logical operations to avoid the problem you saw. Subtracting a true (1) from a false (0) will require them to be cast to double instead of logical so it can handle the -1. Then that causes weird side effects like you saw, along with not being able to use the array as a logical index anymore (like I did). So while in some circumstances you can use mathemetical operations, it's always safest to stick with logical operations when dealing with logical variables.
Thanks for accepting the answer.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Images in Centro assistenza e File Exchange

Prodotti

Release

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by