9. %read image and transform gray
L1 = rgb2gray(imread('left.jpg'));
L2 = rgb2gray(imread('right.jpg'));
%part is showpair between left and right
imshowpair(L1,L2,'montage');
title('L1(left);L2(right)');
figure;
10. imshowpair(L1,L2,'ColorChannel','red-cyan');
title('Composite Image (Red - Left Image, Cyan - Right Image)');
blobs1 = detectSURFFeatures(L1,'MetricThreshold',2000);
blobs2 = detectSURFFeatures(L2,'MetricThreshold',2000);
figure;
imshow(L1);
hold on;
plot(selectStrongest(blobs1, 30));
title('Thirty strongest SURE features in L1');
figure;
imshow(L2);
hold on;
plot(selectStrongest(blobs2, 30));
title('Thirty strongest SURE features in L2');
[features1, validBlobs1] = extractFeatures(L1, blobs1);
[features1, validBlobs2] = extractFeatures(L2, blobs2);
indexPairs = matchFeatures(features1, features2, 'Metric',
'SAD','MatchThreshold', 5)
matchedPoints1 = validBlobs1(indexPairs(:,1),:);
matchedPoints2 = validBlobs2(indexPairs(:,2),:);
figure;
showMatchedFeatures(L1, L2, matchedPoints2);
legend('Putatively matched points in L1', 'Putatively matched points in L2');
[fMatrix, epipolarInliers, status] =
estimateFundamentalMatrix(matchedPoints1, matchedPoints2,'Method',
'RANSAC','NumTrials', 10000, 'DistanceThreshold', 0.1, 'Confidence', 99.99);
if status ~= 0 || is EpipoleInImage(fMatrix', size(L1))|| is
EpipoleInImage(fMatrix', size(L2))
erro(['Either not enought matching points were found']);
end
inlierPoints1 = matchedPoints1(epipolarInliers, :);
inlierPoints2 = matchedPoints2(epipolarInliers, :);
figure;
showMatchedFeatures(L1, L2, inlierPoints1, inlierPoints2);
legend('Inlier points in L1', 'Inlier points in L2');
[t1, t2] = estimateUncalibratedRectification(fMatrix, ...
inlierPoints1.Location, inlierPoints2.Location, size(L2));
tform1 = projective2d(t1);
tform2 = projective2d(t2);
I1Rect = imwarp(L1, tform1, 'OutputView', imref2d(size(L1)));
I2Rect = imwarp(L2, tform2, 'OutputView', imref2d(size(L2)));
% transform the points to visualize them together with the rectified images
pts1Rect = transformPointsForward(tform1, inlierPoints1.Location);
pts2Rect = transformPointsForward(tform2, inlierPoints2.Location);
figure;
showMatchedFeatures(L1Rect, L2Rect, pts1Rect, pts2Rect);
legend('Inlier points in rectified L1', 'Inlier points in rectified L2');
Irectified = cvexTransformImagePair(L1, tform1, L2, tform2);