cd /home/don/Dropbox/school/CS485/hw4


%targetimg = 'house1.jpg';

%filter = ones(7,7);
%edgethreshold = [.3];

%im = imread(targetimg);
%imgray = rgb2gray(im);
%imsmall = imresize(imgray,.25);





%imresult1 = conv2(imsmall, filter, 'same');
%figure; imagesc(imresult1); colormap gray; axis image;

% [bw, thr] = edge(imsmall,'canny',edgethreshold); 
% figure; imagesc(bw); colormap gray; axis image
% 
% connectedbw = bwlabel(bw);
% figure; imagesc(connectedbw);axis image;

%corners = harris_corners(im, 7, 1.5);
%imshow(im); hold on
%plot(corners(:,1), corners(:,2),'ro');

%%3a
%norot_img = imread('house1.jpg');
%rot_img = imread('house1-rotated.jpg');
%corners = harris_corners(norot_img, 7, 1.5);
%figure; imshow(norot_img); hold on
%plot(corners(:,1), corners(:,2),'ro');
%corners = harris_corners(rot_img, 7, 1.5);
%figure; imshow(rot_img); hold on
%plot(corners(:,1), corners(:,2),'ro');

%3b
%old_im = im;
%im = imread('house1.jpg');
%corners = harris_corners(im, 7, 1.5);
%figure; imshow(im); hold on
%plot(corners(:,1), corners(:,2),'ro');

%im = imread('house1-2down.jpg');
%corners = harris_corners(im, 7, 1.5);
%figure; imshow(im); hold on
%plot(corners(:,1), corners(:,2),'ro');%%

%im = imread('house1-4down.jpg');
%corners = harris_corners(im, 7, 1.5);
%figure; imshow(im); hold on
%plot(corners(:,1), corners(:,2),'ro');
%im = old_im;


%SSD
% 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);
% for feature=1:length(corr_corners)
%     feature
%     curr = corr_corners(feature,:); 
%     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;
%         count = 0;
%         for i = 1+win:length(im_right(feature,:,1))-win
%             for j = 1+win:length(im_right(:,1))-win
%                 count = count +1;
%                 ssd=0.0;
%   	            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
%     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

%for i=1:length(corr_corners)
%    line([best(i,1)+length(im_left(1,:,1)) corr_corners(i,1)], [best(i,2) corr_corners(i,2)]);
%end

% 
 im_left = imread('house1.jpg');
% 
% 
 im_right = imread('house2.jpg');
% 
 [fa,da] = vl_sift(im2single(rgb2gray(im_left))) ;
 [fb,db] = vl_sift(im2single(rgb2gray(im_right))) ;
 
 [matches, scores] = vl_ubcmatch(da,db) ;
 
 [drop, perm] = sort(scores, 'ascend') ;
 matches = matches(:, perm) ;
 scores  = scores(perm) ;
 
 imshow(appendimages(im_left,im_right));
 

 range = 1:250;
 xa = fa(1,matches(1,range)) ;
 xb = fb(1,matches(2,range)) + size(im_left,2) ;
 ya = fa(2,matches(1,range)) ;
 yb = fb(2,matches(2,range)) ;
 
 hold on ;
 h = line([xa ; xb], [ya ; yb]) ;
