前言:
開發環境:win10 Visual Studio 2017
開發語言:C++
編譯器:Debug X64
Opencv版本:4.1.1
代碼實現:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
bool MultiChannelBlending();
int main()
{
system("color 0F");
if (MultiChannelBlending())
{
cout << endl << "OK! Please start your performance ! ";
}
while (1)
{
if (waitKey(100) == 27)
break;
}
}
bool MultiChannelBlending()
{
Mat Background, Bull, Rocket, Celts, Toronto, NewYork, Pelicans;
Mat BlueChannelsBackground, BlueChannelsBull, BlueChannelsSpur;
Mat GreenChannelsBackground, GreenChannelsCelts, GreenChannelsToronto;
Mat RedChannelsBackground,RedChannelsNewYork, RedChannelsPelicans;
vector<Mat>ChannelsBackground, ChannelsBull, ChannelsSpur, ChannelsCelts, ChannelsToronto, ChannelsNewYork, ChannelsPelicans;
//【1】讀入圖片
Background = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\背景.png");
Bull = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\bull.png");
Rocket = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\火箭.png");
Celts = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\凱爾特人.png");
Toronto = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\猛龍.png");
NewYork = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\NewYork.png");
Pelicans = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\鵜鶘.png");
//【2】把一個3通道圖像轉換成3個單通道圖像
split(Background, ChannelsBackground);//分離色彩通道
split(Bull, ChannelsBull);//分離色彩通道
split(Rocket, ChannelsSpur);//分離色彩通道
split(Celts, ChannelsCelts);//分離色彩通道
split(Toronto, ChannelsToronto);//分離色彩通道
split(NewYork, ChannelsNewYork);//分離色彩通道
split(Pelicans, ChannelsPelicans);//分離色彩通道
//【3】將原圖的某通道引用返回給imageBlueChannel,注意是引用,相當於兩者等價,修改其中一個另一個跟着變
// 【3-1】藍色通道
BlueChannelsBackground = ChannelsBackground.at(0);
BlueChannelsBull = ChannelsBull.at(2);
BlueChannelsSpur = ChannelsSpur.at(0);
// 【3-2】綠色通道
GreenChannelsBackground = ChannelsBackground.at(1);
GreenChannelsCelts = ChannelsCelts.at(1);
GreenChannelsToronto = ChannelsToronto.at(1);
// 【3-3】紅色通道
RedChannelsBackground = ChannelsBackground.at(2);
RedChannelsNewYork = ChannelsNewYork.at(2);
RedChannelsPelicans = ChannelsPelicans.at(2);
//【4】將原圖的某通道的某座標處下的一塊區域和logo圖進行加權操作,將得到的混合結果存到對應背景圖的通道中
// 【4-1】藍色通道
addWeighted(BlueChannelsBackground(Rect(0, 220, Bull.cols, Bull.rows)), 1.0, BlueChannelsBull, 0.5, 0,BlueChannelsBackground(Rect(0, 220, Bull.cols, Bull.rows)));
addWeighted(BlueChannelsBackground(Rect(0, 0, Rocket.cols, Rocket.rows)), 1.0, BlueChannelsSpur, 0.5, 0,BlueChannelsBackground(Rect(0, 0, Rocket.cols, Rocket.rows)));
// 【4-2】綠色通道
addWeighted(GreenChannelsBackground(Rect(220, 0, Celts.cols, Celts.rows)), 1.0,GreenChannelsCelts, 0.5, 0, GreenChannelsBackground(Rect(220, 0, Celts.cols, Celts.rows)));
addWeighted(GreenChannelsBackground(Rect(220, 220, Toronto.cols, Toronto.rows)), 1.0,GreenChannelsToronto, 0.5, 0, GreenChannelsBackground(Rect(220, 220, Toronto.cols, Toronto.rows)));
// 【4-3】紅色通道
addWeighted(RedChannelsBackground(Rect(440, 0, NewYork.cols, NewYork.rows)), 1.0, RedChannelsNewYork, 0.5, 0, RedChannelsBackground(Rect(440, 0, NewYork.cols, NewYork.rows)));
addWeighted(RedChannelsBackground(Rect(440, 220, Pelicans.cols, Pelicans.rows)), 1.0, RedChannelsPelicans, 0.5, 0, RedChannelsBackground(Rect(440, 220, Pelicans.cols, Pelicans.rows)));
//【5】將三個單通道重新合併成一個三通道
merge(ChannelsBackground, Background); // 注意,這裏是將三個合併後的單通道再次合併爲一個三通道,只需要一步操作!
//【6】顯示效果圖
imshow("NBA", Background);
return true;
}
結果:
參考資料: