Counting coins in an image...

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

1 commentaire

Amith Kamath
Amith Kamath le 17 Jan 2013
Modifié(e) : Image Analyst le 17 Jan 2013
Is this so that you don't use the Image Processing Toolbox, or is it just to avoid that particular function? If it's the latter, I would guess that you can use bwlabel() as well.

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 17 Jan 2013
Modifié(e) : Image Analyst le 17 Jan 2013

2 votes

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.

4 commentaires

Hamed
Hamed le 18 Jan 2013
Tnx. But how get 'averageNumberOfPixelsInACoin' ?
Image Analyst
Image Analyst le 18 Jan 2013
Measure the smallest and largest coins with a ruler. Then figure out how many pixels across they are. You can measure your screen in cm, measure the coin in cm, and knowing how many pixels across your image is, you can calculate the number of pixels per coin. Then to get the area, just use pi*(diameter in pixels)^2 / 4.
Hamed
Hamed le 18 Jan 2013
Tnx. Sorry but i'm amature. Please show me how do you'r prev answer...
Put the coins on a table. Put one coin from each size next to each other so they're all in a line. (Let's say there are 3 coins of different sizes.) Get a ruler. Lay the ruler across the (three) coins. Read the length off the ruler. Divide by the number of coins. This is the average diameter of the coins.
Lay down a ruler or yardstick/meterstick. Take an image of it using whatever method and field of view you are going to use. Note how many centimeters your entire field of view is. Then determine how many pixels across your image is (for example read it into MATLAB and call size(), or look in the status bar at the bottom of Windows Explorer after you've clicked on the image).
Let's say your average coin size is 1.8 cm, say that your image is 20 cm across, and let's say that your image size is 1280 across. So your typical coin diameter will be
averageDiameterInPixels = (1280 pixels) * (1.8 cm) / (20 cm) = 115.2 pixels.
and your typical coin area =
averageArea = pi * averageDiameterInPixels ^2 / 4;
= 10,423 (square) pixels.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by