# How to count the number of coins in a image using erosion operator?

41 views (last 30 days)
krishna on 13 Aug 2013
in a image after using erosion operator on that image there will be number of coins placed randomly so now we have to find the total number of coins in that image. What are the steps to do this? can we do this by using the area of the coins?
##### 2 CommentsShowHide 1 older comment
krishna on 19 Aug 2013
i have tried using edge detection but not getting the exact logic . the problem is in a image der r some coins we will use erosion operator on that and then we have to count no of coins in that in real time . What logic can we use? for images search in google images u will get one image in which dis operator is used

Evan on 13 Aug 2013
Edited: Evan on 13 Aug 2013
##### 2 CommentsShowHide 1 older comment
Evan on 20 Aug 2013
As is done in the tutorial, you can threshold the image, then use regionprops() to obtain all sorts of information about the image. While regionprops can allow you to find much more sophisticated characteristics of your photo, if you just want to count the coins, all you need to do is count the size of the structure returned.
The below snippet of code goes from an RGB image to a count of the number of regions in the thresholded image:
grayPhoto = rgb2gray(myPhoto);
threshold = 0.5; %set this value to one which works for you
nCoins = size(coinLocs,1);
fprintf('There are %i coins in this photo',nCoins)
So, basically, it all depends on being able to find a threshold value that robustly separates coins from the background of your image.

Image Analyst on 19 Aug 2013
I have no idea why you're wanting to do it via morphology instead of with bwlabel() or regionprops() like most people would do it. But if you do you first need to threshold, then call bwulterode() to get a dot for each coin, then sum the image to count the dots. This is not an efficient way to count them, but it is a morphological way if you require that for some reason (like you want to show how slow and inefficient some methods can be).
binaryImage = coinsImage < 128; % or whatever.
dots = bwulterode(binaryImage);
numberOfCoins = sum(dots(:));
##### 2 CommentsShowHide 1 older comment
Image Analyst on 20 Aug 2013
Overlapped coins should give two dots using bwulterode() because the two overlapped coins will split apart into two pieces as you continually erode it away. So the code I already gave you should still work.

### Categories

Find more on Object Analysis in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by