是否可以将旋转的图像复制到另一个具有opencv的图像的旋转的ROI? [英] Is it possible to copy a rotated image into a rotatedrect ROI of another image with opencv?

查看:1439
本文介绍了是否可以将旋转的图像复制到另一个具有opencv的图像的旋转的ROI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,问了几乎相同的问题,但我已经尝试了很多方法,我仍然不能做我想做的,我甚至不确定它可能与opencv单独。
我已经旋转了一个图像,我想把它复制到另一个图像。问题是,无论什么方式,我裁剪这个旋转的图像,它总是复制在第二个图像的周围有一个非旋转的方形。从下面的图片可以看出(忘记了白色部分,这是确定)。我只是想删除条纹部分。
我相信我的问题是我的投资回报率,我复制图像,因为这个ROI是一个矩形,而不是一个RotatedRect。如下面的代码所示。

  cv :: Rect roi(Pt1.x,Pt1.y,ImageAd.cols ,ImageAd.rows); 
ImageAd.copyTo(ImageABC(roi));

但是我不能使用以下代码中的rotateRect ...

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

那么有没有办法做我想要的在opencv?
谢谢!





在使用下面的方法和掩码后,我得到这个图像,如图所示被roi切断,我用它来说明图像中我要复制的地方旋转图像。基本上现在我已经屏蔽了图像,我如何选择将这个屏蔽的图像放在我的第二个图像。目前我使用一个矩形,但不会工作,因为我的形象不再是一个矩形,而是一个旋转的矩形。看看代码,看看我现在错误地做了它(它切断,如果我使更大的一个异常被抛出)。

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



解决方案

而不是投资回报率,您可以使用掩码进行复制,


$ b $


  • 使用此掩码将源图像复制到目标图像

    / p>

  • 查看下面的C ++代码



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

    使用绘制轮廓创建遮罩。

      Point2f vertices [4]; 
    rRect.points(vertices);
    Mat掩码(src.rows,src.cols,CV_8UC1,cv :: Scalar(0));
    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);

    最后使用上述掩码将来源复制到目的地。

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


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

    解决方案

    Instead of ROI you can use mask to copy,

    1. First create mask using rotated rect.

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

    这篇关于是否可以将旋转的图像复制到另一个具有opencv的图像的旋转的ROI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆