title: Overlay smaller image in a larger image in OpenCV
body: <p>I would like to replace a part of the image with my image in Opencv </p>
  
  <p>I used </p>
  
  <pre><code>cvGetPerspectiveMatrix() with a warpmatrix and using cvAnd() and cvOr()
  </code></pre>
  
  <p>but could not get it to work</p>
  
  <p>This is the code that is currently displaying the image and a white polygon for the replacement image. I would like to replace the white polygon for a pic with any dimension to be scaled and replaced with the region pointed.</p>
  
  <p>While the code is in javacv I could convert it to java even if c code is posted </p>
  
  <pre><code>grabber.start();
  while(isDisp() &amp;&amp; (image=grabber.grab())!=null){
    if (dst_corners !=  null) {// corners of the image to be replaced
    CvPoint points = new CvPoint((byte) 0,dst_corners,0,dst_corners.length);
    cvFillConvexPoly(image,points, 4, CvScalar.WHITE, 1, 0);//white polygon covering the replacement image 
    }
  correspondFrame.showImage(image);
  }
  </code></pre>
  
  <p>Any pointers to this will be very helpful.</p>
  
  <p>Update:</p>
  
  <p>I used  warpmatrix with this code and I get a black spot for the overlay image</p>
  
  <pre><code>cvSetImageROI(image, cvRect(x1,y1, overlay.width(), overlay.height()));
  CvPoint2D32f p = new CvPoint2D32f(4);
  CvPoint2D32f q = new CvPoint2D32f(4);
  
  q.position(0).x(0);
  q.position(0).y(0);
  q.position(1).x((float) overlay.width());
  q.position(1).y(0);
  q.position(2).x((float) overlay.width());
  q.position(2).y((float) overlay.height());
  q.position(3).x(0);
  q.position(3).y((float) overlay.height());
  
  p.position(0).x((int)Math.round(dst_corners[0]);
  p.position(0).y((int)Math.round(dst_corners[1]));
  p.position(1).x((int)Math.round(dst_corners[2]));
  p.position(1).y((int)Math.round(dst_corners[3]));
  p.position(3).x((int)Math.round(dst_corners[4]));
  p.position(3).y((int)Math.round(dst_corners[5]));
  p.position(2).x((int)Math.round(dst_corners[6]));
  p.position(2).y((int)Math.round(dst_corners[7]));
  
  cvGetPerspectiveTransform(q, p, warp_matrix);
  
  cvWarpPerspective(overlay, image, warp_matrix);
  </code></pre>
  
  <p>I get a black spot for the overlay image and even though the original image is a polygon with 4 vertices the overlay image is set as a rectangle. I believe this is because of the ROI. Could anyone please tell me how to fit the image as is and also why I am getting a black spot instead of the overlay image.</p>