void addWeighted(inputarray src1,double alpha1,inputarray src2,doubke alpha2,double gamma ,outputarray dst,int dtype=-1);
(1)第一个Mat类
(2)第一个Mat类的权重
(3)第二个Mat类
(4)第二个Mat类的权重
(5)一个加到权值总和上的标量值
(6)输出的Mat类
(7)输出数组的可选深度
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
bool ROI_AddImage();
bool LinearBlending();
bool ROI_LinearBlending();
int main()
{
system("color 5E");
if (ROI_AddImage() && LinearBlending() && ROI_LinearBlending())
cout << "success" << endl;
waitKey(0);
return 0;
}
bool ROI_AddImage()//利用感兴趣区域实现图像的叠加
{
Mat srcImage1 = imread("C:/Users/csh/Desktop/1.jpg");
Mat logoImage = imread("C:/Users/csh/Desktop/4.jpg");
if (!srcImage1.data)
{
printf("error1\n");
return false;
}
if (!logoImage.data)
{
printf("error2\n");
return false;
}
Mat imageROI = srcImage1(Rect(200, 100, logoImage.cols, logoImage.rows));//设定感兴趣区域
Mat mast = imread("C:/Users/csh/Desktop/4.jpg", 0);//加载掩膜,必须是灰度图
logoImage.copyTo(imageROI, mast);//将掩膜覆盖到感兴趣区域
imshow("效果图1", srcImage1);
return true;
}
bool LinearBlending()//实现图像的线性混合
{
Mat dstImage;
Mat srcImage2 = imread("C:/Users/csh/Desktop/2.jpg");
Mat srcImage3 = imread("C:/Users/csh/Desktop/3.jpg");//两幅图片为同样的类型和尺寸
addWeighted(srcImage2, 0.5, srcImage3, 0.5, 0.0,dstImage);
imshow("效果图2", dstImage);
return true;
}
bool ROI_LinearBlending()//指定区域线性图像混合
{
Mat srcImage = imread("C:/Users/csh/Desktop/1.jpg");
Mat logoImage = imread("C:/Users/csh/Desktop/4.jpg");
Mat imageROI = srcImage(Rect(150, 200, logoImage.cols, logoImage.rows));
addWeighted(imageROI, 0.5, logoImage, 0.5, 0.0, imageROI);
imshow("效果图3", imageROI);
return true;
}