title: OpenCV findHomography Issue
body: <p>I am working on a Panography / Panorama application in OpenCV and I've run into a problem I really can't figure out. For an idea of what a panorama photograph looks like, have a look here at the Panography Wikipedia article: <a href="http://en.wikipedia.org/wiki/Panography" rel="noreferrer">http://en.wikipedia.org/wiki/Panography</a></p>
  
  <p>So far, I can take multiple images, and stitch them together while making any image I like a reference image; here's a little taster of what I mean.</p>
  
  <p><img src="https://i.stack.imgur.com/U4wqT.jpg" alt="An example Panography image I&#39;ve created"></p>
  
  <p>However, as you can see - it has a lot of issues. The primary one I am facing is that the images are getting cut (re: far right image, top of images). To highlight why this is happening, I'll draw the points that have been matched, and draw lines for where the transformation will end up:</p>
  
  <p><img src="https://i.stack.imgur.com/gAkbp.jpg" alt="The image matches"></p>
  
  <p>Where the left image is the reference image, and the right image is the image after it's been translated (original below) - I have drawn the green lines to highlight the image. The image has the following corner points: </p>
  
  <pre><code>TL: [234.759, -117.696]
  TR: [852.226, -38.9487]
  BR: [764.368, 374.84]
  BL: [176.381, 259.953]
  </code></pre>
  
  <p>So the main problem I have is that after the perspective has been changed the image: </p>
  
  <p><img src="https://i.stack.imgur.com/HO6xK.png" alt="Original Image"></p>
  
  <p>Suffers losses like so: </p>
  
  <p><img src="https://i.stack.imgur.com/Xn1oV.png" alt="Cut up image"></p>
  
  <p>Now enough images, some code.</p>
  
  <p>I'm using a <code>cv::SurfFeatureDetector</code>, <code>cv::SurfDescriptorExtractor</code> and <code>cv::FlannBasedMatcher</code> to get all of those points, and I calculate the matches and more importantly good matches by doing the following: </p>
  
  <pre><code>/* calculate the matches */
  for(int i = 0; i &lt; descriptors_thisImage.rows; i++) {
      double dist = matches[i].distance;
      if(dist &lt; min_dist) min_dist = dist;
      if(dist &gt; max_dist) max_dist = dist;
  }
  
  /* calculate the good matches */
  for(int i = 0; i &lt; descriptors_thisImage.rows; i++) {
      if(matches[i].distance &lt; 3*min_dist) {
          good_matches.push_back(matches[i]);
      }
  }
  </code></pre>
  
  <p>This is pretty standard, and to do this I followed the tutorial found here: <a href="http://opencv.itseez.com/trunk/doc/tutorials/features2d/feature_homography/feature_homography.html" rel="noreferrer">http://opencv.itseez.com/trunk/doc/tutorials/features2d/feature_homography/feature_homography.html</a></p>
  
  <p>To copy images atop of one another, I use the following method (where <code>img1</code> and <code>img2</code> are <code>std::vector&lt; cv::Point2f &gt;</code>)</p>
  
  <pre><code>/* set the keypoints from the good matches */
  for( int i = 0; i &lt; good_matches.size(); i++ ) {
      img1.push_back( keypoints_thisImage[ good_matches[i].queryIdx ].pt );
      img2.push_back( keypoints_referenceImage[ good_matches[i].trainIdx ].pt );
  }
  
  /* calculate the homography */
  cv::Mat H = cv::findHomography(cv::Mat(img1), cv::Mat(img2), CV_RANSAC);
  
  /* warp the image */
  cv::warpPerspective(thisImage, thisTransformed, H, cv::Size(thisImage.cols * 2, thisImage.rows * 2), cv::INTER_CUBIC );
  
  /* place the contents of thisImage in gsThisImage */
  thisImage.copyTo(gsThisImage);
  
  /* set the values of gsThisImage to 255 */
  for(int i = 0; i &lt; gsThisImage.rows; i++) {
      cv::Vec3b *p = gsThisImage.ptr&lt;cv::Vec3b&gt;(i);
      for(int j = 0; j &lt; gsThisImage.cols; j++) {
          for( int grb=0; grb &lt; 3; grb++ ) {
              p[j][grb] = cv::saturate_cast&lt;uchar&gt;( 255.0f );
          }
      }
  }
  
  /* convert the colour to greyscale */
  cv::cvtColor(gsThisImage, gsThisImage, CV_BGR2GRAY);
  
  /* warp the greyscale image to create an image mask */
  cv::warpPerspective(gsThisImage, thisMask, H, cv::Size(thisImage.cols * 2, thisImage.rows * 2), cv::INTER_CUBIC );
  
  /* stitch the transformed image to the reference image */
  thisTransformed.copyTo(referenceImage, thisMask);
  </code></pre>
  
  <p>So, I have the coordinates of where the warped image is going to end up, I have the points that create the homogeneous matrix that's used for these transformations - but I can't figure out how I should go about translating these images so they can't get cut up. Any help or pointers are very appreciated! </p>