Suppose you want to find a Homography transform that maps a set of 2D coordinates to another arbitrary set of 2D coordinates. This problem has many applications, such as image registration based on keypoint matching. This post will describe a few steps to make sure the homography estimation is numerically stable and can be performed using 32bit floating point coordinates.

## Mathematical Solution

Firstly, we should formalize the problem definition. Let , be a set of n pairs of 2D coordinates for which there exists a homography transform such that for all . The transform equation can be written in matrix form as below:

where are the coefficients of the homography transform. This equation can be rewritten in matrix form as

This is a linear system in the form , with having 2n rows and 9 columns. To avoid the pitfall of a trivial solution (which would render ), this system can be solved by using the PCA algorithm, as follows.

If we multiply both sides of equation (I) by , we end up with an equation in the form . Note that is a square matrix of size 8×8. Therefore, it is possible to compute its eigenvalues and eigenvectors. The final value of is the eigenvector of with the lowest eigenvalue. Note that we may need to normalize the final solution by dividing all elements of by its eighth element.

## Numerical Solution

As we have seen, in order to determine the homography parameters, we need to calculate the matrix . It is easy to verify that the largest element of may scale up to quartically with respect to the input coordinates, while its smallest elements scale linearly with respect to the input coordinates.

As an example, consider the first six rows of above and compare them with the seventh row below, which was transposed for clarity:

Thus, it is clear that we may end up with a matrix with wildly different numbers in terms of scale if the input coordinates are not preprocessed. This difference can be large enough for significant numerical imprecisions to arise if we mix them in the same computation, depending on the underlying primitive type used to represent floating point numbers. This, however, is exactly what happens in numerical methods for determining eigenvectors and eigenvalues.

Therefore, we need to cope with this issue if we want to obtain a precise homography estimate. One way to do so would be to use a broader primitive type to represent our matrix (e.g. use double instead of float), but that would only be mitigating the real problem. A better solution is to normalize the input coordinates so that all of them fit in the unit range .

This can be done by selecting a scale parameter such that . Then, we divide all coordinates and by and compute the matrix using these normalized coordinates. Finally, we find the homography parameters for .

Note that this homography maps normalized coordinates to normalized coordinates, i.e. . To find the final homography that maps the unscaled coordinates, we need to perform the following composition:

where corresponds to the function that divides a coordinate by .

## Final thoughts

To illustrate the idea of the discussed method for computing the homography parameters, consider the Matlab/Octave implementation below.

% Input coordinates in homogeneous coordinates p=[ 281.1662 516.9434 484.2327 262.9684 154.7470 136.7685 379.9645 379.7526 1 1 1 1 ]; d=[ 290 490 490 290 159 159 359 359 1 1 1 1 ]; % Scale parameter s=max(max(p(:), d(:))); % Number of pairs of coordinates number_coords = size(p, 2); % Homography transform function H=@(p, h) h*p ./ repmat((h*p)(3,:), 3, 1); % Normalize coordinates p_s = p / s; d_s = d / s; % Build matrix A A=[]; for i=1:number_coords A = [ A; p_s(1,i) p_s(2,i) 1 ... 0 0 0 ... -p_s(1,i)*d_s(1,i) -p_s(2,i)*d_s(1,i) -d_s(1,i); 0 0 0 ... p_s(1,i) p_s(2,i) 1 ... -p_s(1,i)*d_s(2,i) -p_s(2,i)*d_s(2,i) -d_s(2,i) ]; end % Compute eigenvectors and eigenvalues [v, l] = eig(A'*A); % Normalize and convert homography to matrix form h_s = reshape(v(:,1)/v(9,1), [3 3])'; % Represent scale as a matrix S = [ 1/s 0 0 0 1/s 0 0 0 1 ]; S_inv = [ s 0 0 0 s 0 0 0 1 ]; % Compose normalized homography with scale operators h = S_inv * h_s * S; % Test: d ~= H(p,h) abs(H(p,h) - d)