copying non-rectangular roi opencv(复制非矩形roi opencv)
问题描述
我想用 C++ opencv 复制不是矩形的图像的一部分.零件的角点在图像中是已知的.我想将它粘贴到另一个图像中的确切位置.有人可以帮我吗?
源图像和目标图像大小相同.
这是源图像的示例,我知道 p1、p2、p3、p4,我想将该部分复制到新图像.
我已经有了目标图片.例如下面的图像是目标图像,我只想将源图像的标记部分粘贴到目标图像.我该怎么做?
最终的输出应该是这样的.
谢谢,
首先使用您的四个坐标创建一个蒙版图像.
现在使用 Mat::copyTo() 将你的黑色图像复制到源这里你可以使用上面的掩码.
分配黑色图像和蒙版作为源尺寸
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));
现在使用 drawContours 创建蒙版图像,在这里您应该使用 CV_FILLED 来确定轮廓厚度.
喜欢
向量<矢量<点>>坐标;co_ordinates.push_back(vector());坐标[0].push_back(P1);坐标[0].push_back(P2);坐标[0].push_back(P3);坐标[0].push_back(P4);drawContours(掩码,坐标,0,标量(255),CV_FILLED,8);
最后使用上面的掩码将黑色图像复制到源
black.copyTo(src,mask);
<块引用>
见下结果,
根据您在下面的评论,这里是您需要遵循的步骤
首先按照上述方法创建蒙版图像
使用遮罩将源图像复制到新的 Mat dst1.
反转遮罩并将目标图像复制到新的 Mat dst2
对于最终结果,只需将 dest1 和 dest2 添加到新的 Mat 中即可.
假设你已经按照上面的方法创建了掩码
复制源到新的 Mat
Mat dst1;src.copyTo(dst1,mask);
现在反转遮罩并将目标图像复制到新的垫子
Mat dst2;bitwise_not(掩码,掩码);dst.copyTo(dst2,mask);
将两者相加得到最终结果
Mat 结果=dest1+dest2;
如果您的两个图像大小不同,则可以使用以下代码
在这里您应该使用图像 ROI 进行复制、创建蒙版等.
![Mat src=imread("src.png",1);Mat dst=imread("dest.jpg",1);int new_w=0;int new_h=0;如果(src.cols>dst.cols)new_w=dst.cols;别的new_w=src.cols;如果(src.rows>dst.rows)new_h=dst.rows;别的new_h=src.rows;rect rectROI(0,0,new_w,new_h);Mat mask(new_h, new_w, CV_8UC1, cv::Scalar(0));点 P1(107,41);点 P2(507,61);点P3(495,280);点 P4(110,253);向量<矢量<点>>坐标;co_ordinates.push_back(vector());坐标[0].push_back(P1);坐标[0].push_back(P2);坐标[0].push_back(P3);坐标[0].push_back(P4);drawContours(掩码,坐标,0,标量(255),CV_FILLED,8);Mat srcROI=src(rectROI);Mat dstROI=dst(rectROI);垫 dst1;垫 dst2;srcROI.copyTo(dst1,mask);imwrite("dst1.jpg",dst1);bitwise_not(掩码,掩码);dstROI.copyTo(dst2,mask);dstROI.setTo(0);dstROI=dst1+dst2;imshow("最终结果",dst);][4]
I want to copy a part of an image which is not rectangle with 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.
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,
First create a mask image using your four co-ordinates.
Now using Mat::copyTo() copy your balck image to source here you can use above mask.
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.
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
First create Mask image as described above
Copy the the source image to new Mat dst1 using the mask.
Invert your mask and copy destination image to a new Mat dst2
For final result just add up dest1 and dest2 to new Mat.
Suppose you already created mask as above
Copy source to new Mat
Mat dst1; src.copyTo(dst1,mask);
Now invert Mask and copy destination image to new 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..
![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]
这篇关于复制非矩形roi opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:复制非矩形roi opencv
基础教程推荐
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01