addWeighted()计算数组的加权和(融合)

addWeighted()定义如下

The function addWeighted calculates the weighted sum of two arrays as follows:
\f[\texttt{dst} (I)= \texttt{saturate} ( \texttt{src1} (I)* \texttt{alpha} +  \texttt{src2} (I)* \texttt{beta} +  \texttt{gamma} )\f]
where I is a multi-dimensional index of array elements. In case of multi-channel arrays, each
channel is processed independently.
The function can be replaced with a matrix expression:
@code{.cpp}
    dst = src1*alpha + src2*beta + gamma;
@endcode
@note Saturation is not applied when the output array has the depth CV_32S. You may even get
result of an incorrect sign in the case of overflow.
@param src1 first input array.
@param alpha weight of the first array elements.
@param src2 second input array of the same size and channel number as src1.
@param beta weight of the second array elements.
@param gamma scalar added to each sum.
@param dst output array that has the same size and number of channels as the input arrays.
@param dtype optional depth of the output array; when both input arrays have the same depth, dtype
can be set to -1, which will be equivalent to src1.depth().
@sa  add, subtract, scaleAdd, Mat::convertTo
*/
CV_EXPORTS_W void addWeighted(InputArray src1, double alpha, InputArray src2,
                              double beta, double gamma, OutputArray dst, int dtype = -1);

dst = src1*alpha + src2*beta + gamma;

注意:

addWeighted()函数的三个Mat对象的大小需要一样,否则容易出错;

#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char *agrv[])
{
	Mat imag_person = imread("C:\\Users\\Desktop\\picture.jpg");
	Mat imag_lion = imread("C:\\Users\\Desktop\\lion.jpg");
	if (!imag_person.data&&!imag_lion.data)
	{
		cout << "Open error!" << endl;
		return -1;
	}
	Rect rec(800, 0, imag_lion.cols, imag_lion.rows);//确定融合图片的位置与大小;
//Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
//_Tp x; //!< x coordinate of the top-left corner
//_Tp y; //!< y coordinate of the top-left corner
//_Tp width; //!< width of the rectangle
//_Tp height; //!< height of the rectangle
	Mat imag_RIO = imag_person(rec);//将imag_person中rec区域的图片保存在imag_RIO中;
	addWeighted(imag_RIO, 0.8, imag_lion, 0.2, 0, imag_RIO);//进行图片融合,将(imag_RIO与imag_lion)进行融合后,保存在image_RIO中;
	imshow("image_RIO",imag_RIO);
	imshow("【创建矩形框】", imag_person);/*为什么输出img_person中也有融合?难道是由于在创建imag_RIO是imag_person指定位置的图形?
	相当于image_person是一个大数组M,N(imag_RIO)是M中指定位置指定大小的子集合,imag_RIO与imag_lion进行融合后(即进行加权和),
	最后将加权后的结果保存在imag_RIO中,而Imag_RIO又是imag_person的子集,所以imag_person相应部分也进行融合;**有人知道原因吗?可以
	交流一下;***/
	/*找到答案了:首先需要从Mat类的结构说起,Mat类是由两个数据部分组成:矩阵头(包含矩阵尺寸、存储方法、存储地址等信息)和一个指向存储所有像素的矩阵
	(根据所选存储方法的不同,矩阵可以是不同的维数)的指针。由于图像产生的矩阵是非常巨大的,所以在万不得已时,不应该进行大图像的复制,这样会降低程序的运行效率;那如何解决这个问题呢?
	OpenCV使用了引用计数机制。其思路是让每一个Mat对象有自己的信息头,但是共享同一矩阵。
	这通过让矩阵指针指向同一个地址而实现,而拷贝构造函数则只复制头信息和矩阵指针,而不复制矩阵。但是若想复制这个图像矩阵,而不是简单复制其头信息和矩阵指针,可以通过clone()或者copyTo()
	实现。所以在图像融合时,原图像为什么也会发生改变的原因就解释的同了*/
	waitKey(0);
	return 0;
}

addWeighted()计算数组的加权和(融合)_第1张图片
如果将融合的结果保存在imag_lion中?

addWeighted(imag_RIO, 0.8, imag_lion, 0.2, 0, imag_lion);
	imshow("imag_RIO", imag_RIO);
	imshow("【创建矩形框】", imag_person);
	imshow("imag_lion", imag_lion);

addWeighted()计算数组的加权和(融合)_第2张图片addWeighted()计算数组的加权和(融合)_第3张图片

你可能感兴趣的:(addWeighted()计算数组的加权和(融合))