Question

This question could be very silly for others, but for me it is very difficult.

Hello, I am a beginner in MATLAB, I am recently reading a paper where we have to add noise in an image in following manner.

Gaussian Noise => sigma = 10

Salt & Pepper Noise => p = 20% sp

I know about MATLAB functions for adding noise, we use imnoise() function for it.

noise_image = imnoise(input_image, 'salt & pepper', gaus_val)

will add salt & pepper noise and

noise_image = imnoise(input_image, 'gaussian', 0 , salt_pepper_val);

will add ZERO-MEAN Gaussian noise of variance salt_pepper_val.

Now my question is that using imnoise() function how can I add following amount of noise. In other words for what value of gaus_val and salt_pepper_val, I will get gaussian noise of amount sigma = 10%, 20%,..... and salt and pepper noise of amount 20%, 30%...............

Gaussian Noise => sigma = 10

Salt & Pepper Noise => p = 20% sp

Was it helpful?

Solution

Different parameters are used because the two types of noise are characterised differently:

  1. gaussian noise corrupts the whole image, and if it's white (i.e. no spatial covariance) with zero mean (on average it doesn't brighten or darken the image) then it is completely defined by the noise amplitude sigma. if you read the documentation www.mathworks.com/help/images/ref/imnoise.html then you see that you can specify both the mean and the variance (sigma*sigma -- if sigma = 10 then variance = 100). you can then add noise with sigma 10 using:

    >> noise_image = imnoise(input_image, 'gaussian', 0 , 100);
    
  2. salt and pepper noise are white(salt) and black(pepper) spots in an image, i.e. they don't affect every pixel. sp is the percentage of an image that is corrupted (default 5% or 0.05), so sp = 20% = 0.2 means that 1 in 5 pixels are corrupted:

    >> noise_image = imnoise(input_image, 'salt & pepper', 0.2);
    

exactly as described on the help page.

EDIT: please beware that imnoise() is used for images with intensities between 0 and 1, and that values outside the range [0,1] are clipped -- the variance may not be the one you specify.

you can also easily do the same without using imnoise:

(1) load an image as [0,1] greyscale:

  >> input_image = mean(imread('peppers.png'),3)/256;

(2) add gaussian noise of 0.1 * the image's sigma:

  >> image2 = randn(size(input_image)); 
  >> image2 = input_image+image2*0.1*std(input_image(:))/std(image2(:)); 

(3) add s&p noise in 20% of pixels:

  >> image3 = input_image; 
  >> li=randperm(length(image3(:))); 
  >> image3(li(1:(end/5)))=1; 
  >> image3(li(1:(end/10)))=0; 

(4) show side by side:

  >> imagesc([input_image image2 image3])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top