finding region

조회 수: 8 (최근 30일)
Mohammad Golam Kibria
Mohammad Golam Kibria 2011년 6월 29일
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.

채택된 답변

Andrei Bobrov
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);
  댓글 수: 1
Mohammad Golam Kibria
Mohammad Golam Kibria 2011년 6월 30일
Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Sean de Wolski
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
Andrei Bobrov 2011년 6월 29일
+1
Hi Sean! I stupid, forgotten about the variable 4-connectivity. Thanks

댓글을 달려면 로그인하십시오.


Paulo Silva
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
  댓글 수: 1
Mohammad Golam Kibria
Mohammad Golam Kibria 2011년 6월 30일
Thanks. Its also ok.How to pre-allocate the up and down vectors?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by