Ask Your Question

How to add Gaussian noise using JAVA

asked 2019-01-08 17:28:17 -0500 gravatar image

updated 2019-01-08 22:42:36 -0500

berak gravatar image
    Mat grayMat = new Mat();
    Mat noiseMat = new Mat();
    BitmapFactory.Options o = new BitmapFactory.Options();
    int width = grayBitmap.getWidth();
    int height= grayBitmap.getHeight();
    //bitmap to MAT
    GaussianNoise = grayMat.clone();

I don't know how to do next, to generate a Mat which is the same size as grayMat and then add them together.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-01-08 22:21:55 -0500

berak gravatar image

updated 2019-01-08 22:38:14 -0500

first, forget about the silly Bitmaps, you'll never need any here.

Mat noise = new Mat(img.size(), img.type());

then, for gaussian noise, you'll need the mean and stdev of your image:

MatOfDouble mean = new MatOfDouble ();
MatOfDouble dev = new MatOfDouble ();


// the java wrappers allow only to apply single numbers here, imho, that's a bug !
//  if your img has more than 1 channel, you might need to split() it into seperate 
//  channels, apply noise on each seperately, and merge() the results
Core.randn(noise,mean.get(0,0)[0], dev.get(0,0)[0]);

Core.add(img, noise, img);

image description

edit flag offensive delete link more


Thank you for your reply, but I think this noise is too big, I just want a noise which mean is 0 and dev is 0.1, so I use Core.randn (noise,0,1), but after I add it with gray mat, it will not work and Error shows that:Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in void cv::arithm_op(cv::InputArray, cv::InputArray, cv::OutputArray, cv::InputArray, int, void (*)(const uchar, size_t, const uchar, size_t, uchar, size_t, int, int, void), bool, void, int), file /build/master_pack-android/opencv/modules/core/src/arithm.cpp, line 659.Could you help me to solve this problem? thank you in advance. gravatar ( 2019-01-21 20:55:22 -0500 )edit
  • you can apply noise on the desired image itself, you don't need a 2nd one
  • if you try to add 2 images they need to have the same size, type and number of channels (not hard to understand, right ?)
berak gravatar imageberak ( 2019-01-22 01:04:57 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-01-08 17:28:17 -0500

Seen: 33 times

Last updated: Jan 08