image1 = imread('i1.jpeg');
image2 = imread('i2.jpeg');
points1 = detectSURFFeatures(rgb2gray(image1));
points2 = detectSURFFeatures(rgb2gray(image2));
[features1, validPoints1] = extractFeatures(rgb2gray(image1), points1);
[features2, validPoints2] = extractFeatures(rgb2gray(image2), points2);
indexPairs = matchFeatures(features1, features2);
matchedPoints1 = validPoints1(indexPairs(:, 1), :);
matchedPoints2 = validPoints2(indexPairs(:, 2), :);
[tform, inlierPoints1, inlierPoints2] = estimateGeometricTransform(matchedPoints2, matchedPoints1, 'projective');
showMatchedFeatures(image2, image1, inlierPoints1, inlierPoints2, 'montage');
title("showMatchedFeatures")
outputView = imref2d(size(image1));
warpedImage = imwarp(image2, tform, 'OutputView', outputView);
imshowpair(image1, warpedImage, 'montage');
title("showMatchedFeatures")
grayImage1 = rgb2gray(image1);
grayWarpedImage1 = rgb2gray(warpedImage);
whiteMask1 = grayImage1 == 255;
whiteMask2 = grayWarpedImage1 == 255;
newImage(whiteMask1) = warpedImage(whiteMask1);
newImage(whiteMask2) = image1(whiteMask2);
title('Noise corrected Image');