OpenCV4例程学习——AddingImages
程序示例
/**
* @file AddingImages.cpp
* @brief Simple linear blender ( dst = alpha*src1 + beta*src2 )
* @author OpenCV team
*/
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.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;
/**
* @function main
* @brief Main function
*/
int main(void)
{
double alpha = 0.5; double beta; double input;
Mat src1, src2, dst;
/// Ask the user enter alpha
cout << " Simple Linear Blender " << endl;
cout << "-----------------------" << endl;
cout << "* Enter alpha [0.0-1.0]: ";
cin >> input;
// We use the alpha provided by the user if it is between 0 and 1
if (input >= 0 && input <= 1)
{
alpha = input;
}
//![load]
/// Read images ( both have to be of the same size and type )
src1 = imread("data/Linux.jpg");
src2 = imread("data/Windows.jpg");
//![load]
if (src1.empty()) { cout << "Error loading src1" << endl; return -1; }
if (src2.empty()) { cout << "Error loading src2" << endl; return -1; }
//![blend_images]
beta = (1.0 - alpha);
addWeighted(src1, alpha, src2, beta, 0.0, dst);
//![blend_images]
//![display]
imshow("Linear Blend", dst);
waitKey(0);
//![display]
return 0;
}
代码理解吸收
这部分代码还是简单易懂的,不需要过多描述。
方法cv::imread为读取图像,返回值为Mat的指针头,方法之中已经为返回的矩阵申请内存。
方法cv::addWeighted为将两幅图像加权合并,注意传入的两个Mat对象大小要是一致的。
方法cv::imshow为显示图像。
方法cv::waitKey为等待按键方法,cv::imshow方法后使用才能使得图像显示出来。