opencv代码中的错误:C ++

时间:2016-12-29 19:31:46

标签: 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_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_a_noise_planes[1]中的颜色通道值为1。所以我将addweighted()函数更改为:

  

addWeighted(gauss_noise,1.0,img_a_noise_planes [1],1.0,1.0,img_a_noise_planes [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)

在合并功能中,每个img_a_noise_planes都有channel=1,我已将3放在那里,因为img_a有3个频道,并且是通过合并制作的。

2 个答案:

答案 0 :(得分:1)

基于错误消息,看起来img_a_noise_planes从未被初始化或设置为输入的大小。

答案 1 :(得分:1)

我猜这里的高斯type Mat就是问题。 如果您不确定img_a类型,则可以移动gauss_noise创建并使其取决于拆分通道的类型。例如:

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