OpenCV學習:實現簡單的圖像疊加


  本實例使用簡單的線性疊加方法來實現兩幅圖像的疊加,主要使用的知識如下:

  1)線性融合  

   

  2)addWeighted函數

  //! computes weighted sum of two arrays (dst = alpha*src1 + beta*src2 + gamma)

CV_EXPORTS_W 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().

  代碼如下:

//圖像疊加(Mat)  
#include <opencv2/opencv.hpp>  
using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    double alpha = 0.5; 
    double beta; 
    double input;
    Mat src1, src2, dst;
    /// 請輸入alpha值
    cout<<" Simple Linear Blender "<<endl;
    cout<<"-----------------------"<<endl;
    cout<<"*Enter alpha [0-1]: ";
    
    /// 獲取alpha輸入    
    cin >> input;
    if( input >= 0.0 && input <= 1.0 )
    { 
        alpha = input; 
    }
    /// 加載相同尺寸,相同格式的兩張圖像
    src1 = imread("./Res/Windows7_logo.jpg");
    src2 = imread("./Res/Windows7_text.jpg");
    if( !src1.data ) 
    { 
        printf("Error loading src1 \n"); 
        return -1; 
    }
    if( !src2.data ) 
    { 
        printf("Error loading src2 \n"); 
        return -1; 
    }

    /// 創建顯示窗口
    namedWindow("Image one", 1);
    namedWindow("Image two", 1);
    namedWindow("Linear Blend", 1);

    /// 執行線性融合
    beta = 1.0 - alpha;
    addWeighted( src1, alpha, src2, beta, 0.0, dst);

    /// 顯示結果
    imshow( "Image one", src1 );
    imshow( "Image two", src2 );
    imshow( "Linear Blend", dst );

    /// 等待鍵盤事件
    waitKey(0);
    return 0;
}

   運行結果:

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM