Copy an cv::Mat inside a ROI of another one

C++OpencvCopyRoi

C++ Problem Overview


I need to copy a cv::Mat image (source) to an ROI of another (Destination) cv::Mat image.

I found this reference, but it seems that it does not work for my case. Do you have any pointers how could I do this using the OpenCV C++ interface?

C++ Solutions


Solution 1 - C++

OpenCV 2.4:

src.copyTo(dst(Rect(left, top, src.cols, src.rows)));

OpenCV 2.x:

Mat dst_roi = dst(Rect(left, top, src.cols, src.rows));
src.copyTo(dst_roi);

Solution 2 - C++

In addition or correction to above answers, if you want to copy a smaller region of open Mat to another Mat, you should do:

src(Rect(left,top,width, height)).copyTo(dst);

Solution 3 - C++

Did work for me this way:

Mat imgPanel(100, 250, CV_8UC1, Scalar(0));
Mat imgPanelRoi(imgPanel, Rect(0, 0, imgSrc.cols, imgSrc.rows));
imgSrc.copyTo(imgPanelRoi);

imshow("imgPanel", imgPanel);
waitKey();

I am using Opencv 2.4.9 Based on Andrey's answer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontheosemView Question on Stackoverflow
Solution 1 - C++Andrey KamaevView Answer on Stackoverflow
Solution 2 - C++MichView Answer on Stackoverflow
Solution 3 - C++Renato AloiView Answer on Stackoverflow