Monday, June 30, 2008

symmetric affine transformations

A common task in computer vision problems is fitting an affine transformation to a set of point correspondences. The transformation is uniquely determined by 3 correspondences for 2D points or 4 correspondences for 3D points, and can be obtained by solving a linear system of equations.

Often, however, we have more correspondences. The usual solution is then to minimize the sum of squared distances between each point in one set and its transformed corresponding point in the other set. The problem with this approach is that you get different answers if the point sets trade roles. So, if we have point sets P and Q, the affine transformation APQ that transforms P to Q will in general not be the inverse of the affine transformation AQP that transforms Q to P.

(Note that sometimes, this makes sense. If one of your point sets is a ground-truth "model" and the other is an "observation", it is reasonable to minimize the squared distances between observation points and transformed model points.)

Is there an alternative? There is, and it's based on a technique called total least squares. I'll write more about this later, but for now, here's some Matlab code that solves the problem for sets of 2D points:

function [a_pq,a_qp] = symmetric_affine(p,q)
% p and q are both 2-by-n matrices
% a_pq and a_qp will be 2-by-3 matrices

n = size(p,2);
x = [p; q];
c = mean(x,2);
x = x - c*ones(1,n);
[v d] = eigs(x*x',2,0);
a = [v; -c'*v]';

a_pq = [-a(:,[3 4])\a(:,[1 2 5])];
a_qp = [-a(:,[1 2])\a(:,[3 4 5])];