Info

This question is closed. Reopen it to edit or answer.

Problem with my code, any help?

1 view (last 30 days)
Joseph
Joseph on 15 Oct 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
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!

Answers (1)

Sean de Wolski
Sean de Wolski on 15 Oct 2014
b = bwlabel(logical(a))
  3 Comments
Sean de Wolski
Sean de Wolski on 15 Oct 2014
Edited: Sean de Wolski on 15 Oct 2014
oh, in that case:
b = labelmatrix(bwconncomp(a))
Why reinvent the wheel?
Sean de Wolski
Sean de Wolski on 15 Oct 2014
Edited: Sean de Wolski on 15 Oct 2014
Or if you're just trying to avoid a toolbox dependency:
[~,~,idx] = unique(yourb)
b = reshape(idx - 1,size(yourb))

Community Treasure Hunt

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

Start Hunting!