OpenCV4.0学习笔记(5)图像混合

OpenCV4.0学习笔记(4)图像混合

原理:

对于一些图像(一般是一张主要图像和张背景图像)我们经常想让他们重叠,即显示背景和较透明的人物轮廓以得到更好的艺术效果。
对于图像中的任意一点像素g(x,y),其值满足

g(x,y)=(1-a)f1(x,y)+af2(x,y)

其中a,(1-a)分别为两幅图像在结果图像上的占比权重。

API

addWeight(src1, alpha, src2,(1.0 - alpha), 0.0, dst)
//图像1,图像1在处理后图像的权重,图像二,图像二权重,gamma值(一般设置0.0),输出图像1

只有一个API而且原理较为简单

但是需要注意的是:

1.alpha为双精度浮点型变量

2.混合图像1,2的格式必须完全一致,大小,色彩空间。

3.gamma值设置时一般为0.0

练习代码


#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main(int argc, char**argv)
{
	/*Mat M(100, 100, CV_8UC3,Scalar(0, 0, 0));
	if (M.empty())
	{
		printf("can't show the image...");
		return -1;
	}
	imshow("output", M);
	*/

	Mat src1,src2,dst;
	src1 = imread("E://text//text.jpg");
	src2 = imread("E://text/.jpg");
	if (src1.empty())
	{
		printf("can't show the image1...");
		return -1;
	}
	if (!scr1.data)
	{
		printf("can't show the image2...");
		return -1;
	}

	double alpha = 0.7;
	if (src1.rows == src2.rows && src1.cols == src2.cols && src1.type() == src2.type())
	{
		addWeighted(src1, alpha, src2, (1.0 - alpha), 0.0, dst);
		//add(src1, src2, dst, Mat());
		//multiply(src1, src2, dst, 1.0);
		imshow("the first image", src1);
		imshow("the second image", src2); 
		imshow("the mixed image", dst);
	}
	else
	{

		printf("could not blend images, the size of images is not same...\n");
		return -1;
	}


	waitKey(0);
	return 0;
}

运行结果:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狗头狗不狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值