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.
img_a? Unless it'sCV_16SC3, you need to specify the output depth inaddWeighted.