Help with coin counting from an image

44 views (last 30 days)
Patrick Rohrmoser
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
clear all;
close all;
Amount = 0;
% Read the image
Coins = imread('Coins4.jpeg');
%Coins = imresize(Coins,0.3);
CoinsBinary = im2bw(Coins,0.15);
se = strel('disk',20);
fill = imfill(open,'holes');
clearing = imclearborder(fill);
%*** Automatic measuring of the diameter ****
diameter = regionprops(clearing,'MinorAxisLength');
A_cell = struct2cell(diameter);
for j = 1:length(diameter)
r(j) = A_cell{j};
radius =r/2;
rMin = int32(min(radius));
rMax = int32(max(radius));
[centers, radii] = imfindcircles(clearing,[rMin rMax],'ObjectPolarity','bright','Sensitivity',0.98,'EdgeThreshold',0.85);
numCircles = length(centers)
h = viscircles(centers,radii);
% 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;
fprintf('Total amount of coins: %.2f €', Amount);

Answers (1)

Madhav Thakker
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 deep learning solutions for object detection and classification.
Hope this helps.


Find more on Denoising and Compression 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