Joining label
Show older comments
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
on 20 Jun 2011
Answers (3)
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
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
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)
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!