Opencv3 C++ VS2017 学习笔记 04图像的混合

图像的线性混合


  • 线性混合模型
    • \large g\left ( x \right )=\left ( 1-\alpha \right )f_{0}\left ( x \right )+\alpha f_{1}\left ( x \right )
    • α取值0-1
    • \large f_{0} 和 f_{1}对应src1和src2, 操作对象是像素\large f_{1}是src1和src2, 对应操作对象是像素
  • API
    • addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, Output dst, int dtype=-1);
    • src1 ,src2 即输入图片
    • alpha=1-beta
    • gamma是平衡参数
    • dst输出对象
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, char ** argv)
{
	Mat src0, src1;
	src0 = imread("C:\\Users\\xujin\\Desktop\\test0.JPG");
	if (!src0.data)
	{
		cout << "no image";
		return -1;
	}
	namedWindow("src0_image", WINDOW_AUTOSIZE);
	imshow("src0_image", src0);
	src1 = imread("C:\\Users\\xujin\\Desktop\\test1.JPG");
	if (!src1.data)
	{
		cout << "no image";
		return -1;
	}
	namedWindow("src1_image", WINDOW_AUTOSIZE);
	imshow("src1_image", src1);
	if (src1.rows == src0.rows && src1.cols == src0.cols && src0.type() == src1.type())
	{
		Mat dst;
		double alpha = 0.2;
		double beta = 1 - alpha;
		double gamma = 0.0;
		int dtype = -1;
		addWeighted(src0, alpha, src1, beta, gamma, dst, dtype);
		//add(src0, src1, dst, Mat());  没有参数值会过于生硬
		//multiply(src1, src0, dst);    也是一种混合
		namedWindow("dst_image", WINDOW_AUTOSIZE);
		imshow("dst_image", dst);
	}
	else
	{
		cout << "not equal";
		return -1;
	}

	waitKey(0);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Mrsherlock_/article/details/104491786