MATLAB: Help needed in debugging the code for Identifying round objects ..

debuggingimage processing

This is a matlab function that i wrote for identifying tound objects.i'm kinda a newbie in image processing so i'm guessing my code is messed up some where.But i don't seem to understand my error.please do help..:)
The code is ::
function [ ] =imagetestfile( im )
%This is a fuction to identify round objects from the given input image
im1=rgb2gray(im);
thresh=graythresh(im1);
im1=im2bw(im1,thresh);
[a b]=bwboundaries(im1);
info=regionprops(b,'Area','Centroid','Perimeter');
i=1;
for k=1:length(a)
q=(4*pi*info(k).Area)/(info(k).Perimeter)^2;
if q>0.80
c{i}=a{k};
info1{i}.Centroid=info{k}.Centroid;
i=i+1 ;
end
end
imshow(im);
hold on
for k=1:i-1
x=info1(k).Centroid(1);
y=info1(k).Centroid(2);
boundary = c{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 6);
line(x,y, 'Marker', '*', 'MarkerEdgeColor', 'r');
end
hold off
end

Best Answer

This is a pretty good attempt, and I think the code below does what you intended.
The one biggest change is that instead of trying to "copy" into a new variable "info1", I just build up a mask called "isCircle". It holds one element for every label, and just gets set to true if that element is circular.
The only other changes were to use "noholes" in the bwboundaries call (it's a little quicker and I think what you intended) and using "plot" instead of "line" (which is just a simplification).
function [ ] = imagetestfile( im ) %This is a fuction to identify round objects from the given input image
im1=rgb2gray(im);
thresh=graythresh(im1);
im1=im2bw(im1,thresh);
[a b]=bwboundaries(im1,'noholes');
info=regionprops(b,'Area','Centroid','Perimeter');
isCircle = false(1,length(a));
for k=1:length(a)
q=(4*pi*info(k).Area)/(info(k).Perimeter)^2;
if q>0.80
isCircle(k) = true;
end
end
imshow(im);
hold on
for k=find(isCircle)
x=info(k).Centroid(1);
y=info(k).Centroid(2);
boundary = a{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 6);
plot(x,y, 'r*');
end
hold off
end