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 <cv.h>
#include <highgui.h>
#include <iostream>
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::endl;
std::cout<<"-----------------------"<<std::endl;
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则是商用,需要花钱的,而且比较贵。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值