% 试着实现 SFM
clear all;clc;close all;
im1 = rgb2gray(imread(‘E:\三维重建\matlab_3D_reconstruct\1.jpg‘));
im2 = rgb2gray(imread(‘E:\三维重建\matlab_3D_reconstruct\2.jpg‘));
im1 = im1‘;
im2 = im2‘;
[im1, des1, locs1] = siftFunc(im1);
[im2, des2, locs2] = siftFunc(im2);
% showkeys(im1, locs1); % 画图时所处尺度和方向信息是需要的,不能丢弃
% showkeys(im2, locs2);
locs1(:,3:4) = []; % 所处尺度,方向信息扔掉
locs2(:,3:4) = []; % 所处尺度,方向信息扔掉
%% 进行匹配
distRatio = 0.50;
% For each descriptor in the first image, select its match to second image.
des2t = des2‘; % Precompute matrix transpose
for i = 1 : size(des1,1)
dotprods = des1(i,:) * des2t; % Computes vector of dot products
[vals,indx] = sort(acos(dotprods)); % Take inverse cosine and sort results
% Check if nearest neighbor has angle less than distRatio times 2nd.
if (vals(1) < distRatio * vals(2))
match(i) = indx(1);
else
match(i) = 0;
end
end
num = sum(match > 0);
disp(strcat(‘总共有‘,num2str(num),‘个匹配点‘));
% locs1里头的每个des都逐一挑出来,和locs2中的所有点计算距离,如果
% 最大距离和次大距离的比值小于distRatio,则互相匹配,匹配的点的编号
% 存在match里,要剔除那些不匹配的点
locs1_index = find(match>0);
locs2_index = match(locs1_index);
clear des2t distRatio dotprods i indx match vals;
locs1 = locs1(locs1_index,:);
des1 = des1(locs1_index,:);
locs2 = locs2(locs2_index,:);
des2 = des2(locs2_index,:);
clear locs1_index locs2_index;
clear des1 des2; % 后面已经不需要描述子的信息了,可以删除
%% 显示匹配
im3 = appendimages(im1,im2);
% Show a figure with lines joining the accepted matches.
figure(‘Position‘, [100 100 size(im3,2) size(im3,1)]);
colormap(‘gray‘);
imagesc(im3);
hold on;
cols1 = size(im1,2);
for i = 1: num
line([locs1(i,2) locs2(i,2)+cols1], ...
[locs1(i,1) locs2(i,1)], ‘Color‘, ‘c‘);
end
clear im3 cols1 i;
pause(5);clc;close all;
%%
% plot(locs1(:,1),locs1(:,2),‘r+‘);axis equal;
% 第一列是640的方向,即图片竖的方向,第二列是480的方向
homo_locs1 = [locs1‘;
ones(1,size(locs1,1))];
homo_locs2 = [locs2‘;
ones(1,size(locs2,1))];
% [F, e1, e2] = fundmatrix(homo_locs1, homo_locs2);
[F, inliers, e1, e2] = ransacfitfundmatrix(homo_locs1, homo_locs2, 0.0001);
% 极点e1,e2 在拍完照片后就已经确定了,是可以算出来的
% ransac是在不考虑极线几何的情况下做的数字式的误差剔除
% 知乎的回答里似乎没有用极线几何剔除错配点,认为ransac已经剔除了错配点
2016/05/29 卡在摄像机自标定了,调用了http://www.peterkovesi.com/matlabfns/index.html里头的一些函数,还调用了一个siftwin32.exe程序