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!

답변 (1개)

Sean de Wolski
Sean de Wolski 2014년 10월 15일

0 개 추천

b = bwlabel(logical(a))

댓글 수: 3

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

이 질문은 마감되었습니다.

질문:

2014년 10월 15일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by