%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');

% Converts the datatype from uint8 to double
c2 = double(c2);
c4 = double(c4);
c6 = double(c6);



% 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
ctd = double(ctd);
ctd = ctd(:)./norm(ctd(:));



% 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(['c' 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.*ctd));

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')


