title: OpenCV : the difference between mask and ROI
body: <p>I tried to set a ROI with angle in an image. At first, I thought that using a mask would give me the same result as setting the ROI in my IplImage structure. Then, I would just use cvResize the same way I did when I used ROI, but this time for a mask.</p>
  <p>However, this is obviously not this easy, because of the angle.</p>
  <p>Is there any way to copy the inside of any rectangle, at any angle, into a new IplImage, which would be of the size of this very rectangle?</p>
  <pre class="lang-c prettyprint-override"><code>CvSeq* approximatedContour = cvApproxPoly(currentContour,
                                            sizeof(CvContour),
                                            0,
                                            CV_POLY_APPROX_DP,
                                            8);
  
  // Circonscrire le polygone trouve dans un rectangle
  etiquetteBox = cvMinAreaRect2(approximatedContour);
  
  CvPoint2D32f boxPoints[4];
  CvPoint2D32f* c1 = (&amp;cvPoint2D32f(0,0),
                      &amp;cvPoint2D32f(200,0),
                      &amp;cvPoint2D32f(0,200),
                      &amp;cvPoint2D32f(200,200));
  
  CvMat* mmat = cvCreateMat(3,3,CV_32FC1);
  
  cvBoxPoints(etiquetteBox, boxPoints);
  
  IplImage* mask = cvCreateImage(cvSize(in-&gt;width,in-&gt;height), IPL_DEPTH_8U, 1);
  IplImage* ROIimg = cvCreateImage(cvSize(in-&gt;width,in-&gt;height), IPL_DEPTH_8U, 1);
  drawBox(mask,etiquetteBox,target_color[3]);
  
  cvAnd(thresImg,mask,ROIimg,mask);
  if(voirSeuillage)
      cvCvtColor(ROIimg,in,CV_GRAY2BGR); //ROIimg is OK here!
  
  mmat = cvGetPerspectiveTransform(boxPoints,c1,mmat);
  cvWarpPerspective(ROIimg,thresImgResized,mmat); // here I get a full black image!
  </code></pre>
  <p>Doing this, as kindly suggested by Banthar, I get a full black image instead of what is delimited by boxPoints in ROIimg, what's wrong with this code?</p>
  <h2>After applying answer:</h2>
  <p>Here is what I do now :</p>
  <pre class="lang-c prettyprint-override"><code>double angle = 0.;
  // TODO adaptive angle compensation
  if(abs(etiquetteBox.angle) &gt; 30)
      angle = etiquetteBox.angle + 270.;
  else
      angle = etiquetteBox.angle - 270.;
  
  CvPoint2D32f boxPoints[4];
  CvPoint2D32f c1[] = {cvPoint2D32f(0,0),
                       cvPoint2D32f(20,0),
                       cvPoint2D32f(20,20),
                       cvPoint2D32f(0,20)};
  
  CvMat* mmat = cvCreateMat(3,3,CV_32FC1);
  cvBoxPoints(etiquetteBox, boxPoints);
  Point center = Point(10,10);
  
  //warp the image to fit the polygon into the 20x20 image
  mmat = cvGetPerspectiveTransform(boxPoints,c1,mmat);
  cvWarpPerspective(thresImg,thresImgResized,mmat);
  //rotate the image because the inconsistent angle of etiquetteBox
  // from a frame to the next ...
  //it would be very cool to find a way to fix this...
  CvMat rot_mat = getRotationMatrix2D( center, angle,1.0);
  cvWarpAffine(thresImgResized,rotatedIm,&amp;rot_mat);
  It is still not quite what I want, because the object is rotating into the 20x20 rotatedImg; in thresImgResized, after cvWarpPerspective, the object is well segmented, BUT it is reversed because the inconsistency of the angle of etiquetteBox (-0 degrees in a frame, -90 in the next, depending on how I hold the object to be detected), which I get this way:
  
  cvFindContours(dilImage,
                 contoursStorage,
                 &amp;contours,
                 sizeof(CvContour),
                 CV_RETR_LIST,
                 CV_CHAIN_APPROX_TC89_KCOS);
  
  // Trouver des polygones
  CvSeq* currentContour = contours;
  while(currentContour != 0 &amp;&amp; !etiquette)
  {
      CvSeq* approximatedContour = cvApproxPoly(currentContour,
                                                sizeof(CvContour),
                                                0,
                                                CV_POLY_APPROX_DP,
                                                9);
  
      // Circonscrire le polygone trouve dans un rectangle
      etiquetteBox = cvMinAreaRect2(approximatedContour);
  </code></pre>
  <p>I don't know how to fix this, but, at least, it is better than setting my IplImage ROI, because I compensate the etiquetteBox's angle switch from -0 to -90 degrees in consecutive frames.</p>