简体   繁体   English

是否可以使用opencv将旋转的图像复制到另一图像的旋转的ROI中?

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

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. 好的,很抱歉再次询问几乎相同的问题,但是我尝试了很多方法,但是我仍然无法做我想做的事情,而且我甚至不确定单独使用opencv是否有可能。 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. 我相信我的问题是我将图像复制到的ROI,因为该ROI是rect而不是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... 但是我不能像下面的代码那样使用rotatedRect进行复制到...

cv::RotatedRect roi(cent, sizeroi, angled);
ImageAd.copyTo(ImageABC(roi));

So is there a way of doing what I want in opencv? 那么在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. 在将下面的方法与蒙版一起使用之后,我得到了该图像,该图像被roi剪切掉了,在该图像中我曾经说过要在图像中复制旋转的图像。 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. 目前,我使用的是rect,但是由于我的图像不再是rect,而是旋转的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). 查看代码,看看我现在是怎么做的(它切断了,如果我使rect变大,则会引发异常)。

cv::Rect roi(Pt1.x, Pt1.y, creditcardimg.cols, creditcardimg.rows); 
        creditcardimg.copyTo(imagetocopyto(roi),mask);

在此处输入图片说明

在此处输入图片说明

Instead of ROI you can use mask to copy, 您可以使用蒙版进行复制,而不是使用ROI,

  1. First create mask using rotated rect. 首先使用旋转矩形创建蒙版。

  2. Copy your source image to destination image using this mask 使用此蒙版将源图像复制到目标图像

See below C++ code 参见下面的C ++代码

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);

在此处输入图片说明在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM