Know which branchpoints are connected to other branchpoints?

2 次查看(过去 30 天)
If I get a skeleton of a binary image and return the branchpoints I want to know which ones are connected to which. eg if I have 5 branchpoints and branchpoint 2 is connected to branchpoint 4, how would I know this?

回答(1 个)

DGM
DGM 2024-10-12
There are probably smarter ways to do it, but:
% a logical image
inpict = imread('hands1-mask.png');
% the skeleton
sk = bwskel(inpict);
imshow(sk)
% i'm just going to consider all nodes
% i.e. both branchpoints and endpoints
bp = bwmorph(sk,'branchpoints');
ep = bwmorph(sk,'endpoints');
N = bp | ep;
% dilate to make sure 8-connectivity is broken
N = bwmorph(N,'dilate');
% the edges alone
E = sk & ~N;
% form label arrays
[Le numedges] = bwlabel(E,8); % for addressing
[Ln numnodes] = bwlabel(N,8); % for sampling
% find connectivity list
connectednodes = zeros(numedges,2);
for ke = 1:numedges
% create a mask and sample the node label array
thismk = bwareafilt((Le == ke) | N,1);
connectednodes(ke,:) = nonzeros(unique(Ln(thismk)));
end
% summarize
edgelist = (1:numedges).';
nodelist = (1:numnodes).';
table(edgelist,connectednodes)
ans = 9x2 table
edgelist connectednodes ________ ______________ 1 1 7 2 2 5 3 3 5 4 4 6 5 5 6 6 6 7 7 7 8 8 8 10 9 8 9
I'm sure any other information can be figured out from there.
% we can still figure out what the neighbors are
neighbornodes = cell(numnodes,1);
for k = 1:numnodes
mk = connectednodes == k;
mk = any(mk,2) & ~mk; % exclude self-connection
neighbornodes{k} = connectednodes(mk).';
end
table(nodelist,neighbornodes)
ans = 10x2 table
nodelist neighbornodes ________ _____________ 1 {[ 7]} 2 {[ 5]} 3 {[ 5]} 4 {[ 6]} 5 {[ 2 3 6]} 6 {[ 4 5 7]} 7 {[ 1 6 8]} 8 {[7 10 9]} 9 {[ 8]} 10 {[ 8]}
This can probably be broken if nodes are too close to each other (<3px or so). At that point, the dilation eliminates the interconnecting edge.

Community Treasure Hunt

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

Start Hunting!

Translated by