I1 = rgb2gray(imread('im0.png'));
I2 = rgb2gray(imread('im1.png'));

NUM_MATCHES = 30;

points1 = detectSURFFeatures(I1);
points2 = detectSURFFeatures(I2);

[f1,vpts1] = extractFeatures(I1,points1);
[f2,vpts2] = extractFeatures(I2,points2);

[indexPairs, matchmetric] = matchFeatures(f1,f2) ;
[B, I] = sort(matchmetric);
matchedPoints1 = vpts1(indexPairs(:,1));
matchedPoints2 = vpts2(indexPairs(:,2));

matchedPoints1 = matchedPoints1(I(1:NUM_MATCHES));
matchedPoints2 = matchedPoints2(I(1:NUM_MATCHES));

figure; showMatchedFeatures(I1,I2,matchedPoints1,matchedPoints2);
legend('matched points 1','matched points 2');

x1 = [matchedPoints1.Location, ones(NUM_MATCHES,1)];
x2 = [matchedPoints2.Location, ones(NUM_MATCHES,1)];
save('x1.mat', 'x1');
save('x2.mat', 'x2');
