clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
imageorj=imread('coins.jpg');
subplot(2, 3, 1);
imshow(imageorj);
drawnow;
axis on;
title('Original Image', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
image=rgb2gray(imageorj);
level = graythresh(image);
bw = ~im2bw(image,level);
subplot(2, 3, 2);
imshow(bw);
title('Thresholded Image', 'FontSize', fontSize);
bw=imfill(bw,'holes');
bw = bwareaopen(bw,30);
subplot(2, 3, 3);
imshow(bw);
title('Binary Image', 'FontSize', fontSize);
[labeledImage, numberOfCoins] = bwlabel(bw, 4);
subplot(2, 3, 4);
imshow(labeledImage, []);
title('Labeled Image', 'FontSize', fontSize);
stats = regionprops(labeledImage, 'Area', 'Centroid');
allAreas = [stats.Area]
sortedAreas = sort(allAreas, 'descend')
subplot(2, 3, 5);
bar(sortedAreas, 'BarWidth', 1);
title('Areas', 'FontSize', fontSize);
grid on;
d1 = (sortedAreas(5)+sortedAreas(6))/2
d2 = (sortedAreas(7)+sortedAreas(8))/2
d3 = (sortedAreas(9)+sortedAreas(10))/2
d4 = (sortedAreas(11)+sortedAreas(12))/2
d5 = (sortedAreas(13)+sortedAreas(14))/2
figure
imshow(imageorj);
total = 0;
coinFontSize = 15;
for n = 1 : numberOfCoins
thisArea = stats(n).Area
centroid = stats(n).Centroid;
if thisArea > 10158
total = total + 0.50;
caption = sprintf('50 Qepik, area = %d', thisArea);
elseif thisArea > 7561
total = total + 0.20;
caption = sprintf('20 Qepik, area = %d', thisArea);
elseif thisArea > 6605
total = total + 0.10;
caption = sprintf('10 Qepik, area = %d', thisArea);
elseif thisArea > 5317
total = total + 0.05;
caption = sprintf('5 Qepik, area = %d', thisArea);
elseif thisArea > 4518
total = total + 0.03;
caption = sprintf('3 Qepik, area = %d', thisArea);
else
total = total + 0.01;
caption = sprintf('1 Qepik, area = %d', thisArea);
end
text(centroid(1), centroid(2), caption, 'Color', 'b', 'FontSize', coinFontSize, 'FontWeight', 'bold');
end
caption = sprintf('Total coins amount = %.2f AZN', total);
title(caption, 'FontSize', fontSize)
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
helpdlg(caption);