finding region
2 views (last 30 days)
Show older comments
Hi I have a matrix as follows:
I=
1 1 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Here 1 has divided the matrix in two parts I need to have the indices of these two separate parts. say idx1 will be upper zeros and idx2 contains lower zero indices.Can any one help.
0 Comments
Accepted Answer
Andrei Bobrov
on 29 Jun 2011
BW2 = bwmorph(I,'diag');
L = bwlabel(~BW2);
Id1 = bwdist(I,'chessboard') == 1;
for i1 = 1:max(L(:))
L(bwdist(L == i1,'chessboard')==1 & Id1) = i1;
end
idx1 = find(max(L(1,:))==L);
idx2 = find(max(L(end,:))==L);
EDIT as at Sean
L = bwlabel(~I,4);
idx1 = find(max(L(1,:))==L);
idx2 = find(max(L(end,:))==L);
More Answers (2)
Sean de Wolski
on 29 Jun 2011
CC = bwconncomp(~I,4);
CC.PixelIdxList will contain the linear indices. To get the sub indices either use regionprops with the 'pixellist' option or ind2sub
1 Comment
Andrei Bobrov
on 29 Jun 2011
+1
Hi Sean! I stupid, forgotten about the variable 4-connectivity. Thanks
Paulo Silva
on 29 Jun 2011
Just for fun
I=[1 1 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0];
up=[];down=[];
for c=1:size(I,2)
d=0;u=1;
for r=1:size(I,1)
if(d==1)
down=[down sub2ind(size(I),r,c)];
end
if(I(r,c)==1)
d=1;u=0;
end
if u==1
up=[up sub2ind(size(I),r,c)];
end
end
end
disp('index of zeros bellow the 1')
down
disp('index of zeros above the 1')
up
%for big arrays it's better to pre-allocate the up and down vectors, much faster
See Also
Categories
Find more on Sparse Matrices 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!