I have read Finding a 3D transformation matrix based on the 2D coordinates but I think my situation is different because I think I need a 4x3 matrix, not a 3x3 matrix. I'm not sure but this might be because I have rotation and translation in addition to just the perspective transformation.
Here is the setup:
suppose you have several 2D points in an image: (x1,y1) (x2,y2) (x3,y3) (x4,y4)
suppose you also have several corresponding 3D points on an arbitrary plane: (X1,Y1,Z1) (X2,Y2,Z2) (X3,Y3,Z3) (X4,Y4,Z4)
to transform from 2D to 3D using homogenous coordinates, we can use
(X,Y,Z,W) = M*(x,y,1). Here M must be a 4x3 matrix
So a 2D-homogenousCoords point gets transformed into a 3D-homogenousCoords point.
Then, I could divide (X,Y,Z,W) by W to get (X,Y,Z,1), which is a form that I can read out the true X,Y,Z values in "regular" coordinates.
Now, here is a problem. I don't know what is W for any of my (X,Y,Z) points. (If I did know each point's W, I think there are standard linear algebra way for finding M.)
So to find M, I multiply things out like the following:
X = M11*x + M12*y + M13*1
Y = M21*x + M22*y + M23*1
Z = M31*x + M32*y + M33*1
W = M41*x + M42*y + M43*1
but these X,Y,Z,W are the homogenous coords, so to get the "real" X,Y,Z coords:
X = (M11*x + M12*y + M13*1) / (M41*x + M42*y + M43*1)
Y = (M21*x + M22*y + M23*1) / (M41*x + M42*y + M43*1)
Z = (M31*x + M32*y + M33*1) / (M41*x + M42*y + M43*1)
also, I can get rid of one parameter from each equation by multiplying each equation by (1/M43)/(1/M43). Then I can also rename the ratio of parameters. I'm left with:
X = (a1*x + a2*y + a3*1) / (a10*x + a11*y + 1)
Y = (a4*x + a5*y + a6*1) / (a10*x + a11*y + 1)
Z = (a7*x + a8*y + a9*1) / (a10*x + a11*y + 1)
finally I plug in all the (X,Y,Z) and (x,y,z) values that I have into multiple instances of these equations and algebraically re-arrange everything to get the classic A=Bx form, where x is vector of unknown a's (a1 ... a11).
Once I have a1 through a11, I could go back and work out what the original components of M were. Either way I can now project points from 2D to 3D using perspective transformation even if there is rotation or translation.
My question is whether this is the best way to find this kinds of general 2D to 3D perspective transformation?