Joining label

1 view (last 30 days)
Mohammad Golam Kibria
Mohammad Golam Kibria on 19 Jun 2011
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 Comment
Mohammad Golam Kibria
Mohammad Golam Kibria on 20 Jun 2011
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

Sign in to comment.

Answers (3)

Jan
Jan on 19 Jun 2011
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 on 19 Jun 2011
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 on 19 Jun 2011
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)

Tags

Community Treasure Hunt

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

Start Hunting!