How to set ROI in OpenCV?

后端 未结 2 1649
不思量自难忘°
不思量自难忘° 2020-12-01 14:40

I have two images, the first one smaller than the other. I need to copy the second image on the first image. To do so, I need to set the ROI on the first one, copy the secon

2条回答
  •  孤街浪徒
    2020-12-01 15:22

    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);
    

提交回复
热议问题