1 | initial version |
Indeed your issue was that you were not working with signed images. And in the following code you can find an alternative in order to avoid looping through the image pixels, which my guess would be that it is more time consuming (I have not tested it to be honest) than using the internal functions of opencv:
void addGaussianNoise(Mat &image, double average=0.0, double standard_deviation=10.0)
{
// We need to work with signed images (as noise can be
// negative as well as positive). We use 16 bit signed
// images as otherwise we would lose precision.
Mat noise_image(image.size(), CV_16SC3);
randn(noise_image, Scalar::all(average), Scalar::all(standard_deviation));
Mat temp_image;
image.convertTo(temp_image,CV_16SC3);
addWeighted(temp_image, 1.0, noise_image, 1.0, 0.0, temp_image);
temp_image.convertTo(image,image.type());
}
2 | No.2 Revision |
Indeed your issue was that you were not working with signed images. And in the following code you can find an alternative in order to avoid looping through the image pixels, which my guess would be that it is more time consuming (I have not tested test it to be honest) than using the internal functions of opencv:
void addGaussianNoise(Mat &image, double average=0.0, double standard_deviation=10.0)
{
// We need to work with signed images (as noise can be
// negative as well as positive). We use 16 bit signed
// images as otherwise we would lose precision.
Mat noise_image(image.size(), CV_16SC3);
randn(noise_image, Scalar::all(average), Scalar::all(standard_deviation));
Mat temp_image;
image.convertTo(temp_image,CV_16SC3);
addWeighted(temp_image, 1.0, noise_image, 1.0, 0.0, temp_image);
temp_image.convertTo(image,image.type());
}