Is it possible to copy a rotated image into a rotatedrect ROI of another image with opencv?

c++, image, opencv

Solution

Instead of ROI you can use mask to copy,

First create mask using rotated rect.

Copy your source image to destination image using this mask

See below C++ code

Your rotated rect and I calculated manually.

RotatedRect rRect = RotatedRect(Point2f(140,115),Size2f(115,80),192);

Create mask using draw contour.

   Point2f vertices[4];
   rRect.points(vertices);
   Mat mask(src.rows, src.cols, CV_8UC1, cv::Scalar(0));
   vector< vector<Point> >  co_ordinates;
   co_ordinates.push_back(vector<Point>());
   co_ordinates[0].push_back(vertices[0]);
   co_ordinates[0].push_back(vertices[1]);
   co_ordinates[0].push_back(vertices[2]);
   co_ordinates[0].push_back(vertices[3]);
   drawContours( mask,co_ordinates,0, Scalar(255),CV_FILLED, 8 );

Finally copy source to destination using above mask.

    Mat dst;
    src.copyTo(dst,mask);

Problem

Ok sorry for asking pretty much the same question again but I've tried many methods and I still can't do what I'm trying to do and I'm not even sure it's possible with opencv alone. I have rotated an image and I want to copy it inside another image. The problem is that no matter what way I crop this rotated image it always copies inside this second image with a non rotated square around it. As can be seen in the image below.(Forget the white part thats ok). I just want to remove the striped part. I believe my problem is with my ROI that I copy the image to as this ROI is a rect and not a RotatedRect. As can be seen in the code below. ``` cv::Rect roi(Pt1.x, Pt1.y, ImageAd.cols, ImageAd.rows); ImageAd.copyTo(ImageABC(roi)); ``` But I can't copyTo with a rotatedRect like in the code below... ``` cv::RotatedRect roi(cent, sizeroi, angled); ImageAd.copyTo(ImageABC(roi)); ``` So is there a way of doing what I want in opencv? Thanks! After using method below with masks I get this image which as seen is cut off by the roi in which I use to say where in the image I want to copy my rotated image. Basically now that I've masked the image, how can I select where to put this masked image into my second image. At the moment I use a rect but that won't work as my image is no longer a rect but a rotated rect. Look at the code to see how I wrongly do it at the moment (it cuts off and if I make the rect bigger an exception is thrown). ``` cv::Rect roi(Pt1.x, Pt1.y, creditcardimg.cols, creditcardimg.rows); creditcardimg.copyTo(imagetocopyto(roi),mask); ```

Original source

Related problems