0

@ Iwillnotexist Idonotexist presented his code for image perspective transformation (rotations around 3 axes): link

I'm looking for a function (or math) to make an inverse perspective transformation.

Let's make an assumption, that my "input image" is a result of his warpImage() function, and all angles (theta, phi and gamma), scale and fovy are also known.

I'm looking for a function (or math) to compute inverse transformation (black border doesn't matter) to get an primary image.

How can I do this?

Community
  • 1
  • 1
paws
  • 197
  • 1
  • 2
  • 13

2 Answers2

0

The basic idea is you need to find the inverse transformation. In the linked question they have F = P T R1 R2 where P is the projective transformation, T is a translation, and R1, R2 are two rotations.

Denote F* as the inverse transformation. We can the inverse as F* = R2* R1* T* P*. Note the order changes. Three of these are easy R1* is just another rotation but with the angle negated. So the first inverse rotation would be

        cos th    sin th    0    0
R1* =  -sin th    cos th    0    0
            0        0      1    0
            0               0    1

Note the signs on the two sin terms are reversed.

The inverse of a translation is just a translation in the opposite direction.

       1       0      0      0
T*=    0       1      0      0
       0       0      1      h
       0       0      0      1

You can check these calculating T* T which should be the identity matrix.

The trickiest bit is the projective component we have

      cot(fv/2)    0             0               0
  P =   0       cot(fv/2)        0               0
        0           0      -(f+n)/(f-n)   -2 f n / (f-n) 
        0           0           -1               0

The inverse of this is

      tan(fv/2)    0             0               0
  P*=   0       tan(fv/2)        0               0
        0           0            0              -2 
        0           0        (n-f)/(f n)    (f+n)/(f n)

Wolfram alpha inverse with v=fv

You then need to multiply these together in the reverse order to get the final matrix.

Salix alba
  • 7,536
  • 2
  • 32
  • 38
  • Thanks a lot, but... It doesn't work... I done everything as you wrote, but unfortunately it works only for rotations around the first axis ("perpendicular to the screen"). When I try to rotate input image around any of two else axes (even for 1 degree), the result image is empty... I calculate inverse matrices R1, R2, R3 (i changed sinus to -sinus), T (i changed -h to h) and P as you wrote... Maybe i don't understand what is "fv" and "v" in your (and in Wolfram Alpha) examples...? – paws Oct 13 '15 at 11:14
  • Ah there was a typo. It should have been `f n` not `f v`. I've amended my answer and undated to Wolfram link. – Salix alba Oct 13 '15 at 13:24
  • Thanks, but... still nothing... I think, that should be also "2 * f * n" instead of "f * n" in the denominator in P(3,2) and P(3,3), shouldn't it? But regardless of it, the transformed image is a little bit weird... Whole image is gray - when I transorm a chessboard image, every pixel in the transformed image has the value 128, so it looks that it is mean value... I don't have any idea, what I do wrong... It would be great if you or someone would take a look of my code, but I'm not sure where may I put it... (there is too less space in this comment block)... – paws Oct 13 '15 at 14:06
0

I also had issues to back-transform my image. You need to store the points

ptsInPt2f   and   ptsOutPt2f

which are computed in the the 'warpMatrix' method. To back-transform, simply use the same method M = getPerspectiveTransform(ptsOutPt2f, ptsInPt2f); but with reversed param order (output as first argument, input as second). Afterwards a simple crop will get rid of all the black.