复制非矩形OpenCV的投资回报率 [英] copying non-rectangular roi opencv
问题描述
我想要复制未使用C ++的OpenCV矩形的图像的一部分。部分的角点在图像中是已知的。我希望把它粘贴在确切位置的另一幅图像。任何人都可以请帮我吗?
源图像和目标图像相同的大小。
这里是源图像的例子,我知道P1,P2,P3,P4和我想的那部分复制到一个新的形象。
我已经有一个目标图像。例如下面的图片是目的地形象,我想只粘贴源图像到目标图像的显着部分。我该怎么办呢?
和最终的输出应该是这个样子的。
谢谢,
-
首先使用你的四坐标创建一个面具的形象。
-
现在使用垫::将copyTo()复制你巴尔克图像源在这里你可以使用上面的面具。
分配黑色图像和掩码,源大小
垫SRC = imread(img.png,1);
垫黑色(src.rows,src.cols,src.type(),CV ::标量::所有(0));
垫面膜(src.rows,src.cols,CV_8UC1,CV ::标(0));
现在使用创建屏蔽图像<一个href=\"http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=drawcontours#drawcontours\">drawContours,在这里,你应该使用CV_FILLED轮廓厚度。
如同
矢量&lt;矢量&lt;点和GT; &GT; co_ordinates;
co_ordinates.push_back(矢量&lt;点和GT;());
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(掩模,co_ordinates,0,标量(255),CV_FILLED,8);
最后,利用上述口罩黑色图像复制到源
black.copyTo(SRC,面罩);
块引用>请参见下面的结果,
编辑:
根据下面在这里您的评论是你需要遵循的步骤
以上
的描述首先创建面膜图片复制源图像到新垫DST1使用面膜。
翻转你的面具和目标图像复制到一个新垫DST2
有关最终结果只是dest1和dest2加起来新垫。
假设你已经创建了面具如上
复制源新材料
垫DST1;
src.copyTo(DST1,掩模);
现在颠倒面具和复制目的地形象新垫
垫DST2;
bitwise_not(掩模,掩模);
dst.copyTo(DST2,掩模);通过增加获取最终的结果都
垫结果= dest1 + dest2;
在您的情况这两种图像大小不同,那么你可以用下面的code
在这里,你应该使用图像的投资回报复制,创建面膜等。
[垫子SRC = imread(src.png,1)!;
垫DST = imread(dest.jpg,1);
INT new_w = 0;
INT new_h = 0;
如果(src.cols&GT; dst.cols)
new_w = dst.cols;
其他
new_w = src.cols;如果(src.rows&GT; dst.rows)
new_h = dst.rows;
其他
new_h = src.rows;矩形rectROI(0,0,new_w,new_h);
垫面膜(new_h,new_w,CV_8UC1,CV ::标(0));点P1(107,41);
点P2(507,61);
点P3(495280);
点P4(110253);
矢量&lt;矢量&lt;点和GT; &GT; co_ordinates;
co_ordinates.push_back(矢量&lt;点和GT;());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(掩模,co_ordinates,0,标量(255),CV_FILLED,8);垫srcROI = SRC(rectROI);
垫dstROI = DST(rectROI);
太DST1;
太DST2;srcROI.copyTo(DST1,掩模);
imwrite(dst1.jpg,DST1);bitwise_not(掩模,掩模);
dstROI.copyTo(DST2,掩模);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]
这篇关于复制非矩形OpenCV的投资回报率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!