OpenCV3之——多通道图像混合示例
程序员文章站
2022-07-16 17:26:11
...
我们把多通道混合的实现代码封装在了名为MultiChannelBlending()函数中,详细代码如下:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
//全局函数声明
bool MultiChannelBlending();
int main(int argc, char** argv) {
system("color 5A");
if (MultiChannelBlending()) {
cout << endl << "\n运行成功,得出了需要的图像!";
}
waitKey(0);
return 0;
}
//------------------MultiChannelBlending()函数--------------
bool MultiChannelBlending() {
//定义相关变量
Mat srcImage, logoImage;
vector<Mat> channels;
//-------------------蓝色通道部分------------------------
Mat imageBlueChannel;
logoImage = imread("dota_logo.jpg", 0);//logo灰度图
srcImage = imread("dota_jugg.jpg");
if (!logoImage.data) {
cout << "读取logoImage错误!" << endl;
return false;
}
if (!srcImage.data) {
cout << "读取srcImage错误!" << endl;
return false;
}
split(srcImage, channels);
imageBlueChannel = channels.at(0);//蓝色通道ROI区域与logo线性混合再赋值到ROI区域
addWeighted(imageBlueChannel(Rect(560, 240, logoImage.cols, logoImage.rows)), 1.0,
logoImage, 0.5, 0,
imageBlueChannel(Rect(560, 240, logoImage.cols, logoImage.rows)));
//再将三个单通道重新合并成一个三通道
merge(channels, srcImage);
//显示效果图
imshow("<1>游戏原画+logo蓝色通道", srcImage);
//-------------------绿色通道部分------------------------
Mat imageGreenChannel;
logoImage = imread("dota_logo.jpg", 0);//logo灰度图
srcImage = imread("dota_jugg.jpg");
if (!logoImage.data) {
cout << "读取logoImage错误!" << endl;
return false;
}
if (!srcImage.data) {
cout << "读取srcImage错误!" << endl;
return false;
}
split(srcImage, channels);
imageGreenChannel = channels.at(1);//绿色通道ROI区域与logo线性混合再赋值到ROI区域
addWeighted(imageGreenChannel(Rect(560, 240, logoImage.cols, logoImage.rows)), 1.0,
logoImage, 0.5, 0,
imageGreenChannel(Rect(560, 240, logoImage.cols, logoImage.rows)));
//再将三个单通道重新合并成一个三通道
merge(channels, srcImage);
//显示效果图
imshow("<2>游戏原画+logo绿色通道", srcImage);
//-------------------红色通道部分------------------------
Mat imageRedChannel;
logoImage = imread("dota_logo.jpg", 0);//logo灰度图
srcImage = imread("dota_jugg.jpg");
if (!logoImage.data) {
cout << "读取logoImage错误!" << endl;
return false;
}
if (!srcImage.data) {
cout << "读取srcImage错误!" << endl;
return false;
}
split(srcImage, channels);
imageRedChannel = channels.at(2);//红色通道ROI区域与logo线性混合再赋值到ROI区域
addWeighted(imageRedChannel(Rect(560, 240, logoImage.cols, logoImage.rows)), 1.0,
logoImage, 0.5, 0,
imageRedChannel(Rect(560, 240, logoImage.cols, logoImage.rows)));
//再将三个单通道重新合并成一个三通道
merge(channels, srcImage);
//显示效果图
imshow("<2>游戏原画+logo红色通道", srcImage);
return true;
}
运行结果: