OpenCv學習筆記(三):使用addweighted函式將兩幅影象疊加

NO IMAGE

      在影象處理的操作中經常會遇到將影象疊加的問題,這在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則是商用,需要花錢的,而且比較貴。