MATLAB: Counting just coins in this image !…

bwconncompcountshapes

Hi
I tried below code to count coins in image and it's work fine :
function ret = CountCoins(img)
subplot(2,2,1);
imshow(img);
subplot(2,2,2);
imgBW = im2bw(img);
imshow(imgBW);
subplot(2,2,3);
imhist(img);
subplot(2,2,4);
imgZ = zeros(size(img));
imgZ(img > 100) = 1;
imshow(imgZ);
ret = round(sum(imgBW(:)) / 2100);
imgConn = bwconncomp(imgZ);
ret = imgConn.NumObjects;
end
I want just count coins no anything else.
Look…
With above function i can count coins in this image :
Result is 10.(Correct)
But result of this image is wrong;
Result is 12.(Wrong)
because 2 shapes are not coins!
_______________________________________
My question:
How can i count "Just" coins on that image?

Best Answer

Did you see my image segmentation tutorial: http://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial-blobsdemo? I do exactly that, plus a bunch of other useful things. I find 5 cent and 10 cent pieces based on their area. What you want to do is to adapt that to look at something else, like a combination of MeanIntensity and circularity (the ratio of perimeter^2 over (4*pi*area)), or you might want to use the standard deviation in the blob. Then use ismember() to extract out just the blobs that you want. My demo shows you how to do all that, you just need to adapt it slightly, like I said.