# Help with coin counting from an image

8 views (last 30 days)
Patrick Rohrmoser on 29 Mar 2021
Answered: Madhav Thakker on 14 Apr 2021
Hey guys,
I hope you can help me in this case. I want to calculate the amount of coins, so for example I have a 2€ and 1€ coin, then the result should be 3€. So far I can check the diameter of various BW images. As you can see in the code I set the range where the circles should be detected in respect to the min and max radius of the coins. My problem is when the distance to the coins is different then I get different diameters. Has anyone a good idea how to deal with this issue?
Big thanks in advance for your time and support!
% _____________________________________________________________
% Clear content
clc;
clear all;
close all;
%______________________________________________________________
Amount = 0;
% Read the image
%Coins = imresize(Coins,0.3);
CoinsBinary = im2bw(Coins,0.15);
se = strel('disk',20);
open=imopen(CoinsBinary,se);
fill = imfill(open,'holes');
clearing = imclearborder(fill);
figure
imshow(clearing)
%*** Automatic measuring of the diameter ****
diameter = regionprops(clearing,'MinorAxisLength');
A_cell = struct2cell(diameter);
for j = 1:length(diameter)
r(j) = A_cell{j};
end
[centers, radii] = imfindcircles(clearing,[rMin rMax],'ObjectPolarity','bright','Sensitivity',0.98,'EdgeThreshold',0.85);
numCircles = length(centers)
figure
imshow(Coins)
% Just for checking if it works to calculate the sum like this....
for i = 1:numCircles
if radii(i) > rMin && radii(i) < 140
Amount = Amount + 0.1;
elseif radii(i) > 140 && radii(i) < 150
Amount = Amount + 0.2;
elseif radii(i) > 150 && radii(i) < 160
Amount = Amount + 1;
elseif radii(i) > 160 && radii(i) < 170
Amount = Amount + 0.5;
elseif radii(i) > 170 && radii(i) < rMax
Amount = Amount + 2;
end
end
fprintf('Total amount of coins: %.2f €', Amount);

Madhav Thakker on 14 Apr 2021
Hi Patrick,
I understand you want to classify each object (coin in your case) and then do some processing (adding the values). If you are not getting good results with standard Image processing techniques, you can look into https://in.mathworks.com/help/vision/ug/getting-started-with-object-detection-using-deep-learning.html deep learning solutions for object detection and classification.
Hope this helps.