getfundamentalmatrix.m

来自「这是我用Delphi和Matlab写的一个程序」· M 代码 · 共 46 行

M
46
字号
function [F, e0, e1] = GetFundamentalMatrix(x0, x1)
%  FUNDAMENTAL_MATRIX  Return the fundamental matrix and epipolars
%  x0: 左图像的特征点。它是一个3×N的矩阵,矩阵的每一行为特征点的像素坐标(x, y, 1)'
%  x1: 右图像的特征点。它是一个3×N的矩阵,矩阵的每一行为特征点的像素坐标(x, y, 1)'
%  左右矩阵对应的一行为一对匹配点
%  F: 基本矩阵,e0: 左图像的极点,e1: 右图像的极点
%
%  刘然: liuran781101@tom.com

%  获得N的值
npts = size(x0,2);

%  Normalise each set of points so that the origin
%  is at centroid and mean distance from origin is sqrt(2).
%  normalise2dpts also ensures the scale parameter is 1. (即第三个坐标分量为1)
%  Matlab中的函数的参数都是按地址传递的
[x0, T0] = normalise2dpts(x0);  %  T0为变换矩阵
[x1, T1] = normalise2dpts(x1);  %  T1为变换矩阵

% Build the constraint matrix, 它是一个1×9的矩阵
A = [x1(1,:)'.*x0(1,:)' x1(1,:)'.*x0(2,:)' x1(1,:)' ...
    x1(2,:)'.*x0(1,:)' x1(2,:)'.*x0(2,:)'  x1(2,:)' ...
    x0(1,:)' x0(2,:)' ones(npts,1) ];

[U,D,V] = svd(A, 0); %  Under MATLAB use the economy decomposition

%  Extract fundamental matrix from the column of V corresponding to
%  smallest singular value.
F = reshape(V(:,9),3,3)';

%  Enforce constraint that fundamental matrix has rank 2 by performing
%  a svd and then reconstructing with the two largest singular values.
[U,D,V] = svd(F, 0);  %  再对结果做一次奇异值分解,得到U、D、V三个矩阵
F = U * diag([D(1,1) D(2,2) 0]) * V';  %  Reconstructing F with the two largest singular values

% Denormalise
F = T1' * F * T0;  % 还原成原来的坐标表示下的基本矩阵

if nargout == 3  	% Solve for epipoles, nargout给出的是调用程序时指定的输出宗量的个数
  [U,D,V] = svd(F,0);
  e0 = hnormalise(V(:, 3));  %  让齐次坐标的h等于1
  e1 = hnormalise(U(:, 3));  %  让齐次坐标的h等于1
end


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?