How do I add white Gaussian noise with SNR=5dB to an image using imnoise
?
I know that the syntax is:
J = imnoise(I,type,parameters)
and:
SNR = 10log10[var(image)/var(error image)]
How do I use this SNR value to add noise to the image?
To add white Gaussian noise to an image (denote it I) using the imnoise command, the syntax is: where m is the mean noise and v is its variance. It is also important to note that imnoise assumes that the intensities in image I range from 0 to 1.
J = imnoise (I,'gaussian') adds zero-mean, Gaussian white noise with variance of 0.01 to grayscale image I. J = imnoise (I,'gaussian',m) adds Gaussian white noise with mean m and variance of 0.01. J = imnoise (I,'gaussian',m,var_gauss) adds Gaussian white noise with mean m and variance var_gauss.
The noise is applied to approximately d*numel (I) pixels. Variance of multiplicative noise, specified as a numeric scalar. Noisy image, returned as a numeric matrix of the same data type as input image I. For images of data type double or single, the imnoise function clips output pixel values to the range [0, 1] after adding noise.
If the input image is a different class, the imnoise function converts the image to double, adds noise according to the specified type and parameters, clips pixel values to the range [0, 1], and then converts the noisy image back to the same class as the input. The Poisson distribution depends on the data type of input image I:
Let's start by seeing how the SNR relates to the noise. Your error image is the difference between the original image and the noisy image, meaning that the error image is the noise itself. Therefore, the SNR is actually:
SNR = 10log10[var(image)/var(noise)]
For a given image and SNR=5db, the variance of the noise would be:
var(noise) = var(image)/10SNR/10 = var(image)/sqrt(10)
Now let's translate all of this into MATLAB code. To add white Gaussian noise to an image (denote it I
) using the imnoise
command, the syntax is:
I_noisy = imnoise(I, 'gaussian', m, v)
where m
is the mean noise and v
is its variance. It is also important to note that imnoise
assumes that the intensities in image I
range from 0 to 1.
In our case, we'll add zero-mean noise and its variance is v = var(I(:))/sqrt(10)
. The complete code is:
%// Adjust intensities in image I to range from 0 to 1
I = I - min(I(:));
I = I / max(I(:));
%// Add noise to image
v = var(I(:)) / sqrt(10);
I_noisy = imnoise(I, 'gaussian', 0, v);
Clarification: we use var(I(:))
to treat compute the variance of all samples in image I
(instead of var(I)
, which computes variance along columns).
Hope this helps!
I = imread('eight.tif');
I = double(I);
%// Adjust intensities in image I to range from 0 to 1
I = I - min(I(:));
I = I / max(I(:));
%// Add noise to image
v = var(I(:)) / sqrt(10);
I_noisy = imnoise(I, 'gaussian', 0, v);
%// Show images
figure
subplot(1, 2, 1), imshow(I), title('Original image')
subplot(1, 2, 2), imshow(I_noisy), title('Noisy image, SNR=5db')
Here's the result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With