C++ 将图像区域复制到另一图像中的另一区域
我想做一件非常简单的事情:将图像中的一个区域复制到新图像中的一个新区域。在OpenCV 2.3备忘单中,他们提出了以下解决方案: “示例3.通过转换将图像ROI复制到另一个图像” 我的代码如下:C++ 将图像区域复制到另一图像中的另一区域,c++,opencv,computer-vision,C++,Opencv,Computer Vision,我想做一件非常简单的事情:将图像中的一个区域复制到新图像中的一个新区域。在OpenCV 2.3备忘单中,他们提出了以下解决方案: “示例3.通过转换将图像ROI复制到另一个图像” 我的代码如下: Mat frameO, frameS; original >> frameO; stabilized >> frameS; Mat output(frameO.rows+40, frameO.cols*2+60, CV_32FC3); output.setTo(0);
Mat frameO, frameS;
original >> frameO;
stabilized >> frameS;
Mat output(frameO.rows+40, frameO.cols*2+60, CV_32FC3);
output.setTo(0);
Rect r(0,0, frameO.cols, frameO.rows);
Mat destROI = output(Rect(20,20, frameO.cols, frameO.rows));
frameO(r).copyTo(destROI);
我只想在输出中的位置
Rect(20,20,frameO.cols,frameO.rows)
复制图像frameO
。任何人都可以告诉我为什么这不起作用?实际上,这些命令在OpenCV 2.3中不起作用,但现在以下命令在2.4版本中可以正常工作:
Mat frame1 = imread(nameReading1);
Mat output(frame1.rows*2, frame1.cols*2, frame1.type());
output.setTo(0);
frame1.copyTo(output(Rect(0, 0, frame1.cols, frame1.rows)));
只要类型一致,这将在输出中复制
frame1
,因此创建输出时要小心frame1
将被复制到由Rect(0,0,frame1.cols,frame1.rows)
定义的输出中的ROI中 相同过程的另一种方法是使用函数
因为您已经知道ROI,所以可以创建一个子图来存储临时数据,并将其粘贴到任何所需大小的新图像中
Mat tempImage = imageTest.SubMat(rowStart, rowEnd, colStart, colEnd);
Mat targetImage = new Mat(rowEnd - rowStart, colEnd - colStart, MatType.CV_64FC3);
tempImage.CopyTo(targetImage);
现在,您可以将内容粘贴到新图像上,如下所示:
double[,]transMatDynamicScaling=newdouble[2,3]{{{1,0,desiredROW},{0,1,desiredCol};
Mat转换Mat=新Mat(2,3,MatType.CV_64FC1,transMatDynamicScaling)代码>
最后,您可以使用warp函数粘贴到newImage(记住在内存中创建一个新图像)
Cv2.WarpAffine(targetImage,addImage1,transformDynamicScaling,addImage1.Size())
我知道我玩游戏迟到了,也许这对未来的用户有用。您提供的代码片段不完整。你可以发布整个代码吗,包括你实际执行复制的操作。可能重复的,也可能重复的对不起,我错过了最后一行,我刚刚编辑了它。关于你的评论karlphilip,事实上那里的问题没有得到回答。当执行frameO(r).copyTo(destROI)时;图像被很好地复制到destROI。如果我显示它,它将包含新图像,但它不会影响我希望图像结束的输出。
Mat tempImage = imageTest.SubMat(rowStart, rowEnd, colStart, colEnd);
Mat targetImage = new Mat(rowEnd - rowStart, colEnd - colStart, MatType.CV_64FC3);
tempImage.CopyTo(targetImage);