zoukankan      html  css  js  c++  java
  • 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 
    "); 
            return -1; 
        }
        if( !src2.data ) 
        { 
            printf("Error loading src2 
    "); 
            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;
    }

       运行结果:

      

      

  • 相关阅读:
    详解C#break ,continue, return
    c# winform 全角自动转化半角问题(C#中ImeMode的值):转载
    简短总结一下C#里跨线程更新UI(转)
    必备:常用px,pt,em换算表(转)
    C# Textbox的ImeMode取值对中文输入法的影响 (转)
    转自:C#中TextBox水印提示的简单实现
    转载:C# this.invoke()作用 多线程操作UI 理解二
    转载:C# this.Invoke()的作用与用法 理解三
    MySQL数据库----基础操作
    python之路----线程
  • 原文地址:https://www.cnblogs.com/MakeView660/p/6513305.html
Copyright © 2011-2022 走看看