Sometimes, a processing function(处理函数) needs to be applied only to a portion(部分) of an image. OpenCV incorporates (内嵌)an elegant(精致) and simple mechanism(机制) to define a subregion(子区域) in an image and manipulate(操作) it as a regular image(普通图像).
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.
#include <iostream> #include <opencv2/opencv.hpp> using namespace cv; int main() { namedWindow("YunFung Image"); Mat image = imread("test.jpg"); Mat logo = imread("yunfung.png"); // define image ROI at image top-right Mat imageROI(image, Rect(image.cols - logo.cols, 0, // ROI coordinates logo.cols, logo.rows)); // ROI size // insert logo logo.copyTo(imageROI); // not imwrite imshow("YunFung Image", image); waitKey(0); return 0; }