【OpenCV+Cpp】day04图像混合

【OpenCV+Cpp】day04图像混合


文章目录

  • 【OpenCV+Cpp】day04图像混合
  • 前言
  • 一、理论——线性混合操作
  • 二、相关API
  • 三、代码演示


前言

继续记录C++图像处理的学习过程,学习课件参考B站OpenCV_C++图像处理课程。OpenCV_C++图像处理课程
本文分为理论、相关API和代码实现部分。


一、理论——线性混合操作

图像的线性混合即将两张图像以线性方式混合为一张图像,具体公式如下。【OpenCV+Cpp】day04图像混合_第1张图片
以上公式中, f 0 ( x ) f_0(x) f0(x)表示输入图像 0 0 0 f 1 ( x ) f_1(x) f1(x)表示输入图像 1 1 1 α \alpha α表示权重(线性系数), g ( x ) g(x) g(x)表示混合后的输出图像。

二、相关API

【OpenCV+Cpp】day04图像混合_第2张图片

三、代码演示

#include 
#include 
#include 

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
	Mat img1, img2, output1, output2, output3;
	//读入两张图片
	img1 = imread("./1.png");
	img2 = imread("./2.png");
	if (!img1.data) {
		cout << "could not load image1..." << endl;
		return -1;
	}
	if (!img2.data) {
		cout << "could not load image2..." << endl;
		return -1;
	}
	double alpha = 0.5;
	//判断两张输入图像的大小和类型是否相等
	if (img1.rows == img2.rows && img1.cols == img2.cols && img1.type() == img2.type()) {
		//权重相加
		addWeighted(img1, alpha, img2, (1.0 - alpha), 0.0, output1);
		//矩阵相加
		add(img1, img2, output2, Mat());
		//矩阵相乘
		multiply(img1, img2, output3, 1.0);
		imshow("image1", img1);
		imshow("image2", img2);
		namedWindow("blend demo", WINDOW_AUTOSIZE);
		imshow("addWeighted demo", output1); 
		imshow("add demo", output2);
		imshow("multiply demo", output3);
	}else {
		printf("could not blend images, the size of images is not same...\n");
	}
	waitKey(0);
	return 0;
}

你可能感兴趣的:(【OpenCV+Cpp】day04图像混合)