您的位置:首页 > 运维架构

OpenCV学习:使用OpenCV对两幅图像求和(求混合(blending))

2014-12-30 15:16 363 查看
Opencv_tutorials学习笔记

使用OpenCV对两幅图像求和(求混合(blending))

学习知识点:

线性混合(linear blending)

使用addWeighted进行两幅图像求和

分析说明:

线性混合操作是一种典型的二元像素操作:





通过在范围

内改变

,这个操作可以用来对两幅图像或两段视频产生时间是的画面叠加(cross-dissolve)效果。

代码说明:

addWeighted:计算两个数组的加权和。

C: void cvAddWeighted(const CvArr* src1, double alpha, const CvArr* src2, double beta,
double gamma, CvArr* dst)
C++: void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma,
OutputArray dst, int dtype=-1)


参数:

src1——第一个数组(First source array)

alpha——第一个数组的权值(Weight for the first array elements)

src2——第二个数组,和第一个数组有相同大小,相同的通道数(Second source array of the same size and channel number as src1)

beta——第二个数组的权值( Weight for the second array elements)

dst——目标数组,和输入数组有相同的大小,相同的通道数(Destination array that has the same size and number of channels as the input arrays.)

gamma——加到每个和上的数值(Scalar added to each sum.)

dtype——目标数组的深度值(Optional depth of the destination array. When both input arrays have the same depth, dtype can
be set to -1, which will be equivalent to src1.depth())

附录:opencv提供的函数实现(自己写的注释,请谨慎参考)

#include <cv.h>			//opencv头文件
#include <highgui.h>
#include <iostream>

using namespace cv;		//使用命名空间
using namespace std;

int main( int argc, char** argv )
{
double alpha = 0.5; double beta; double input;

Mat src1, src2, dst;

/// Ask the user enter alpha
cout<<" Simple Linear Blender "<<endl;
cout<<"-----------------------"<<endl;
cout<<"* Enter alpha [0-1]: ";
cin>>input;

/// We use the alpha provided by the user iff it is between 0 and 1
if( alpha >= 0 && alpha <= 1 )
{ alpha = input; }

/// Read image ( same size, same type )
src1 = imread("D:\\files\\C_exe\\Img_exercise\\4.jpg");	//读入图像
src2 = imread("D:\\files\\C_exe\\Img_exercise\\9.jpg");

if( !src1.data ) { printf("Error loading src1 \n"); return -1; }		//判断图像读取成功与否
if( !src2.data ) { printf("Error loading src2 \n"); return -1; }

/// Create Windows
namedWindow("Linear Blend", 1);		//创建一个显示窗口,Q_yyt:第二个参数为什么是1,不是CV_WINDOW_AUTOSIZE

beta = ( 1.0 - alpha );
addWeighted( src1, alpha, src2, beta, 0.0, dst);	//调用addWeighted函数

imshow( "Linear Blend", dst );

waitKey(0);
return 0;
}
运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐