title: Calculate a 2D homogeneous perspective transformation matrix from 4 points in MATLAB
body: <p>I've got coordinates of 4 points in 2D that form a rectangle and their coordinates after a perspective transformation has been applied.</p>
  
  <p><a href="https://i.stack.imgur.com/ejL28.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ejL28.png" alt="enter image description here"></a></p>
  
  <p>The perspective transformation is calculated in homogeneous coordinates and defined by a 3x3 matrix <code>M</code>. If the matrix is not known, how can I calculate it from the given points?</p>
  
  <p>The calculation for one point would be:</p>
  
  <pre><code>| M11 M12 M13 |   | P1.x |   | w*P1'.x |
  | M21 M22 M23 | * | P1.y | = | w*P1'.y |
  | M31 M32 M33 |   | 1    |   | w*1     |
  </code></pre>
  
  <p>To calculate all points simultaneously I write them together in one matrix <code>A</code> and analogously for the transformed points in a matrix <code>B</code>: </p>
  
  <pre><code>    | P1.x P2.x P3.x P4.x |
  A = | P1.y P2.y P3.y P4.y |
      | 1    1    1    1    |
  </code></pre>
  
  <p>So the equation is <code>M*A=B</code> and this can be solved for <code>M</code> in MATLAB by <code>M = B/A</code> or <code>M = (A'\B')'</code>.</p>
  
  <p>But it's not that easy. I know the coordinates of the points after transformation, but I don't know the exact <code>B</code>, because there is the factor <code>w</code> and it's not necessary 1 after a homogeneous transformation. Cause in homogeneous coordinates every multiple of a vector is the same point and I don't know which multiple I'll get.</p>
  
  <p>To take account of these unknown factors I write the equation as <code>M*A=B*W</code>
  where <code>W</code> is a diagonal matrix with the factors w1...w4 for every point in <code>B</code> on the diagonal. So <code>A</code> and <code>B</code> are now completely known and I have to solve this equation for <code>M</code> and <code>W</code>.</p>
  
  <p>If I could rearrange the equation into the form <code>x*A=B</code> or <code>A*x=B</code> where <code>x</code> would be something like <code>M*W</code> I could solve it and knowing the solution for <code>M*W</code> would maybe be enough already. However despite trying every possible rearrangement I didn't managed to do so. Until it hit me that encapsulating <code>(M*W)</code> would not be possible, since one is a 3x3 matrix and the other a 4x4 matrix. And here I'm stuck.</p>
  
  <p>Also <code>M*A=B*W</code> does not have a single solution for <code>M</code>, because every multiple of <code>M</code> is the same transformation. Writing this as a system of linear equations one could simply fix one of the entries of <code>M</code> to get a single solution. Furthermore there might be inputs that have no solution for <code>M</code> at all, but let's not worry about this for now.</p>
  
  <p>What I'm actually trying to achieve is some kind of vector graphics editing program where the user can drag the corners of a shape's bounding box to transform it, while internally the transformation matrix is calculated.</p>
  
  <p>And actually I need this in JavaScript, but if I can't even solve this in MATLAB I'm completely stuck.</p>