This Matlab tutorial I use SIFT, RANSAC, and homography to find corresponding points between two images. Here I have used vlfeat to find SIFT features.
Full code is available at my GitHub repository
Major steps are:
0.Adding vlfeat to your Matlab workspace:
1 |
run('<path_to_vlfeat>/toolbox/vl_setup') |
1.Detect key points and extract descriptors. In the image below you can see some SIFT key points and their descriptor.
1 2 3 4 5 |
image_left=imread('images/image-left.jpg'); scale=0.20; image_left=imresize(image_left,scale); image_left=single(rgb2gray(image_left)) ; [f_image_left,d_image_left] = vl_sift(image_left) ; |
2.Matching features:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[dims, image_right_features]=size(d_image_right); [dims, image_left_features]=size(d_image_left); M=zeros(image_right_features,image_left_features); for i=1:image_right_features for j=1:image_left_features diff = double(d_image_right(:,i)) - double(d_image_left(:,j)) ; distance = sqrt(diff' * diff); M(i,j)=distance ; end end |
3.Pruning features
In this step, I only took first k top matches.
number_of_top_matches_to_select=90;
1 2 |
M_Vec=sort(M(:)); M=(M < M_Vec( number_of_top_matches_to_select ) ); |
4.Estimating transformation using RANSAC
I used RANSAC to estimate the transformation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
for i=1:M_Rows for j=1:M_Cols if M(i,j)>0 x_right=round(f_image_right(1,i)); y_right=round(f_image_right(2,i)); x_left=round(f_image_left(1,j)); y_left=round(f_image_left(2,j)); left_matches=vertcat(left_matches, [x_left,y_left]); right_matches=vertcat(right_matches, [x_right,y_right]); end end end p=99/100; e=50/100; [s,dim]=size(left_matches); s=3 N= (log (1-p))/(log(1-(1-e)^s)) iter=N;%we choose this so with probability of 99% at least one random sample is free from ouliers num=3;%minmum number of samples to make the model i.e for line is 2 and for affine is 3 threshDist=5; %5 pixel [best_number_of_inliers,best_M,index_of_matches]=affine_ransac_est(left_matches,right_matches,num,iter,threshDist); |
5.Compute optimal transformation
Finally, I used the least square method to find the affine transformation between two images.
1 |
best_M=affine_least_square_generalized(best_left_matches, best_right_matches); |