Problem with my code, any help?

I am a begineer and I am trying to identify objects in a binary image, the same way as bwlabel does, but It didn ´t make exactly what I want
This is what I get: b =
0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 10 10 10 0 0
0 1 0 0 0 10 10 10 0 0
0 1 0 0 0 10 10 10 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 14 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
But, this i whhat I want:
b =
0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 2 2 2 0 0
0 1 0 0 0 2 2 2 0 0
0 1 0 0 0 2 2 2 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 3 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
a=zeros(10);
a(2:4,6:8)=1;
a(7,8)=1;
a(2:5,2)=1;
[x,y]=size(a);
[x1,x2]=find(a==1);
[x11,x22]=size(x1);
b=zeros(x,y);
z=[x1 x2];
T=0;
conta=1;
i=1;
j=1;
while i<=x11
for ii=z(i,1)-1:z(i,1)+1
for jj=z(i,2)-1:z(i,2)+1
if a(ii,jj)==1
b(ii,jj)=conta;
end
if i>2
if abs(z(i,1)-z(i-1,j))> 2 || abs(z(i,2)-z(i-1,2))> 3
conta=conta+1;
end
end
end
end
i=i+1;
end
Any help would be aprecciated!

Respuestas (1)

Sean de Wolski
Sean de Wolski el 15 de Oct. de 2014

0 votos

b = bwlabel(logical(a))

3 comentarios

Joseph
Joseph el 15 de Oct. de 2014
Yes, but my aim is without using the function!
Sean de Wolski
Sean de Wolski el 15 de Oct. de 2014
Editada: Sean de Wolski el 15 de Oct. de 2014
oh, in that case:
b = labelmatrix(bwconncomp(a))
Why reinvent the wheel?
Sean de Wolski
Sean de Wolski el 15 de Oct. de 2014
Editada: Sean de Wolski el 15 de Oct. de 2014
Or if you're just trying to avoid a toolbox dependency:
[~,~,idx] = unique(yourb)
b = reshape(idx - 1,size(yourb))

La pregunta está cerrada.

Preguntada:

el 15 de Oct. de 2014

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by