finding region

8 visualizzazioni (ultimi 30 giorni)
Mohammad Golam Kibria
Mohammad Golam Kibria il 29 Giu 2011
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.

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 29 Giu 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);

Più risposte (2)

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

Accedi per commentare.


Paulo Silva
Paulo Silva il 29 Giu 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
  1 Commento
Mohammad Golam Kibria
Mohammad Golam Kibria il 30 Giu 2011
Thanks. Its also ok.How to pre-allocate the up and down vectors?

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by