title: OpenCV Homography, Transform a point, what is this code doing?
body: <p>I'm working with a homography calculated by OpenCV. I currently use this homography to transform points using the function below. This function performs the task I require however I have no clue how it actually works.</p>
  
  <p>Can anyone explain, line by line exactly, the logic/theory behind the last 3 lines of code, I understand that this transforms the point x,y but I'm unclear as to why this works:</p>
  
  <p>Why are <code>Z</code>, <code>px</code> and <code>py</code> calculated in this way, what do the elements in <code>h</code> correspond to?</p>
  
  <p>Your comments are greatly appreciated :)</p>
  
  <pre><code>double h[9];
  homography = cvMat(3, 3, CV_64F, h);
  CvMat ps1 = cvMat(MAX_CALIB_POINTS/2,2,CV_32FC1, points1);
  CvMat ps2 = cvMat(MAX_CALIB_POINTS/2,2,CV_32FC1, points2);
  
  cvFindHomography(&amp;ps1, &amp;ps2, &amp;homography, 0);
  
  ...
  
  // This is the part I don't fully understand
  double x = 10.0;
  double y = 10.0;
  double Z = 1./(h[6]*x + h[7]*y + h[8]);
  px = (int)((h[0]*x + h[1]*y + h[2])*Z);
  py = (int)((h[3]*x + h[4]*y + h[5])*Z);
  </code></pre>