简体   繁体   English

复制非矩形roi opencv

[英]copying non-rectangular roi opencv

I want to copy a part of an image which is not rectangle with C++ opencv. 我想用C ++ opencv复制不是矩形的图像的一部分。 The corner points of the part is known in the image. 零件的角点在图像中是已知的。 I want to paste it in a another image in exact location. 我想将其粘贴到其他图像中的确切位置。 Can anybody please help me? 有人可以帮我吗?

The source image and the destination image are of same size. 源图像和目标图像大小相同。

here is an example of source image, I know p1,p2,p3,p4 and I want to copy that part to a new image. 这是源图像的示例,我知道p1,p2,p3,p4,并且我想将该部分复制到新图像中。 在此处输入图片说明

I already have a destination image. 我已经有一个目标图像。 For example the below image is destination image, and I want to paste only the marked part of the source image to the destination image. 例如,下面的图像是目标图像,我只想将源图像的标记部分粘贴到目标图像。 How can I do it? 我该怎么做? 在此处输入图片说明

And the final output should look something like this one. 最终输出应类似于此内容。 在此处输入图片说明

Thanks, 谢谢,

  1. First create a mask image using your four co-ordinates. 首先使用您的四个坐标创建蒙版图像。

  2. Now using Mat::copyTo() copy your balck image to source here you can use above mask. 现在使用Mat :: copyTo()将您的黑图像复制到源,您可以在此处使用上述蒙版。

Allocate black image and mask as source size 分配黑色图像和遮罩作为源尺寸

Mat src=imread("img.png",1);
Mat black(src.rows, src.cols, src.type(), cv::Scalar::all(0));
Mat mask(src.rows, src.cols, CV_8UC1, cv::Scalar(0));

Now create mask image using drawContours , here you should use CV_FILLED for contour thickness. 现在使用drawContours创建蒙版图像,这里您应该使用CV_FILLED作为轮廓线厚度。

Like 喜欢

   vector< vector<Point> >  co_ordinates;
   co_ordinates.push_back(vector<Point>());
   co_ordinates[0].push_back(P1);
   co_ordinates[0].push_back(P2);
   co_ordinates[0].push_back(P3);
   co_ordinates[0].push_back(P4);
   drawContours( mask,co_ordinates,0, Scalar(255),CV_FILLED, 8 );

Finally copy black image to source using above mask 最后使用上述蒙版将黑色图像复制到源

black.copyTo(src,mask);  

See below result, 看到下面的结果,

在此处输入图片说明

Edit : 编辑:

Based on your comment below here is the steps you need to follow 根据下面的评论,您需要执行以下步骤

  1. First create Mask image as described above 首先如上所述创建蒙版图像

  2. Copy the the source image to new Mat dst1 using the mask. 使用蒙版将源图像复制到新的Mat dst1。

  3. Invert your mask and copy destination image to a new Mat dst2 反转遮罩并将目标图像复制到新的Mat dst2

  4. For final result just add up dest1 and dest2 to new Mat. 对于最终结果,只需将dest1和dest2加到新的Mat中即可。

    Suppose you already created mask as above 假设您已经如上所述创建了遮罩

    Copy source to new Mat 将源复制到新的Mat

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

Now invert Mask and copy destination image to new Mat 现在反转遮罩并将目标图像复制到新的Mat

Mat dst2;
bitwise_not(mask,mask);
dst.copyTo(dst2,mask);

Get final result by adding both 通过将两者加在一起获得最终结果

Mat result=dest1+dest2;

In case your both image are of different size then you can use following code 如果您的两个图像的大小不同,则可以使用以下代码

Here you should use image ROI for copy, create mask etc.. 在这里,您应该使用图像ROI复制,创建蒙版等。

![Mat src=imread("src.png",1);
Mat dst=imread("dest.jpg",1);
int new_w=0;
int new_h=0;
if(src.cols>dst.cols)
 new_w=dst.cols;
else
 new_w=src.cols;

if(src.rows>dst.rows)
 new_h=dst.rows;
else
 new_h=src.rows;

Rect rectROI(0,0,new_w,new_h);
Mat mask(new_h, new_w, CV_8UC1, cv::Scalar(0));

Point P1(107,41);
Point P2(507,61);
Point P3(495,280);
Point P4(110,253);
vector< vector<Point> >  co_ordinates;
co_ordinates.push_back(vector<Point>());

co_ordinates\[0\].push_back(P1);
co_ordinates\[0\].push_back(P2);
co_ordinates\[0\].push_back(P3);
co_ordinates\[0\].push_back(P4);
drawContours( mask,co_ordinates,0, Scalar(255),CV_FILLED, 8 );

Mat srcROI=src(rectROI);
Mat dstROI=dst(rectROI);
Mat dst1;
Mat dst2;

srcROI.copyTo(dst1,mask);
imwrite("dst1.jpg",dst1);

bitwise_not(mask,mask);
dstROI.copyTo(dst2,mask);

dstROI.setTo(0);
dstROI=dst1+dst2;
imshow("final result",dst);][4]

在此处输入图片说明

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

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