%Seth Eatinger

%----------------------------------------------------------------
%calcspot()
%
% This function processes the raw detections from the scanit() function to
%find the actual corners of the card. This function must deal with a 
%number of issues including some false detection and corner point 
%calculations.  Essentially, the raw detections tend to clump around
%the actual corners of the cards.  This function, then, separates
%the raw data into these individual clumps or "hotspots."  Each
%hotspot equates to a corner of a card in the image.  The median
%(works better than the mean) is found among the data in each hotspot
%These values are put into a matrix and sent to the matching/correlation
%functions designed by Raymond Wagner.



function [spot] = locspot3(sweet)

%Tweeking constants
skip = 30;            %Distance between hotspots 
maxsize = 1000;       %Used to filter out the huge spade in the middle on the ace of spade.  The number of detections is above 1000.
maxHotSpot = 10;      %Maximum number of hotspots to detect.  Used to initialize other variables.


%Finds the number of detection window locations.
dim = size(sweet);
r=dim(1,1);

%Sets up the indexing variables.
hotspotGlobal = 1;                         %Holds the total number of hotspots found.
hotspotIndexMatRow(1,1:maxHotSpot) = 1;    %Matrix holds the lengths of each column in the med_row matrix.
hotspotIndexMatCol(1,1:maxHotSpot) = 1;    %Matrix holds the lengths of each column in the med_col matrix.


%Sets up storage matrice
med_row = [];                              %Storage matrix to hold the separated raw detection row values (each column is a different hotspot)
med_col = [];                              %Storage matrix to hold the separated raw detection column values (each column is a diff. hotspot)


%Sorting loop


for x = 1:r-1,
   
   diffcross = sweet(x+1,2) - sweet(x,2);   %Distance between two detection values.
	
   if (diffcross > skip)                    %The next raw detection point is the start of a new hotspot. 
      
      %Values are stored.
      med_row(hotspotIndexMatRow(1,hotspotGlobal),hotspotGlobal) = sweet(x,1);
      med_col(hotspotIndexMatCol(1,hotspotGlobal),hotspotGlobal) = sweet(x,2);
      
      %Increment counters.
      hotspotIndexMatRow(1,hotspotGlobal) = hotspotIndexMatRow(1,hotspotGlobal) + 1;
      hotspotIndexMatCol(1,hotspotGlobal) = hotspotIndexMatCol(1,hotspotGlobal) + 1;
      
      hotspotGlobal = hotspotGlobal + 1;
      
   else                                    %Still in the same hotspot.  Store the raw detection point in the same columns.
      
      %Values are stored.
      med_row(hotspotIndexMatRow(1,hotspotGlobal),hotspotGlobal) = sweet(x,1);
      med_col(hotspotIndexMatCol(1,hotspotGlobal),hotspotGlobal) = sweet(x,2);
      
      %Increment counters.
      hotspotIndexMatRow(1,hotspotGlobal) = hotspotIndexMatRow(1,hotspotGlobal) + 1;
      hotspotIndexMatCol(1,hotspotGlobal) = hotspotIndexMatCol(1,hotspotGlobal) + 1;
      
   end
   
end

    
indexer = 1;

for x = 1:hotspotGlobal,
   len = length(med_row(1:hotspotIndexMatRow(1,x)-1,:));                             %This loop runs through the differen hotspots
                                                                                     %and calculates the actual corner points from
   if (len < maxsize)                                                                %the now separated data.  The if-then is used
      y = round(median(med_row(1:hotspotIndexMatRow(1,x)-1,x))) - 6;                 %to filter out the big ace of spades' spade.
      if (y < 0) 
         new(indexer,1) = 1;
      else
         new(indexer,1) = y;
      end
      
      z = round(median(med_col(1:hotspotIndexMatCol(1,x)-1,x)));
      if (z < 0)
         new(indexer,2) = 1;
      else
         new(indexer,2) = z;
      end
      
         
      
      %new(indexer,2) = round(median(med_col(1:hotspotIndexMatCol(1,x)-1,x)));        
      indexer = indexer + 1;
   end
      
end

sortedSpot = sortrows(new,1	);  %sort the corners for ease in reading.

spot = sortedSpot;




     
