%Seth Eatinger
%Elec 301
%Project - Poker Card Detection


% Loads the project path
addpath /home/seat/pr



% Loads the image library cards into MATLAB
c2 = imread('2c.TIF','tif');
c4 = imread('4c.TIF','tif');
c6 = imread('6c.TIF','tif');

f2 = fft2(c2);
f4 = fft2(c4);
f6 = fft2(c6);

d2 = log(1+abs(f2));
d4 = log(1+abs(f4));
d6 = log(1+abs(f6));

% Converts the datatype from uint8 to double
d2 = double(d2);
d4 = double(d4);
d6 = double(d6);



% Load the card-to-detect (CTD)
% We'll make it the four of clubs for the example.
ctd = imread('4c.TIF','tif');

% Displays the image that we are trying to detect.
imagesc(ctd)
title('Card-To-Detect (CTD)')
colormap(gray(256))
axis('square')

% Prepare the CTD to be detected by converting to the double datatype and normalizing
fctd = fft2(ctd);
dctd = log(1+abs(fctd));
dctd = double(dctd);
dctd = dctd(:)./norm(dctd(:));



% This loop will iterate for each of the image library cards and
% perform the necessary calculations (inner products) to
% compare the original photo to the library.

for s = 2:2:6
   
  %Reshapes the 276x256 matrix to a row vector with 276*256 elements.
  tempcard = (eval(['d' num2str(s) '(:)']));

  %Normalizes the library card image.
  tempcard = tempcard./(norm(tempcard));

  %Calculates the correlation between the actual image and sample.
  sim(s,1) = sum(sum (tempcard.*dctd));

end



% Show the correlations between the CTD and the library
sim(2,1) % Comparison between CTD and the 2 of clubs
sim(4,1) % Comparison between CTD and the 4 of clubs - this should be the largest
sim(6,1) % Comparison between CTD and the 6 of clubs

% Finds the maximum correlation
[val, ind] = max(sim);

% Prints out which card matches the CTD.
['The card is the ' num2str(ind) ' of clubs.']

% Displays the sample which best fits the original
figure
imagesc(eval(['c' num2str(ind)]))
title('The CTD has been detected to be the:')
colormap(gray(256))
axis('square')


