% Loading the images
clear all
close all
circle2 = imread('circle2.png');
circle5 = imread('circle5.png');
circle15 = imread('circle15.png');

% Displaying them
figure()
subplot(311)
imshow(circle2)
title('Circle 2');
subplot(312)
imshow(circle5)
title('Circle 5');
subplot(313)
imshow(circle15)
title('Circle 15');

% Filling the circles
circle2_filled = imfill(circle2,'holes');
circle5_filled = imfill(circle5,'holes');
circle15_filled = imfill(circle15,'holes');

% Displaying
figure()
subplot(311)
imshow(circle2_filled)
title('Circle 2 filled');
subplot(312)
imshow(circle5_filled)
title('Circle 5 filled');
subplot(313)
imshow(circle15_filled)
title('Circle 15 filled');

% Using regionprops to find the "true" areas for comparison with different
% methods
circle2_label = bwlabel(circle2_filled,4);
circle2_area = regionprops(circle2_label,'Area');
circle5_label = bwlabel(circle5_filled,4);
circle5_area = regionprops(circle5_label,'Area');
circle15_label = bwlabel(circle15_filled,4);
circle15_area = regionprops(circle15_label,'Area');

% Using regionprops to find the "true" perimeters for comparison with different
% methods
circle2_label = bwlabel(circle2_filled,4);
circle2_perimeter = regionprops(circle2_label,'perimeter');
circle5_label = bwlabel(circle5_filled,4);
circle5_perimeter = regionprops(circle5_label,'perimeter');
circle15_label = bwlabel(circle15_filled,4);
circle15_perimeter = regionprops(circle15_label,'perimeter');
%% Now we use some different methods to calculate the perimeters and areas of these circles

% Get boundaries of circles
c2_boundaries = bwboundaries(circle2_filled);
c5_boundaries = bwboundaries(circle5_filled);
c15_boundaries = bwboundaries(circle15_filled);

% Get their chain codes. Chain code function by Alessandro Mannini (2010)
c2_cc = chaincode(c2_boundaries{1});
c5_cc = chaincode(c5_boundaries{1});
c15_cc = chaincode(c15_boundaries{1});
c2_chaincode = c2_cc.code;
c5_chaincode = c5_cc.code;
c15_chaincode = c15_cc.code;

% CALCULATE PERIMETERS
% based on methods by Freeman, Vossepoel and Kulpa
% See pages 15 and 16 of 2019 lecture notes on object description
c2_P_freeman = perimeter_from_chain_code(c2_chaincode, 'Freeman');
c2_P_vossepoel = perimeter_from_chain_code(c2_chaincode, 'Vossepoel');
c2_P_kulpa = perimeter_from_chain_code(c2_chaincode, 'Kulpa');

c5_P_freeman = perimeter_from_chain_code(c5_chaincode, 'Freeman');
c5_P_vossepoel = perimeter_from_chain_code(c5_chaincode, 'Vossepoel');
c5_P_kulpa = perimeter_from_chain_code(c5_chaincode, 'Kulpa');

c15_P_freeman = perimeter_from_chain_code(c15_chaincode, 'Freeman');
c15_P_vossepoel = perimeter_from_chain_code(c15_chaincode, 'Vossepoel');
c15_P_kulpa = perimeter_from_chain_code(c15_chaincode, 'Kulpa');

% CALCULATE AREAS
% Freeman (1970). See page 15 of 2019 lecture notes on object description
c2_A_freeman = area_from_chain_code(c2_cc);
c5_A_freeman = area_from_chain_code(c5_cc);
c15_A_freeman = area_from_chain_code(c15_cc);
% Pattern matching - bit quads. See page 17 of lecture notes
% NB! implementation of pattern matching is not finished

% Measure differences between "true" and calculated values
fprintf('\nAccuracy of PERIMETER calculations:\n');
% circl2.png
fprintf('circle2.png\tFreeman\t\tDifference: %.2f\n', abs(c2_P_freeman-circle2_perimeter.Perimeter));
fprintf('\t\tVossepoel\tDifference: %.2f\n', abs(c2_P_vossepoel-circle2_perimeter.Perimeter));
fprintf('\t\tKulpa\t\tDifference: %.2f\n\n', abs(c2_P_kulpa-circle2_perimeter.Perimeter));
% circle5.png
fprintf('circle5.png\tFreeman\t\tDifference: %.2f\n', abs(c5_P_freeman-circle5_perimeter.Perimeter));
fprintf('\t\tVossepoel\tDifference: %.2f\n', abs(c5_P_vossepoel-circle5_perimeter.Perimeter));
fprintf('\t\tKulpa\t\tDifference: %.2f\n\n', abs(c5_P_kulpa-circle5_perimeter.Perimeter));
%circle15.png
fprintf('circle15.png\tFreeman\t\tDifference: %.2f\n', abs(c15_P_freeman-circle15_perimeter.Perimeter));
fprintf('\t\tVossepoel\tDifference: %.2f\n', abs(c15_P_vossepoel-circle15_perimeter.Perimeter));
fprintf('\t\tKulpa\t\tDifference: %.2f\n\n', abs(c15_P_kulpa-circle15_perimeter.Perimeter));
% -------------------------------------------------
% -------------------------------------------------
fprintf('\nAccuracy of AREA calculations:\n');
% circl2.png
fprintf('circle2.png\tFreeman\t\tDifference: %.2f\n', abs(c2_A_freeman-circle2_area.Area));
% circle5.png
fprintf('circle5.png\tFreeman\t\tDifference: %.2f\n', abs(c5_A_freeman-circle5_area.Area));
%circle15.png
fprintf('circle15.png\tFreeman\t\tDifference: %.2f\n', abs(c15_A_freeman-circle15_area.Area));



