【OpenCV:从零到一】05:图像混合和叠加

前言
这是我《OpenCV:从零到一》专栏的第五篇博客,想看跟多请戳
本文概要
multiply
addWeighted
Rect以及常用的矩形类
copyTo的另一种用法
案例代码
大概内容:图片线性混合、乘积混合、叠加。

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

using std::cout;
using std::endl;
using namespace cv;

int main(int argc, char** argv) {
	Mat linux, windows, dst1, dst2;
	linux = imread("D:\\86186\\Documents\\opencv\\linux.jpg");
	windows = imread("D:\\86186\\Documents\\opencv\\windows.jpg");
	
	if (!linux.data) {
		cout << "could not load image Linux Logo..." << endl;
		return -1;
	}
	if (!windows.data) {
		cout << "could not load image WIN7 Logo..." << endl;
		return -1;
	}
	if (linux.rows == windows.rows && linux.cols == windows.cols && linux.type() == windows.type()) {
		//混合图像(无论是乘还是加)的大小和类型都要一样

		double alpha = 0.5;//设置混合权重
		addWeighted(linux, alpha, windows, (1.0 - alpha), 0.0, dst1);
		imshow("addWeightedDst", dst1);
		imshow("linux", linux);
		imshow("windows", windows);

		multiply(linux, windows, dst2, 1.0);//Calculates the per-element scaled product of two arrays.
		imshow("multiplyDst", dst2);
		
		Mat smallLinux = imread("D:\\86186\\Documents\\opencv\\smallLinux.jpg");
		Mat imageROI = windows(Rect(20, 20, smallLinux.cols, smallLinux.rows));//ROI region of image
		Mat mask= imread("D:\\86186\\Documents\\opencv\\smallLinux.jpg", IMREAD_GRAYSCALE);//IMREAD_GRAYSCALE==0,mask必须是灰度图像
		smallLinux.copyTo(imageROI, mask);//copyTo的另一种用法是用来叠加图片
		imshow("smallLinux", windows);
	}
	else {
		printf("could not blend images , the size of images is not same...\n");
		return -1;
	}
	waitKey(0);
	return 0;
}

运行效果:
在这里插入图片描述
解析及注意事项

  • 混合的图像要大小一样,叠加的图像要比原图像小,并且位置要不超出原图像
  • 线性混合公式:
    在这里插入图片描述
  • ROI区域创建好之后可以利用copyTo将和ROI一样大小的图片叠加到ROI所在的区域上面

全注释代码

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

using std::cout;
using std::endl;
using namespace cv;

int main(int argc, char** argv) {
	Mat linux, windows, dst1, dst2;
	linux = imread("D:\\86186\\Documents\\opencv\\linux.jpg");
	windows = imread("D:\\86186\\Documents\\opencv\\windows.jpg");

	if (!linux.data) {
		cout << "could not load image Linux Logo..." << endl;
		return -1;
	}
	if (!windows.data) {
		cout << "could not load image WIN7 Logo..." << endl;
		return -1;
	}

	if (linux.rows == windows.rows && linux.cols == windows.cols && linux.type() == windows.type()) {
		//混合图像的大小和类型都要一样(无论是乘还是加)
		double alpha = 0.5;//设置混合权重
		addWeighted(linux, alpha, windows, (1.0 - alpha), 0.0, dst1);
		/*
		InputArray src1,//first input array.
		double alpha,//weight of the first array elements.
		InputArray src2,//second input array of the same size and channel number as src1.大小和通道要和图一相同
		double beta,//weight of the second array elements.
		double gamma,//scalar added to each sum.
		OutputArray dst,//scalar added to each sum.
		int dtype = -1 	//optional depth of the output array;
		*/
		imshow("addWeightedDst", dst1);
		imshow("linux", linux);
		imshow("windows", windows);

		multiply(linux, windows, dst2, 1.0);//Calculates the per-element scaled product of two arrays.
		/*
		计算两个数组的每个元素缩放后的乘积。
		参数:
		InputArray src1,
		InputArray src2,//second input array of the same size and the same type as src1
		OutputArray dst,
		double scale = 1,
		int dtype = -1
		dst(I)=saturate(scale * src1(I) * src2(I))
		*/
		imshow("multiplyDst", dst2);

		Mat smallLinux = imread("D:\\86186\\Documents\\opencv\\smallLinux.jpg");
		Mat imageROI = windows(Rect(20, 20, smallLinux.cols, smallLinux.rows));//ROI region of image
		Mat mask = imread("D:\\86186\\Documents\\opencv\\smallLinux.jpg", IMREAD_GRAYSCALE);//IMREAD_GRAYSCALE==0,mask必须是灰度图像
		smallLinux.copyTo(imageROI, mask);//copyTo的另一种用法是用来叠加图片
		imshow("smallLinux", windows);
		/*
		rect是一个矩形类,参数有四个(当然还有其他重载),建议前两个参数设置小一些,并且区域要小于原图,否则会越界
		_Tp x,//x coordinate of the top-left corner 矩形左上角的x坐标
		_Tp y,//y coordinate of the top-left corner 矩形左上角的y坐标
		_Tp height,//height of the rectangle 矩形高度
		_Tp width,//width of the rectangle  矩形宽度
		宏定义关系如下
		typedef Rect_<int> cv::Rect2i
		typedef Rect_<float> cv::Rect2f
		typedef Rect_<double> cv::Rect2d

		typedef Rect2i cv::Rect
		*/
	}
	else {
		printf("could not blend images , the size of images is not same...\n");
		return -1;
	}
	waitKey(0);
	return 0;
}

翻译笔记
ROI region of interest 感兴趣区域
coordinate v. 调节,配合;使动作协调;(衣服、家具等)搭配;与……形成共价键
adj. 同等的,并列的;配位的;坐标的
n. 坐标;配套服装;同等的人或物

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值