2

How can I add gaussian noise ( with a particular mean and variance ) to an image using emgucv ?

user671805
  • 602
  • 1
  • 12
  • 29

3 Answers3

3

I'm not sure quite what your asking as a Gaussian filter tends to be there to remove noise. To use a custom kernel you can use the following code. If you wish to add noise with a set mean and variance then you may have to resort to looping through the my_image.Data property and add it that way. Here is the code for using a custom Kernel if it's not quite what your after let me know and I'll try and find something more appropriate a link to example images may be useful in this case.

Image<Bgr, Byte> my_image = new Image<Bgr, byte>(open.FileName);

float[,] k = { {0, 0, 0},
               {0, 0, -0},
               {0.33F, 0, -0}};

ConvolutionKernelF kernel = new ConvolutionKernelF(k);

Image<Bgr, float> convolutedImage = my_image * kernel;

pictureBox1.Image = convolutedImage.ToBitmap();

I Hope this Helps,

Chris

Chris
  • 3,462
  • 20
  • 32
0
    Bitmap image = //your bitmap image
    Image<Hsv, Byte> templateImageInHsv = new Image<Hsv, Byte>(image);
    templateImageInHsv = templateImageInHsv.SmoothGaussian(3);

    // Summary:
    //     Perform Gaussian Smoothing in the current image and return the result
    //
    // Parameters:
    //   kernelSize:
    //     The size of the Gaussian kernel (kernelSize x kernelSize)
    //
    // Returns:
    //     The smoothed image
    public Image<TColor, TDepth> SmoothGaussian(int kernelSize);
Nikhil Dinesh
  • 3,359
  • 2
  • 38
  • 41
0
 public static Image<Gray, byte> AddGaussianNoise(Image<Gray, byte> originalImage,int mean=0, int sigma=1)
        {
            var rnd = new Random();
            var noiseGenerator = new GaussianRandom(rnd);
            var noiseImage = new Image<Gray, float>(originalImage.Width, originalImage.Height);
            var tempImage = new Image<Gray, float>(originalImage.Bitmap);
            var noiseArray = Enumerable.Range(0, tempImage.Width * tempImage.Height).Select(o =>(float) noiseGenerator.NextGaussian(0, sigma)+mean).ToArray();                
            noiseArray = noiseArray.Select(o => o+mean ).ToArray();
            var arrayAsBytes= GetByteArray(noiseArray);
            noiseImage.Bytes = arrayAsBytes;
            tempImage += noiseImage;
            return tempImage.Convert<Gray, byte>();

        }

For the GaussianRandom I am using https://stackoverflow.com/a/4594881

Jake_2
  • 78
  • 15