0

I have an RGB large-image, and an RGB small-image. What is the fastest way to replace a region in the larger image with the smaller one? Can I define a multi-channel ROI and then use copyTo? Or must I split each image to channels, replace the ROI and then recombine them again to one?

Yanai Ankri
  • 439
  • 4
  • 11

1 Answers1

1

Yes. A multi channel ROI and copyTo will work. Something like:

int main(int argc,char** argv[])
{
    cv::Mat src = cv::imread("c:/src.jpg");

    //create a canvas with 10 pixels extra in each dim. Set all pixels to yellow.
    cv::Mat canvas(src.rows + 20, src.cols + 20, CV_8UC3, cv::Scalar(0, 255, 255));

    //create an ROI that will map to the location we want to copy the image into
    cv::Rect roi(10, 10, src.cols, src.rows);
    //initialize the ROI in the canvas. canvasROI now points to the location we want to copy to.
    cv::Mat canvasROI(canvas(roi));

    //perform the copy.
    src.copyTo(canvasROI);

    cv::namedWindow("original", 256);
    cv::namedWindow("canvas", 256);

    cv::imshow("original", src);
    cv::imshow("canvas", canvas);

    cv::waitKey();  

}
go4sri
  • 1,490
  • 2
  • 15
  • 29