多通道圖像混合實例
代碼如下:
//多通道圖像混合
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
bool MultiChannelBlending();
int main(int argc, char** argv)
{
system("color 9F");
if (MultiChannelBlending())
{
cout << endl << "\n運行成功,得出了需要的圖像~!";
}
waitKey(0);
return 0;
}
bool MultiChannelBlending()
{
Mat src, logo;
vector<Mat>channels;
//-----藍色部分通道
Mat imageBlueChannel;
logo = imread("E:/image/logo.jpg");
src = imread("E:/image/dota.jpg");
if (logo.empty())
{
cout << "could not load the image..." << endl;
return -1;
}
if (!src.data)
{
cout << "could not load the image..." << endl;
return -1;
}
split(src, channels);
imageBlueChannel = channels.at(0);
addWeighted(imageBlueChannel(Rect(400, 250, logo.cols, logo.rows)), 1.0, logo, 0.5, 0, imageBlueChannel(Rect(400, 250, logo.cols, logo.rows)));
merge(channels, src);
imshow("1.游戲原畫+logo藍色通道", src);
return true;
}
運行時出現了如下錯誤:Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array')中文意思是:輸入參數大小不匹配(操作對象既不是'數組對數組的運算'(兩個數組必須大小相同、通道數相同),又不是數組與標量的運算,也不是標量與數組的運算))
經過求證,發現錯誤出現在兩張圖片的疊加操作,imageBlueChannel是經過通道分離后得到的一個單通道圖像,而logo圖像是一個彩色的三通道圖像,故無法進行疊加操作,必須將logo圖像也轉換為單通道圖像。
解決方法如下:
方法一:logo = imread("E:/image/logo.jpg",0);//以灰度圖像的形式打開
方法二:logo = imread("E:/image/logo.jpg");
cvtColor(logo, logo, CV_BGR2GRAY);//轉換為灰度圖像