# inverse mapping

I'm trying to understand how to find the coefficients a-h for inverse mapping, my goal is to program projective transform from scratch.

let assume i have user input 4 coords entered clockwise onto imageA and i want to map imageB. imageA is 300X300 and imageA is 256X256.

function [ result ] = findCoefficients( imageA, imageB )
% finds coefficients for inverse mapping algorithem
%   takes 2 X 2d vectors each consists of 4 points x,y
%   and returns the coef accroding to reverse mapping function
%
% x y 0 0 1 0 -xx' -yx'
% 0 0 x y 0 1 -xy' -yy'
%                       y' and x' are in the destination picture;

A = [imageB(1,1) imageB(2,1) 0 0 1 0 -imageB(1,1)*imageA(1,1) -imageB(2,1)*imageA(1,1);
0 0 imageB(1,1) imageB(2,1) 0 1 -imageB(1,1)*imageA(2,1) -imageB(2,1)*imageA(2,1);
imageB(1,2) imageB(2,2) 0 0 1 0 -imageB(1,2)*imageA(1,2) -imageB(2,2)*imageA(1,2);
0 0 imageB(1,2) imageB(2,2) 0 1 -imageB(1,2)*imageA(2,2) -imageB(2,2)*imageA(2,2);
imageB(1,3) imageB(2,3) 0 0 1 0 -imageB(1,3)*imageA(1,3) -imageB(2,3)*imageA(1,3);
0 0 imageB(1,3) imageB(2,3) 0 1 -imageB(1,3)*imageA(2,3) -imageB(2,3)*imageA(2,3);
imageB(1,4) imageB(2,4) 0 0 1 0 -imageB(1,4)*imageA(1,4) -imageB(2,4)*imageA(1,4);
0 0 imageB(1,4) imageB(2,4) 0 1 -imageB(1,4)*imageA(2,4) -imageB(2,4)*imageA(2,4)];
B = [imageA(1,1); imageA(2,1); imageA(1,2); imageA(2,2); imageA(1,3); imageA(2,3); imageA(1,4); imageA(2,4)];

result = pinv(A)*B;
end


i'm getting a strange result it maps the photo however no into the bounding square i have chosen

I think you have error in your matrix $A$. According to this derivation, the matrix should be:

$$A=\begin{pmatrix} x & y & 1 & 0 & 0 & 0 & -x' y & -x' y \\ 0 & 0 & 0 & x & y & 1 & -y' x & - y' y \\ & & & & \vdots \end{pmatrix}$$

Maybe your matrix is OK, but it depends on how your point vector is organized. In either case, the problem is probably in organization.

Please note that $A$ can easily get singular or near-singular. Solving $Ah=b$ for $h$ using direct inverse of $A$ is numerically quite unstable. You can use either QR decomposition or SVD to solve the linear system.

Even better method is a normalized DLT (Direct Linear Transform) which work with homogenous coordinates and the normalization step works as preconditioning to the above linear system. This is much more stable solution described in Hartley & Zisserman's book (p. 88, section 4.1).

• thanks for the answer, what you showed me is perspective tranform, i'm trying to use projective transform so the matrix looks a little different. i saw a solution using SVD. but isn't the whole idea of inverse mapping like this not to use SVD? – Gilad Dec 3 '12 at 6:57
• SVD is here just a method for solving linear system - it has nothing to do with mapping. – Libor Dec 3 '12 at 17:48
• I am pretty confident about that matrix form (perspective transform is just an 8-parameter homography or projective transform) since I have used it and studied in H&Z book. Your matrix mixes unrelated parameters. Of course you can use $A^{-1}$ but this works only if you are sure that your mapping is exactly defined (there are many so called degenerate cases which can be determined with singular values). – Libor Dec 3 '12 at 17:54
• @Libor, I have a hard time understanding why normalizing the data before applying DLT, I read Hartley's book, but still can't understand the math behind it, I post a question dsp.stackexchange.com/q/10887/4811, hope you can help me with it. – avocado Sep 28 '13 at 10:49
• @loganecolss I have answered the question you linked to the best of my knowledge. For more information, please refer to the H&Z book (they give a simple explanation) or the paper I have linked in the answer. – Libor Sep 29 '13 at 18:34