1.软件版本
matlab2021a
2.核心代码
clear,clc
I1 = rgb2gray(imread('fetch.JPG'));
I2 = rgb2gray(imread('fetch1.JPG'));
% I1=imresize(I1,[290,290]);
% I2=imresize(I2,[290,290]);
% Get the Key Points
Options.upright=true;
Options.tresh=0.0001;
Ipts1=OpenSurf(I1,Options);
Ipts2=OpenSurf(I2,Options);
% Put the landmark descriptors in a matrix
DA = reshape([Ipts1.descriptor],64,[]);
DB = reshape([Ipts2.descriptor],64,[]);
% Find the best matches
for i=1:length(Ipts1)
for j=1:length(Ipts2)
DC(i,j)=norm(DA(:,i)-DB(:,j));
end
end
DM=[];
for i=1:length(Ipts1)
[B,IX] = sort(DC(i,:));
% [B,IX] = sort(DC1(i,:));
% if B(1)/B(2)<0.8
if min(DC(:,IX(1)))==DC(i,IX(1))
DM=[DM;[i,IX(1)]];
end
end
% Show both images
I = zeros([size(I1,1) size(I1,2)*2 size(I1,3)]);
I(:,1:size(I1,2),:)=I1; I(:,size(I1,2)+1:size(I1,2)+size(I2,2),:)=I2;
figure, imshow(I/255); hold on;
X=zeros(length(DM),2);
Y=zeros(length(DM),2);
% Show the best matches
for i=1:length(DM),
% c=rand(1,3);
% plot([Ipts1(cor1(i)).x Ipts2(cor2(i)).x+size(I1,2)],[Ipts1(cor1(i)).y Ipts2(cor2(i)).y],'-','Color',c)
% plot([Ipts1(cor1(i)).x Ipts2(cor2(i)).x+size(I1,2)],[Ipts1(cor1(i)).y Ipts2(cor2(i)).y],'o','Color',c)
X(i,:)=[Ipts1(DM(i,1)).x ,Ipts1(DM(i,1)).y];
Y(i,:)=[Ipts2(DM(i,2)).x ,Ipts2(DM(i,2)).y];
plot(Ipts1(DM(i,1)).x ,Ipts1(DM(i,1)).y,'go')
plot(Ipts2(DM(i,2)).x+size(I1,2) ,Ipts2(DM(i,2)).y,'bo')
plot([Ipts1(DM(i,1)).x Ipts2(DM(i,2)).x+size(I1,2)],[Ipts1(DM(i,1)).y Ipts2(DM(i,2)).y],'r-')
end
K=3;
Iteration=30;
[AP1,AP2,Q1,Q2,Q11,Q22]=Graph_TMatch(X,Y,K);
figure, imshow(I/255); hold on;
W1=size(I1,2);
S1 = sparse(AP1);
[C11,C12,C13] = find(S1);
for i=1:length(C11)
hold on;
plot([Q1(C11(i),1),Q1(C12(i),1)],[Q1(C11(i),2),Q1(C12(i),2)],'-r');
end
S2 = sparse(AP2);
[C21,C22,C23] = find(S2);
for i=1:length(C21)
hold on;
plot([Q2(C21(i),1)+W1,Q2(C22(i),1)+W1],[Q2(C21(i),2),Q2(C22(i),2)],'-r');
end
figure, imshow(I/255);
hold on;
plot(Q11(:,1),Q11(:,2),'b+');
plot(Q22(:,1)+W1,Q22(:,2),'g+');
for i=1:length(Q11)
hold on;
plot([Q11(i,1),Q22(i,1)+W1],[Q11(i,2),Q22(i,2)],'-r');
end
hold off;
Pos1=[Q11(:,2),Q11(:,1)];
Pos2=[Q22(:,2),Q22(:,1)];
% Calculate affine matrix
Pos1(:,3)=1; Pos2(:,3)=1;
M=Pos1'/Pos2';
% Add subfunctions to Matlab Search path
functionname='OpenSurf.m';
functiondir=which(functionname);
functiondir=functiondir(1:end-length(functionname));
addpath([functiondir '/WarpFunctions'])
% Warp the image
I1_warped=affine_warp(I1,M,'bicubic');
% Show the result
figure,
subplot(1,4,1), imshow(I1);title('Figure 1');
subplot(1,4,2), imshow(I2);title('Figure 2');
subplot(1,4,3), imshow(I1_warped,[]);title('Warped Figure 1');
subplot(1,4,4), imshow(I1_warped+double(I2),[]);title('Warped Figure 1');
Pos1=[Q11(:,2)+0.5*size(I1,2),Q11(:,1)+0.5*size(I1,1)];
Pos2=[Q22(:,2)+0.5*size(I2,2),Q22(:,1)+0.5*size(I2,1)];
% Calculate affine matrix
Pos1(:,3)=1; Pos2(:,3)=1;
M=Pos1'/Pos2';
I11=zeros(2*size(I1,1),2*size(I1,1));
I22=zeros(2*size(I2,1),2*size(I2,1));
I11(0.5*size(I1,1)+1:1.5*size(I1,1),0.5*size(I1,2)+1:1.5*size(I1,2))=I1;
I22(0.5*size(I2,1)+1:1.5*size(I2,1),0.5*size(I2,2)+1:1.5*size(I2,2))=I2;
% M(1:2,3)=M(1:2,3)+0.5*size(I1,1);
% Warp the image
I1_warped=affine_warp(I11,M,'bicubic');
% Show the result
figure,
subplot(1,4,1), imshow(I11,[]);title('Figure 1');
subplot(1,4,2), imshow(I22,[]);title('Figure 2');
subplot(1,4,3), imshow(I1_warped,[]);title('Warped Figure 1');
subplot(1,4,4), imshow(I1_warped+I22,[]);title('Warped Figure 2');
3.操作步骤与仿真结论
D209
标签:SURF,DM,I1,I2,GTM,Ipts1,Ipts2,图像匹配,size From: https://www.cnblogs.com/matlabfpga/p/16980951.html