Joining label

1 次查看(过去 30 天)
Mohammad Golam Kibria
Hi I have the following matrix
I =
0 1 0 0 1 0
0 0 0 1 0 0
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
and
L=bwlabel(I)
L =
0 2 0 0 3 0
0 0 0 3 0 0
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
I need to join or merge those label who have minimum distance 2. Is there any one to help?
  1 个评论
Mohammad Golam Kibria
label 1 and label 3 are in minimum distance 2 but label 2 is not within minimum distance 2 with any of label 1 or 3.
the out put might be like bellow:
0 2 0 0 1 0
0 0 0 1 0 0
0 0 0 1 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1

请先登录,再进行评论。

回答(3 个)

Jan
Jan 2011-6-19
To join the regions, exapand them at first. I assume "minimum distance of 2" means a dilation with a 3x3 matrix:
I = [0 1 0 0 1 0; ...
0 0 0 1 0 0; ...
0 0 0 0 0 0; ...
0 0 1 1 0 0; ...
0 1 0 0 1 0; ...
1 0 0 0 0 1];
I2 = imdilate(I, ones(3, 3));
L = bwlabel(I2);
% Now remove all points, which are 0 in the original matrix:
L(I == 0) = 0;

the cyclist
the cyclist 2011-6-19
It is not clear to me what you mean by "join or merge". Perhaps you can tell us what the correct output is for the example you have given?
The find() command might be part of what you need. For example,
>> [i,j] = find(L>=2);
will give you the (i,j) coordinates of the values of L greater than or equal to 2.

Andrei Bobrov
Andrei Bobrov 2011-6-19
C = nchoosek(1:max(L(:)),2);
[ii jj c] = arrayfun(@(i1)find(bwdist(L==C(i1,1)).*(L==C(i1,2))),1:size(C,1),'un',0);
leq = C(cellfun(@(x)min(x),c)<=2,:);
for jj = 1:size(leq,1),
leq(ismember(leq(:,1),leq(jj,2)),1) = leq(jj,1);
L(L==leq(jj,2)) = leq(jj,1);
end
CORRECTED
I1 = I;
L = bwlabel(I1);
for ii = max(L(:)):-1:1
c{ii} = bwdist(L==ii)==1;
end
I1(sum(cat(3,c{:}),3) > 1)=1
Lnew = bwlabel(I1)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by