How to set ROI in OpenCV?

I think you have something wrong. If the first one is smaller than the other one and you want to copy the second image in the first one, you don’t need an ROI. You can just resize the second image in copy it into the first one.

However if you want to copy the first one in the second one, I think this code should work:

cv::Rect roi = cv::Rect((img2.cols - img1.cols)/2,(img2.rows - img1.rows)/2,img1.cols,img1.rows);

cv::Mat roiImg;
roiImg = img2(roi);

img1.copyTo(roiImg);

Leave a Comment