finding endpoints of a label

5 ビュー (過去 30 日間)
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 26 日
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)

採用された回答

Oleg Komarov
Oleg Komarov 2011 年 6 月 26 日
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 件のコメント
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 27 日
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 2011 年 6 月 27 日
In this case the question is which ones are the endpoints?

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 6 月 26 日
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 件のコメント
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 26 日
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.

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2011 年 6 月 27 日
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 件のコメント
Andrei Bobrov
Andrei Bobrov 2011 年 6 月 28 日
I think you can
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 28 日
But where to change

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by