How to count the number of coins in a image using erosion operator?
18 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Evan
on 13 Aug 2013
Edited: Evan
on 13 Aug 2013
You might find Image Analyst's blobs demo to be helpful: http://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial-blobsdemo
2 Comments
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
maskPhoto = grayPhoto > threshold;
coinLocs = regionprops(maskPhoto,'Centroid');
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 Comments
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.
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!