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

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

 采纳的回答

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

6 个评论

Thanks a bunch... it works. I had real trouble debugging the codes i made. Using that mask is much logical and simpler ...((super like..:D))
No problems abel - congrats on asking a good question (clear, code to show what you'd done so far, etc)
Hey Sven instead of using the image as such what if a pass it through the edge function and get the edges and then run it through bwboundaries. It sounds real logical to me but when tried it out it didn't work i mean it didn't detect round objects.
What i did was
im1=rgb2gray(im);
im1=edge(im1);
....
then i tried rest of the code
Take a look at what the edge function returns - a binary matrix of only the edges. Also note the different "flavours" of the edge function (sobel, prewitt, etc.). It is not guaranteed to fully "connect" an edge right around the objects that you were otherwise connecting using bwboundaries.
Furthermore, consider what the "Area" of each connected group of pixels would be if they were *only* the pixels returned by the edge() function. Can you see that any calculations such as:
q=(4*pi*info(k).Area)/(info(k).Perimeter)^2;
No longer make sense when the Area is not the area of the closed region?
Oh...thanks..!! I was under the impression that area calculated was the area within a given boundary and not the pixel area.
You can clear all this up by just posting your image somewhere so we can perfect the code for you.

请先登录,再进行评论。

更多回答(1 个)

You should be able to get rid of your first loop by doing something like (untested):
isCircle =(4*pi*[info.Area] ./ [info.Perimeter]^2) > 0.8;
That should get the entire "isCircle" array all in one shot without using a loop.

3 个评论

Thanks professor ..It works great when trying for a single image but when i tried to run the entire code including your modification in an infinite loop processing image after image (say for a name sake like "real time") ,ran into trouble,the entire Matlab froze. But running it the other way (ie using the loop) it runs.
That makes no sense. You must have done something wrong, like omitted a dot or something.
hey, the code worked and i messed up. But what if i want to include multiple condition like {info.area>50} ..i tried it the same way with
isCircle =info.Area> 50; but didn't work..

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by