title: Image copied in ROI doesn't follow camera c++. How to fix this?
body: <p>I work on Windows7 x64 with opencv and Visual Studio 2010 on c++ language.</p>
  <p>I created a project in which I show to my camera a rectangular area (call <em><strong>squared_surface</strong></em>). This area is recognized by tracing a rectangle with <code>findSquare ()</code> and <code>drawSquares ()</code> of opencv file <code>squares.cpp</code>.
  On this rectangle I create a ROI and there I copy an image (let's call <em><strong>copied_image)</strong></em></p>
  <p>My problem is that when I rotate <em><strong>squared_surface</strong></em> (in front of camera), <em><strong>copied_image</strong></em> does not follow it.</p>
  <p>I think I need to use the functions <code>getPerpective ()</code> and <code> warpPerspective ()</code>, but I do not know how. Can anyone help me?</p>
  <p>Here's the code:</p>
  <pre><code>int main(){
   vector&lt;vector&lt;Point&gt; &gt; squares;
  
   cv::VideoCapture cap(0);
   for (;;) {
      cv::Mat image;
      cap &gt;&gt; image;
      findSquares(image, squares);
      for (size_t i = 0; i &lt; squares.size(); i++) {
      Rect rectangle = boundingRect(Mat(squares[i]));
                  
      if((rectangle.width&lt;=630)&amp;&amp; (rectangle.width &gt;= 420) &amp;&amp; (rectangle.height&lt;= 490) &amp;&amp;(rectangle.height &gt;= 250 )) {
          cv::Size dsize = Size(rectangle.width, rectangle.height);
          Mat img1 = imread(&quot;scacchiera.jpg&quot;);
      cv::resize(img1,img1,dsize,0,0, INTER_LINEAR);
          Rect roi (rectangle.x, rectangle.y,rectangle.width, rectangle.height);
          
      Mat imageRoi(image, roi);
         img1.copyTo(imageRoi);
         }
      } 
    drawSquares(image, squares);
    imshow(&quot;camera&quot;,image);
    if(waitKey(30) &gt;= 0) break;
  }
  return 0;
  }
  </code></pre>
  <p><img src="https://i.stack.imgur.com/QD8Wx.jpg" alt="enter image description here" />
  <img src="https://i.stack.imgur.com/k8Qaq.jpg" alt="enter image description here" /></p>
  <p>Thanks!</p>
  <p><strong>EDIT.</strong>
  I was thinking of rotating <strong>Copied_image</strong>, so it follows <strong>Squared_surface</strong>, but I need to calculate the angle of rotation of the rectangle identified by the camera (drawn in red in the above images). Is there a way to calculate this angle?</p>
  <p>Or how can I do so that <strong>Copied_image</strong> follows <strong>Squared_surface</strong> when I rotate squared_surface?</p>
  <p>Help me, please!</p>