How to label coins after recognizing them?

4 views (last 30 days)
Hi everyone! I hope everything is great.
I have a question about coin recognition. I'm new to image processing and I'm trying to recognize the coins, label them and calculate the total amount.
Here is my code;
img = imread('bp3.jpg');
I = rgb2gray(img);
imshow(I)
bw = imbinarize(I);
figure; imshow(bw)
bw = bwareaopen(bw, 400);
figure; imshow(bw)
se = strel('disk', 3);
bw = imclose(bw, se);
bw = imfill (bw, 'holes');
figure; imshow(bw);
[r, num] = bwlabel(bw);
prop = regionprops('table',bw,{'Area','centroid'});
% kurus5=0;
% kurus10=0;
% kurus25=0;
% kurus50=0;
% tl1=0;
total=0;
for i=1:num;
switch logical(true)
case prop.Area(i)> 1.4e+06
rgb = insertText(I,[prop.Centroid(i,1) prop.Centroid(i,2)],'1tl','FontSize',48,'TextColor','black');
total=total+1;
%tl1=tl1+1;
case prop.Area(i)>1.2e+06 && prop.Area(i)<1.4e+06
rgb = insertText(I,[prop.Centroid(i,1) prop.Centroid(i,2)],'50kr','FontSize',48,'TextColor','black');
total=total+0.5;
%kurus50=kurus50+1;
case prop.Area(i)>8.7e+05 && prop.Area(i)<1.2e+06
rgb = insertText(I,[prop.Centroid(i,1) prop.Centroid(i,2)],'25kr','FontSize',48,'TextColor','black');
total = total + 0.25;
%kurus25=kurus25+1;
case prop.Area(i)>6.4e05 && prop.Area(i)<8.7e05
rgb = insertText(I,[prop.Centroid(i,1) prop.Centroid(i,2)],'10kr','FontSize',48,'TextColor','black');
total = total + 0.10;
%kurus10=kurus10+1;
otherwise
rgb = insertText(I,[prop.Centroid(i,1) prop.Centroid(i,2)],'5kr', 'FontSize',48,'TextColor','black');
total = total + 0.05;
%kurus5=kurus5+1;
end
end
subplot; imshow(rgb)
title(['Total Amount:',num2str(total),'TL'])
I can calculate the total amount but the problem is I can't label them all. Only one of the labels is shown. (It is 5kr for this example.) And let say if I delete '5kr' part, then only '10kr' label would shown. However I can't show them all at once.
I don't know how to fix that and I'd be glad if you can help me. Thanks in advance.
  2 Comments
Jan
Jan on 17 Jun 2022
This is a really bad idea:
switch logical(true)
On one hand, true is a logcial already, so casting it again is a waste of time. On the other hand, your code is working with the switched switch conditions, but this is at least confusing.
The case prop.Area(i)==1.4e+06 is unlikely, but not ibncluded in your code.
What is the purpose of the orphaned subplot; command?
Aysenur Atagün
Aysenur Atagün on 17 Jun 2022
Hi Jan!
Thank you very much for both your answer and comment. I fixed my code with your suggestions and now it works. I used logical(true) because I thought my loop works only for once and that might be solution.
I'm not sure what you're asking about but I found the area informations with prop command then I used them in the loop. But now I deleted double checks as you mentioned below.
Lastly, I replace 'subplot' with 'figure' and now it's all good.
Thank you again. All the best,
Aysenur

Sign in to comment.

Accepted Answer

Jan
Jan on 17 Jun 2022
Edited: Jan on 17 Jun 2022
rgb = insertText(I,[prop.Centroid(i,1) prop.Centroid(i,2)],'5kr', ...
'FontSize',48,'TextColor','black');
This inserts the text into the original image I in each iteration and former changes are overwritten.
Better:
rgb = I; % Create a copy to accumulate the insertes text objects
for i = 1:num
if prop.Area(i) > 1.4e+06
msg = '1tl';
totalAdd = 1.0;
elseif prop.Area(i) > 1.2e+06 % No need to check Area(i) <= 1.4e6 again
msg = '50kr';
totalAdd = 0.5;
... and so on, please complete this by your own
end
% Avoid repeated code, but use variables:
rgb = insertText(rgb, [prop.Centroid(i,1), prop.Centroid(i,2)], msg, ...
... % ^^^ not the original image I
'FontSize',48,'TextColor','black');
total = totalAdd;
end
If you want to change e.g. the fontsize, you have to modify one line only with this method.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!