1

I am trying to add Gaussian Noise to only a single channel in an image. Here is the code I wrote :

  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);

I am having an error on the addWeighted() line in the code.
Error:

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)

What am I doing wrong here?

Edit: The color channel value in img_a_noise_planes[1] is 1. So I changed my addweighted() function to :

addWeighted(gauss_noise, 1.0, img_a_noise_planes[1], 1.0, 1.0, img_a_noise_planes[1], 1);

Now the error is :

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)

In the merge function, each img_a_noise_planes have channel=1 and I have put 3 in there as img_a has 3 channels and is made by merging.

1
  • What is the depth of img_a? Unless it's CV_16SC3, you need to specify the output depth in addWeighted. Commented Dec 29, 2016 at 21:00

2 Answers 2

1

I would guess the type of the gaussian Mat is the problem here. If you are not sure of the img_a type then you can move the gauss_noise creation and make it dependent on the type of the split channels. For example:

Mat gauss_noise = Mat(img_a.size(), img_a_noise_planes[1].type());
Sign up to request clarification or add additional context in comments.

2 Comments

I changed my gauss_noise definition, it gives the same error as edit
I removed the changes I had done to addweighted function and it works now. Thanks!
1

Based on the error message it looks like img_a_noise_planes was never initalized or set to the size of the input.

1 Comment

This is probably not the case since the split should do the required allocation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.