OpenCv学习笔记(三):使用addweighted函数将两幅图像叠加

原文地址为: OpenCv学习笔记(三):使用addweighted函数将两幅图像叠加

      在图像处理的操作中经常会遇到将图像叠加的问题,这在opencv中提供了极好的支持,如addweighted函数,下面将该函数的帮助部分略微说一下:

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

Parameters
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().
The function addWeighted calculates the weighted sum of two arrays as follows:
dst(I) = saturate(src1(I) alpha + src2(I) beta + gamma)
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:
dst = src1*alpha + src2*beta + gamma;

这部分内容是opencv参考手册上的,极容易看懂,就不再费话翻译了。下面看他给的实例,从下面的例子中的看出图像处理很简单,没有想像的那么神秘,会点编程的人就能很好的进行简单的图像处理,最起码可以用好别人已经封装好的函数,至于什么时候大牛到你封装函数,让别人去使用,要看你的能力和造化了:

#include "stdafx.h"
#include
#include
#include
using namespace cv;
int main( int argc, char** argv )
{

   double alpha = 0.5; double beta; double input;

   Mat src1, src2, dst;

   /// Ask the user enter alpha
   std::cout<<" Simple Linear Blender "<    std::cout<<"-----------------------"<    std::cout<<"* Enter alpha [0-1]: ";
   std::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:/images/LinuxLogo.jpg");
   src2 = imread("d:/images/WindowsLogo.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);

   beta = ( 1.0 - alpha );
   addWeighted( src1, alpha, src2, beta, 0.0, dst);
 
   imshow( "Linear Blend", dst );


   waitKey(0);
   return 0;
}

代码简单易懂,就不具体说明了,期待与大家的共同学习,做好计算机视觉方面的研究。其实这些工作在MATLAB里面 也可以很好的实现,只不过,大家更熟悉C++,而且opencv是开源的,MATLAB则是商用,需要花钱的,而且比较贵。


转载请注明本文地址: OpenCv学习笔记(三):使用addweighted函数将两幅图像叠加

你可能感兴趣的:(OpenCv学习笔记(三):使用addweighted函数将两幅图像叠加)