The Core Functionality5(Adding (blending) two images using OpenCV )

Goal:

Image blending and function addWeighted()

Theory:

Source and: "Computer Vision: Algorithms and Applications"

Image mixing is pixel manipulation, the formula is as follows:

g( x ) = ( 1 - α ) f0(x)+αf1(x)

Source Code 

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
// we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
using std::cin;
using std::cout;
using std::endl;
int main( void )
{
   double alpha = 0.5; double beta; double input;
   Mat src1, src2, dst;
   cout << " Simple Linear Blender " << endl;
   cout << "-----------------------" << endl;
   cout << "* Enter alpha [0.0-1.0]: " << endl;;
   
    //cin >>input;
   input=0.4;
   cout << input << endl;
    // We use the alpha provided by the user if it is between 0 and 1
   if( input >= 0 && input <= 1 )
     { alpha = input; }
   src1 = imread( "lena.jpg" );
   src2 = imread( "tong.jpg" );
   
   cout << src1.channels() << endl;
   cout << src2.channels() << endl;
   
   if( src1.empty() ) { cout << "Error loading src1" << endl; return -1; }
   if( src2.empty() ) { cout << "Error loading src2" << endl; return -1; }
   beta = ( 1.0 - alpha );
   cout << beta << endl;
   
   resize(src2, src2, Size(src1.cols, src1.rows)); //改变图片尺寸大小,由于下一个函数addWeighted()要求
   addWeighted( src2, alpha, src1,beta, 0.0, dst); //the same size
   
   namedWindow("Linear Blend",WINDOW_AUTOSIZE);
   imshow( "Linear Blend", dst);
   waitKey(0);
   return 0;

}

Which needs to be explained: addWeighted( src1, alpha, src2, beta, gamma, dst) principle:

dst=αsrc1+βs r c 2 + γ

CMakeLists.txt文件:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "-std=c++11")
set(VMAKE_BUILD_TYPE "Debug")
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

install(TARGETS DisplayImage RUNTIME DESTINATION bin)

The test results are as follows:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529631&siteId=291194637