cmap = hsv(12);
win = 20/2;
im_left = imread('house1.jpg');
corr_corners = harris_corners(im_left, 7, 1.5);
im_right = imread('house2.jpg');

best = corr_corners;
clear ssdall2;
figure;
imshow(appendimages(im_left,im_right));
drawnow;
hold on;                                                                                                                                                                                      
im_left = double(im_left);
im_right = double(im_right);
%%iterate each feature detected throught the harris corner detector
for feature=1:length(corr_corners)
  feature                                                                                                                                                                               
  curr = corr_corners(feature,:);                                                                                  
  %%Make sure the window will not exit the bounds of the image.                                                                                                                                 
  if curr(2) >1+win && curr(2) < length(im_left(:,1))-win && curr(1) > 1+win && curr(1) < length(im_left(1,:,1))-win
    oldSSD = 99999999*64*win^2;
    %%try and match the window at every pixel in the target image.
    for i = 1+win:length(im_right(feature,:,1))-win
      for j = 1+win:length(im_right(:,1))-win
        ssd=0.0;
        %%the window of the current feature, subtracted with an
        %%arbitrary window of the target image, squared, summed
        temp = (im_left(curr(2)-win:curr(2)+win,curr(1)-win:curr(1)+win,:)  - im_right(j-win:j+win,i-win:i+win,:)).^2;
        ssd=sum(temp(:));
        if(ssd < oldSSD)
          oldSSD = ssd;
          best(feature,:) = [i j];
        end
      end
    end
  end
  %%testing out a threshold of accurate matching.
                                %    if(oldSSD < 3500000)
  line([best(feature,1)+length(im_left(1,:,1)) corr_corners(feature,1)],[best(feature,2) corr_corners(feature,2)],'Color',cmap(ceil(rand*12),:));
  drawnow;
                                %    end
end