finding region
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Andrei Bobrov
2011 年 6 月 29 日
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);
その他の回答 (2 件)
Sean de Wolski
2011 年 6 月 29 日
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 件のコメント
Andrei Bobrov
2011 年 6 月 29 日
+1
Hi Sean! I stupid, forgotten about the variable 4-connectivity. Thanks
Paulo Silva
2011 年 6 月 29 日
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
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!