C++ opencv代码中的错误:C++;

C++ opencv代码中的错误:C++;,c++,opencv,C++,Opencv,我试图将高斯噪声仅添加到图像中的单个通道。以下是我编写的代码: double Mean = 0.0; double StdDev = 10.0; Mat gauss_noise = Mat(img_a.size(), CV_16SC1); randn(gauss_noise, Scalar::all(Mean), Scalar::all(StdDev)); Mat img_a_noise_planes[3]; split(img_a, img_a_noise_plane

我试图将高斯噪声仅添加到图像中的单个通道。以下是我编写的代码:

  double Mean = 0.0;
  double StdDev = 10.0;
  Mat gauss_noise = Mat(img_a.size(), CV_16SC1);
  randn(gauss_noise, Scalar::all(Mean), Scalar::all(StdDev));

  Mat img_a_noise_planes[3];
  split(img_a, img_a_noise_planes);
  addWeighted(gauss_noise, 1.0, img_a_noise_planes[1], 1.0, 1.0, img_a_noise_planes[1]);

  merge(img_a_noise_planes, 3, img_a);
代码中的
addWeighted()
行出现错误。
错误:

OpenCV Error: Bad argument (When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified) in arithm_op, file /home/cortana/Libraries/opencv/modules/core/src/arithm.cpp, line 683
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/cortana/Libraries/opencv/modules/core/src/arithm.cpp:683: error: (-5) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function arithm_op

Aborted (core dumped)
我做错了什么

编辑:
img\u a\u noise\u平面[1]
中的颜色通道值为
1
。因此,我将我的
addweighted()
函数更改为:

加权(高斯噪声,1.0,img噪声平面[1],1.0,1.0,img噪声平面[1],1)

现在的错误是:

OpenCV Error: Assertion failed (mv[i].size == mv[0].size && mv[i].depth() == depth) in merge, file /home/cortana/Libraries/opencv/modules/core/src/convert.cpp, line 222
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/cortana/Libraries/opencv/modules/core/src/convert.cpp:222: error: (-215) mv[i].size == mv[0].size && mv[i].depth() == depth in function merge

Aborted (core dumped)

在merge函数中,每个
img\u a\u noise\u平面都有
channel=1
,我在其中放了3个,作为
img\u a
有3个通道,通过合并而成。

根据错误消息,看起来img\u noise\u平面从未初始化或设置为输入的大小。

我猜出高斯
Mat
类型这就是问题所在。
如果您不确定
img\u a
类型,则可以移动高斯噪声创建,并使其取决于分割通道的类型。例如:

Mat gauss_noise = Mat(img_a.size(), img_a_noise_planes[1].type());

img\U a的深度是多少?除非是
CV_16SC3
,否则您需要在
addWeighted
中指定输出深度。情况可能不是这样,因为拆分应该进行所需的分配。我更改了我的
gauss_noise
定义,它给出的错误与editI删除了我对addWeighted函数所做的更改相同,现在可以工作了。谢谢