ROI image is superimposed with the OpenCV copyTo (ROI, mask)

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )


A, ROI region of interest

1.ROI regional access

ROI area is to select a portion of the image analysis.
ROI region of interest can only be achieved through the following two specific ways, or else after the change does not affect the original image.

(1) a first: use Rect ()

Mat ROI=image(Rect(左上横,左上纵,矩形宽,矩形高));

image is the image analysis.

Such as:

Mat imageROI=image(Rect(0,2,10,20));

(2) The second: use Range ()

Mat ROI=image(Range(左上横, 左上横+矩形宽),Range(左上纵, 左上纵+矩形高))

Such as:

Mat imgeROI2=image(Range(0,0+10),Range(2,2+20));

Change 2.ROI area

Changing the ROI: The selected area is changed, the original image to be analyzed will also change.

Change the way Summary: Use only deep copy of copyTo (), null other copy format.

#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
	Mat A=imread("3.jpg");
	Mat ROI=A(Rect(0,0,100,100));
	
	Mat B=imread("5.jpg");

	//ROI=B;	浅层拷贝1:ROI会被改变,但A没有被改变
	//ROI(B);	浅层拷贝2:这个连编译都会错误

	//B.copyTo(ROI);		//深层拷贝copyTo():ROI会被改变,而且A也被改变了	
	ROI=B.clone();			//深层拷贝clone():ROI会被改变,但A没有被改变
	imshow("A-changed",A);
	waitKey(0);
	return 0;
}

Here Insert Picture Description

Two, ROI load mask

1. mask

And operation
Here Insert Picture Description
https://blog.csdn.net/bitcarmanlee/article/details/79132017

2.copyTo () function

There are two overloaded:

  • image.copyTo (imageROI): copying the contents of the role of the image pasted to the imageROI;

  • image.copyTo (imageROI, mask): role of the mask image and overlapping the mask after the values ​​of 0 (black) corresponding to the point in the image point becomes transparent, leaving other points.

mask image directly equal img2

3.ROI load mask

#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
	Mat backImage=imread("3.jpg");
	Mat logoImage=imread("5.jpg");
	Mat ROI=backImage(Rect(0,0,logoImage.cols,logoImage.rows));
	//ROI
	Mat mask=imread("5.jpg",0);
	//mask要读取灰度图
	logoImage.copyTo(ROI,mask);
	//使用双参数的重载函数
	imshow("Effect",backImage);

	waitKey();
	return 0;
}

Here Insert Picture Description

Reference:
https://www.cnblogs.com/minglou/articles/9594495.html
https://blog.csdn.net/qq_35294564/article/details/81045381

Guess you like

Origin blog.csdn.net/sandalphon4869/article/details/94565827