基于一组匹配对计算仿射或刚性变换

时间:2015-08-17 05:20:57

标签: matlab math image-processing

我在一个图像中有一组200点(x1,y1)与其他200点(x2,y2)匹配。如何找到将(x1,y1)转移到(x2,y2)的最佳变换(仿射或刚性)? MATLAB中的任何函数? 感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用MATLAB' mldivide或反斜杠" \ "运算符使用其最小二乘实现来求解方程组。以下是一种简单的方法。

function affinefit

    % Define number of points and a test affine transformation to estimate 
    n = 200;
    A = [0, -1; 1, 0]; % 90 degree rotation about z-axis
    b = [2; 4]; % Offset

    % Generate initial and transformed points points
    xy1 = rand(2, n);
    xy2 = A * xy1 + repmat(b, 1, n);

    % Perform least squares fit on A(1,1), A(1,2), and b(1)
    Ab1 = [xy1', ones(n, 1)] \ xy2(1,:)';

    % Perform least squares fit on A(2,1), A(2,2), and b(2)
    Ab2 = [xy1', ones(n, 1)] \ xy2(2,:)';

    % Extract least squares estimate
    Ae = [Ab1(1:2)'; Ab2(1:2)'];
    be = [Ab1(3); Ab2(3)];

    % Print results
    fprintf('Truth Transformation\n');
    fprintf('A =\n'); disp(A);
    fprintf('b =\n'); disp(b);

    fprintf('Estimated Transformation\n');
    fprintf('A =\n'); disp(Ae);
    fprintf('b =\n'); disp(be);

end

具有以下输出

>> affinefit
Truth Transformation
A =
     0    -1
     1     0

b =
     2
     4

Estimated Transformation
A =
    0.0000   -1.0000
    1.0000   -0.0000

b =
    2.0000
    4.0000

请注意,还有其他一些事项需要注意,例如异常值,在这种情况下,您可能希望将其与异常值检测器(例如RANSAC)结合使用。