title: OpenCV warpPerspective - how to know destination image size?
body: <p>Ok, I have to admit that I am a novice to OpenCV and that my MATLAB/lin. Algebra knowledge might be introducing a bias. But what I want to do is really simple, while I still did not manage to find an answer.</p>
  
  <p>When trying to rectify an image (or part of an image) under a perspective transformation, you basically perform two steps (assuming you have the 4 points that define the distorted object):</p>
  
  <ol>
  <li>find the transformation between some perfect rectangle and the distorted shape (in OpenCV, via <code>findHomography()</code> or <code>getPerspectiveTransform()</code> - why those two operate differently on the same points is another story, also frustrating); this gives us a matrix T.</li>
  <li>
  apply the inverse of T to the initially distorted shape to transform it into a rectangle (in OpenCV, this is done with <code>warpPerspective()</code>).</li>
  </ol>
  
  <p>Now, this last function (<code>warpPerspective()</code>) asks the user to specify the size of the destination image. </p>
  
  <p>My question is how the users should know beforehand what that size would be. The low-level way of doing it is simply applying the transformation T to the corner points of the image in which the object is found, thus guaranteeing that you don't get out of the bounds with the newly transformed shape.
  However, even if you take the matrix out of T and apply it manually to those points, the result looks weird.</p>
  
  <p>Is there a way to do this in OpenCV? Thanks!</p>
  
  <p>P.S. Below is some code:</p>
  
  <pre><code>float leftX, lowerY, rightX, higherY;   
  
  float minX = std::numeric_limits&lt;float&gt;::max(), maxX = std::numeric_limits&lt;float&gt;::min(), minY = std::numeric_limits&lt;float&gt;::max(), maxY = std::numeric_limits&lt;float&gt;::min();
  
  Mat value, pt;
  for(int i=0; i&lt;4; i++)
  {
      switch(i)
      {
          case 0:
              pt = (Mat_&lt;float&gt;(3, 1) &lt;&lt; 1.00,1.00,1.00);                         
              break;
          case 1:
              pt = (Mat_&lt;float&gt;(3, 1) &lt;&lt; srcIm.cols,1.00,1.00);
              break;
          case 2:
              pt = (Mat_&lt;float&gt;(3, 1) &lt;&lt; 1.00,srcIm.rows,1.00);
              break;
          case 3:
              pt = (Mat_&lt;float&gt;(3, 1) &lt;&lt; srcIm.cols,srcIm.rows,1.00);
              break;
          default:
              cerr &lt;&lt; "Wrong switch." &lt;&lt; endl;
              break;
      }               
      value = invH*pt;    
      value /= value.at&lt;float&gt;(2);        
      minX = min(minX,value.at&lt;float&gt;(0));
      maxX = max(maxX,value.at&lt;float&gt;(0));
      minY = min(minY,value.at&lt;float&gt;(1));
      maxY = max(maxY,value.at&lt;float&gt;(1));
  }
  leftX = std::min&lt;float&gt;(1.00,-minX);
  lowerY = std::min&lt;float&gt;(1.00,-minY);
  rightX = max(srcIm.cols-minX,maxX-minX);
  higherY = max(srcIm.rows-minY,maxY-minY);
  
  warpPerspective(srcIm, dstIm, H, Size(rightX-leftX,higherY-lowerY), cv::INTER_CUBIC);
  </code></pre>
  
  <p>UPDATE: Perhaps my results do not look good because the matrix I'm using is wrong. As I cannot observe what's happening inside <code>getPerspectiveTransform()</code>, I cannot know how this matrix is computed, but it has some very small and very large values, which makes me think they are garbage.
  This is the way I obtain the data from T:</p>
  
  <pre><code>for(int row=0;row&lt;3;row++)
      for(int col=0;col&lt;3;col++)
          T.at&lt;float&gt;(row,col) = ((float*)(H.data + (size_t)H.step*row))[col];
  </code></pre>
  
  <p>(Although the output matrix from <code>getPerspectiveTransform()</code> is 3x3, trying to access its values directly via <code>T.at&lt;float&gt;(row,col)</code> leads to a segmentation fault.)</p>
  
  <p>Is this the right way to do it? Perhaps this is why the original issue arises, because I do not get the correct matrix...</p>