# 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}$$
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.
 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? – Androidy 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