Counting coins in an image...

18 views (last 30 days)
Hamed
Hamed on 17 Jan 2013
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 Comment
Amith Kamath
Amith Kamath on 17 Jan 2013
Edited: Image Analyst on 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.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 17 Jan 2013
Edited: Image Analyst on 17 Jan 2013
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 Comments
Hamed
Hamed on 18 Jan 2013
Tnx. Sorry but i'm amature. Please show me how do you'r prev answer...
Image Analyst
Image Analyst on 18 Jan 2013
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!