title: Computing camera pose with homography matrix based on 4 coplanar points
body: <p>I have 4 coplanar points in a video (or image) representing a quad (not necessarily a square or rectangle) and I would like to be able to display a virtual cube on top of them where the corners of the cube stand exactly on the corners of the video quad.</p>
  
  <p>Since the points are coplanar I can compute the homography between the corners of a unit square (i.e. [0,0] [0,1] [1,0] [1,1]) and the video coordinates of the quad. </p>
  
  <p>From this homography I should be able to compute a correct camera pose, i.e. [R|t] where R is a 3x3 rotation matrix and t is a 3x1 translation vector so that the virtual cube lies on the video quad.</p>
  
  <p>I have read many solutions (some of them on SO) and tried implementing them but they seem to work only in some "simple" cases (like when the video quad is a square) but do not work in most cases.</p>
  
  <p>Here are the methods I tried (most of them are based on the same principles, only the computation of the translation are slightly different). Let K be the intrinsics matrix from the camera and H be the homography. We compute:</p>
  
  <pre><code>A = K-1 * H
  </code></pre>
  
  <p>Let a1,a2,a3 be the column vectors of A and r1,r2,r3 the column vectors of the rotation matrix R.</p>
  
  <pre><code>r1 = a1 / ||a1||
  r2 = a2 / ||a2||
  r3 = r1 x r2
  t = a3 / sqrt(||a1||*||a2||)
  </code></pre>
  
  <p>The issue is that this does not work in most cases. In order to check my results, I compared R and t with those obtained by OpenCV's solvePnP method (using the following 3D points [0,0,0] [0,1,0] [1,0,0] [1,1,0]).</p>
  
  <p>Since I display the cube in the same way, I noticed that in every case solvePnP provides correct results, while the pose obtained from the homography is mostly wrong.</p>
  
  <p>In theory since my points are coplanar, it is possible to compute the pose from a homography but I couldn't find the correct way to compute the pose from H.</p>
  
  <p>Any insights on what I am doing wrong?</p>
  
  <p><strong>Edit after trying @Jav_Rock's method</strong></p>
  
  <p>Hi Jav_Rock, thanks very much for your answer, I tried your approach (and many others as well) which seems to be more or less OK.
  Nevertheless I still happen to have some issues when computing the pose based on 4 coplanar point. In order to check the results I compare with results from solvePnP (which will be much better due to the iterative reprojection error minimization approach).</p>
  
  <p>Here is an example: </p>
  
  <p><img src="https://i.stack.imgur.com/mCvLM.png" alt="cube"></p>
  
  <ul>
  <li>Yellow cube: Solve PNP </li>
  <li>Black Cube: Jav_Rock's technique </li>
  <li>Cyan (and Purple) cube(s): some other techniques given the exact same results</li>
  </ul>
  
  <p>As you can see, the black cube is more or less OK but doesn't seem well proportioned, although the vectors seem orthonormal.</p>
  
  <p><strong>EDIT2:</strong> I normalized v3 after computing it (in order to enforce orthonormality) and it seems to solve some problems as well. </p>