% Demo of histograms and its Cosine similarity
% Concordia Univ, ECE dept, Dr. M. Amer
% Author: J. Valognes
%%%%%%%%%%%%%
%Platform: Windows 64-bit, MATLAB R2016a.
%Run: Run histDemo.m in Matlab.
%Input:	Two images in the 'InputData' folder
%Output: Cosine Similarity measure between the two images histograms
%%%%%%%%%%%%%%%%%%


clc;
clear;

image_folder = './';
imagepath1 = [image_folder, 'Face1.jpg'];
imagepath2 = [image_folder, 'Face2.jpg'];

img1 = imread(imagepath1);
img2 = imread(imagepath2);
if size(img1,3) > 1
    img1 = rgb2gray(img1);
end
if size(img2,3) > 1
    img2 = rgb2gray(img2);
end

%Generate normalized histograms
hist1 = imhist(img1, 256)/(size(img1,1)*size(img1,2));
hist2 = imhist(img2, 256)/(size(img2,1)*size(img2,2));

%Measure cosine similarity
similarity = histCosSimilarity(hist1, hist2);

%Plot histograms
figure(1);
plot(hist1, 'k-', 'LineWidth', 1.5);
hold on	
plot(hist2, 'r:', 'LineWidth', 1.5);        	
%xlim([0 255]); 
%ylim([0 0.04]);
legd1 = 'Hist Face1';
legd2 = ['Hist Face2; histSim=' num2str(similarity)];
legend(legd1,legd2);





