OpenCV之参数不匹配问题

多通道图像混合实例

代码如下:

//多通道图像混合
#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 << " 运行成功,得出了需要的图像~!";
}
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);//转换为灰度图像

原文地址:https://www.cnblogs.com/lbyj/p/12262947.html