OpenCV 4 Computer Vision Application Programming Cookbook(Fourth Edition)
上QQ阅读APP看书,第一时间看更新

How to do it...

Let's take a look at the following steps:

  1. The first step consists of defining the ROI. We can use Rect to define the ROI:
cv::Rect myRoi= cv::Rect(image.cols-logo.cols, //ROI coordinates 
                image.rows-logo.rows, 
                logo.cols,logo.rows)
  1. Once the ROI is defined, we can create a new mat applying the ROI to another mat and it can be manipulated as a regular cv::Mat instance. The key is that the ROI is indeed a cv::Mat object that points to the same data buffer as its parent image and has a header that specifies the coordinates of the ROI. Inserting the logo would then be accomplished as follows:
  // define image ROI at image bottom-right 
  cv::Mat imageROI(image, myRoi);
 
  // insert logo 
  logo.copyTo(imageROI);

Here, image is the destination image, and logo is the logo image (of a smaller size). The following image is then obtained by executing the previous code:

Now, let's go behind the scenes to understand the code better.