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!
Example
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: