function homography = ransac(image1, image2)

% Find SIFT keypoints for each image
[im1, des1, loc1] = sift(image1);
[im2, des2, loc2] = sift(image2);

% distRatio: Only keep matches in which the ratio of vector angles from the
%   nearest to second nearest neighbor is less than distRatio.
distRatio = 0.75;   

% For each descriptor in the first image, select its match to second image.
des2t = des2';                          % Precompute matrix transpose
for i = 1 : size(des1,1)
   dotprods = des1(i,:) * des2t;        % Computes vector of dot products
   [vals,indx] = sort(acos(dotprods));  % Take inverse cosine and sort results

   % Check if nearest neighbor has angle less than distRatio times 2nd.
   if (vals(1) < distRatio * vals(2))
      match(i) = indx(1);
   else
      match(i) = 0;
   end
end

ind=find(match);
r_match(:,1)=ind;
r_match(:,2)=match(ind);
		
result=0;
for K=1:100
    r_value=rand(1);
    r_index=floor(r_value*size(r_match,1));
    if r_index==0
        r_index=1;
    end
    x=floor(loc1(r_match(r_index,1),1));
    y=floor(loc1(r_match(r_index,1),2));
    xprime=floor(loc2(r_match(r_index,2),1));
    yprime=floor(loc2(r_match(r_index,2),2));
    
    count=0;l=1;
    H=[1 0 xprime-x;0 1 yprime-y;0 0 1];
    for j=1:size(r_match,1)
       Hdash=H*[loc1(r_match(j,1),1) loc1(r_match(j,1),2) 1]';
       ssd=(loc2(r_match(j,2),1)-Hdash(1))^2+(loc2(r_match(j,2),2)-Hdash(2))^2;
            if(ssd<0.6) 
                count=count+1;
                p(l,1)=loc1(r_match(j,1),1);
                p(l,2)=loc1(r_match(j,1),2);
				pprime(l,1)=loc2(r_match(j,2),1);
				pprime(l,2)=loc2(r_match(j,2),2);
                l=l+1;
            end
    end
       if result<count
          result=count;
          Hprime=H;
          p_inliers=p;
		  pprime_inliers=pprime;
         
       end
end

p_inliers(:,3)=1;
pprime_inliers(:,3)=1;
h=pprime_inliers\p_inliers;
homography=h';


