MATLAB: Counting coins in an image…

bwconncompImage Processing Toolbox

Hello
I tried below code for counting coins in an image. and it's work…
..........................
function p = CountCoins(i)
subplot(2,2,1);
imshow(i);
subplot(2,2,2);
t=im2bw(i);
imshow(t);
subplot(2,2,3);
imhist(i);
subplot(2,2,4);
x=zeros(size(i));
x(i>110)=1;
imshow(x);
c=bwconncomp(x);
p=c.NumObjects;
end
.................
But i need to do it without using 'bwconncomp()' function. please lead me… Thank You

Best Answer

I'd use bwlabel
[labeledImage, numberOfCoins] = bwlabel(t);
If you're not allowed to use that either, hopefully you're still allowed to use sum(). If you know the area of each coin in pixels, you can sum the number of pixels in (the badly-named) t and divide by the number of pixels in an average coin.
numberOfCoins = round(sum(t(:)) / averageNumberOfPixelsInACoin));
Otherwise you're looking at writing your own connected components routine, which you don't want to do - no reason for it.