clc;
clear;
close all;
load colorImage3005;
load colorImage3006;
I11=colorImage3005;
I22=colorImage3006;
% extractFeatures 函数输入为灰度图像,因此要将RGB图像用函数 rgb2gray 进行转换
image1= rgb2gray(I11);
image2= rgb2gray(I22);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Surf 特征检测
ptsImage1 = detectSURFFeatures(image1,'MetricThreshold',800,'NumOctaves',6);
ptsImage2 = detectSURFFeatures(image2,'MetricThreshold',800,'NumOctaves',6);
disp('SURF Features of Image1')
size(ptsImage1)
disp('SURF Features of Image2')
size(ptsImage2)
% Surf 特征提取
[featuresOriginal,validptsImage1] = extractFeatures(image1,ptsImage1);
[featuresDistorted,validptsImage2] = extractFeatures(image2,ptsImage2);
% Surf 特征匹配
index_pairs = matchFeatures(featuresOriginal,featuresDistorted,'MatchThreshold',1,'MaxRatio',1);
matchedptsImage1 = validptsImage1(index_pairs(:,1));
matchedptsImage2 = validptsImage2(index_pairs(:,2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% points1 = detectHarrisFeatures(image1);
% points2 = detectHarrisFeatures(image2);
%
% [f1, vpts1] = extractFeatures(image1, points1);
% [f2, vpts2] = extractFeatures(image2, points2);
%
% indexPairs = matchFeatures(f1, f2) ;
% matchedptsImage1 = vpts1(indexPairs(1:20, 1));
% matchedptsImage2 = vpts2(indexPairs(1:20, 2));
% 显示有误匹配的情况
figure;
subplot(2,1,1)
showMatchedFeatures(I11,I22,matchedptsImage1,matchedptsImage2,'montage');
str=sprintf('Matched inlier points\n(including outliers)');
title(str,'fontname','Times New Roman','FontSize',12);
disp('Matched features without MLESAC')
size(matchedptsImage1)
% estimateGeometricTransform 函数 剔除误匹配 (MSAC算法)
[tform,inlierptsImage2,inlierptsImage1] =estimateGeometricTransform(matchedptsImage2,matchedptsImage1,'similarity','Confidence',99,'MaxDistance',3);
disp('Matched features with MLESAC')
size(inlierptsImage2)
% 显示没有误匹配的情况
subplot(2,1,2)
showMatchedFeatures_fgy(I11,I22,inlierptsImage1,inlierptsImage2,'montage');
str=sprintf('Matched inlier points\n(excluding outliers)');
title(str,'fontname','Times New Roman','FontSize',12);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 显示匹配图像(有一定间距) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% N 是两幅图像中间空出的像素距离
N=500;
I1=I11;I2=I22;
I = zeros([size(I1,1) size(I1,2)*2+N size(I1,3)]);
I(:,1:size(I1,2),:)=I1;
I(:,size(I1,2)+1:size(I1,2)+size(I2,2)+N,:)=255;
I(:,size(I1,2)+1+N:size(I1,2)+size(I2,2)+N,:)=I2;
figure, imshow(I/255); hold on;
n_matched_features=size(inlierptsImage1);
% 显示匹配点
for i=1:n_matched_features
% 画点
% plot([inlierptsImage1.Location(i,1) inlierptsImage2.Location(i,1)+size(I1,2)+N],[inlierptsImage1.Location(i,2) inlierptsImage2.Location(i,2)],'o','Color','b')
plot(inlierptsImage1.Location(i,1),inlierptsImage1.Location(i,2),'o','Color','b', 'MarkerSize',9,'LineWidth',1.5)
plot(inlierptsImage2.Location(i,1)+size(I1,2)+N,inlierptsImage2.Location(i,2),'+','Color','g', 'MarkerSize',9,'LineWidth',1.5)
% 画线
plot([inlierptsImage1.Location(i,1) inlierptsImage2.Location(i,1)+size(I1,2)+N],[inlierptsImage1.Location(i,2) inlierptsImage2.Location(i,2)],'-','Color','y','LineWidth',1.5)
end