Is there any way to check whether two pixels in a binary image are connected, if connected, it will return TRUE else FALSE.....???
8 views (last 30 days)
Show older comments
Mohammad Bhat
on 23 Nov 2017
Commented: Mohammad Bhat
on 21 Jul 2018
Based on connectivity or whether two pixels in another image ( Image 1 ) are connected, I have to draw line between those pixels in another image (Image 2).? Remember Image 2 carries interest points (branch,end,start, and another important points) of image 1.Any suggestion..?
0 Comments
Accepted Answer
Image Analyst
on 19 Jul 2018
If you simply want to see if they're connected, use bwlabel() to do connected component labelling on them, and then check for the same label value (meaning they're part of the same connected region):
[labeledImage, numberOfRegions] = bwlabel(binaryImage);
if labeledImage(row1, col1) == labeledImage(row2, col2)
% They're connected.
end
Calling bwdistgeodesic() or regionprops() will give you the pixels connecting them.
bwdistgeodesic() will give you the shortest path. See Steve's blog: http://blogs.mathworks.com/steve/2011/11/01/exploring-shortest-paths-part-1/.
Whereas regionprops() (or find() after using ismember() to extract the region you want) will give you ALL the pixels in the region.
2 Comments
More Answers (2)
Walter Roberson
on 23 Nov 2017
You can use the (BW, R, C) form, where R and C are row and column of one of the two endpoints to be queried. Index the result of the function at the other endpoint. If the result there is infinity then there is no connection.
5 Comments
Walter Roberson
on 23 Nov 2017
L = imclearborder(im2bw(imread('lllllll.jpg')));
H = imclearborder(im2bw(imread('hhhhh.jpg')));
Lpos = find(L);
HLab = bwlabel(H);
LLab = HLab(Lpos);
[LRow, LCol] = ind2sub(size(L), Lpos);
LLab is the same for all pixels that were originally part of the same component.
groups = accumarray(LLab, (1:length(LLab)).', [], @(x) {x});
now groups is a cell array of groupings. The values in any one groups{K} are indices into Lpos, the linear indices of the representative pixels in L. Perhaps you might prefer
groupsLidx = accumarray(LLab, Lpos, [], @(x) {x});
in which for any given groupsLidx{K} all of the members are linear indices of individual pixels.
Or perhaps,
LLimg = zeros(size(HLab));
LLimg(Lpos) = HLab(Lpos);
and now LLimg(R1, C1) and LLimg(R2, C2) are part of the same original component in H if they have the same value.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!