Azzera filtri
Azzera filtri

finding endpoints of a label

1 visualizzazione (ultimi 30 giorni)
Mohammad Golam Kibria
Mohammad Golam Kibria il 26 Giu 2011
Hi, I have the following matrix. I =
0 0 0 0
0 1 1 1
0 0 0 0
0 0 0 0
0 0 1 0
0 0 1 0
0 0 1 0
[B,L,N,A]=bwboundaries(I,'noholes');
L =
0 0 0 0
0 1 1 1
0 0 0 0
0 0 0 0
0 0 2 0
0 0 2 0
0 0 2 0
idx=find(L==1) endpoints of label 1 are (2,2) and (2,4)
I need to find that endpoints similarly in case of label 2. Thanks endpoints of label 1 are (2,2) and (2,4)

Risposta accettata

Oleg Komarov
Oleg Komarov il 26 Giu 2011
I = [0 0 0 0
0 1 1 1
0 0 0 0
1 0 0 0
1 0 0 1
1 0 0 1
1 1 1 1];
[L,num] = bwlabel(I,4);
sz = size(I);
InEn = zeros(num,2);
for n = 1:num
% Find initial point of connected component
InEn(n,1) = find(L == n,1,'first');
l = InEn(n,1);
co = 1;
while true
% row and col subs
[r c] = ind2sub(sz,l(co)); % c = ceil(l/4); r = mod(l,4)+ c*sz(1)
% Calculate 4-connected subs
neigh(1:4,1:2) = [r+[0;-1;+1;0] c+[-1;0;0;1]];
% Convert to positions
idx = (neigh(:,2)-1)*sz(1) + neigh(:,1);
% Keep positions in the range of L
idx = idx(ismember(idx,1:prod(sz)),:);
% Move onto next 4-connected component
co = co+1;
idx = idx(~ismember(idx, l) & L(idx) == n);
% If empty then we reached the end
if idx
l(co) = idx;
else
break
end
end
% Assign end
InEn(n,2) = l(end);
end
  2 Commenti
Mohammad Golam Kibria
Mohammad Golam Kibria il 27 Giu 2011
This works good for this matrix.But if my matrix becomes as follows:
I = [0 0 0 0
0 1 1 1
0 0 0 0
1 1 1 1
1 1 0 1
1 1 1 0
1 1 1 0];
then it shows the following error?
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Oleg Komarov
Oleg Komarov il 27 Giu 2011
In this case the question is which ones are the endpoints?

Accedi per commentare.

Più risposte (2)

Walter Roberson
Walter Roberson il 26 Giu 2011
B = bwboundaries(I,'noholes');
xy_1_first = B{1}(1,:);
xy_1_last = B{1}(end,:);
xy_2_first = B{2}(1,:);
xy_2_last = B{2}(end,:);
  1 Commento
Mohammad Golam Kibria
Mohammad Golam Kibria il 26 Giu 2011
But in case :
I =
0 0 0 0
0 1 1 1
0 0 0 0
1 0 0 0
1 0 0 1
1 0 0 1
1 1 1 1
L =
0 0 0 0
0 2 2 2
0 0 0 0
1 0 0 0
1 0 0 1
1 0 0 1
1 1 1 1
It is not showing proper output,xy_1_first should be (4,1)
and xy_1_last should be (5,4) or vice versa.

Accedi per commentare.


Andrei Bobrov
Andrei Bobrov il 27 Giu 2011
L = bwlabel(I)
I2 = bwmorph(I,'endpoints')
epout = arrayfun(@(i1)find(I2 & L == i1)',1:max(L(:)),'un',0)
example:
I =
0 1 0 1 1 1
1 1 0 0 0 1
1 0 0 0 0 0
1 0 1 1 1 0
0 0 1 0 0 0
0 0 1 0 1 1
0 0 1 0 0 0
0 0 0 0 0 0
1 1 0 0 1 0
0 1 1 0 1 0
0 0 1 0 1 0
0 0 1 0 1 0
0 1 1 0 1 0
0 1 0 0 0 0
0 1 0 1 0 0
0 1 1 1 0 0
0 0 0 0 0 0
>> L = bwlabel(I)
I2 = bwmorph(I,'endpoints');
epout = arrayfun(@(i1)find(I2 & L == i1)',1:max(L(:)),'un',0);
L =
0 1 0 4 4 4
1 1 0 0 0 4
1 0 0 0 0 0
1 0 3 3 3 0
0 0 3 0 0 0
0 0 3 0 5 5
0 0 3 0 0 0
0 0 0 0 0 0
2 2 0 0 6 0
0 2 2 0 6 0
0 0 2 0 6 0
0 0 2 0 6 0
0 2 2 0 6 0
0 2 0 0 0 0
0 2 0 2 0 0
0 2 2 2 0 0
0 0 0 0 0 0
>> epout{5}
ans =
74 91
>>
  3 Commenti
Andrei Bobrov
Andrei Bobrov il 28 Giu 2011
I think you can
Mohammad Golam Kibria
Mohammad Golam Kibria il 28 Giu 2011
But where to change

Accedi per commentare.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by