To begin with, SNR refers to noise. Your error image is the difference between the original image and the noise image, which means that the error image is the noise itself. Therefore, the SNR is actually:
SNR = 10log 10 [var(image)/var(noise)]
For a given image and SNR = 5 dB, the noise variance will be:
var(noise) = var(image)/10 SNR/10 = var(image)/sqrt(10)
Now translate all this into MATLAB code. To add white Gaussian noise to the image (denote it I ) using the imnoise , the syntax is as follows:
I_noisy = imnoise(I, 'gaussian', m, v)
where m is the average noise and v is its variance. It is also important to note that imnoise suggests that image intensities I range from 0 to 1.
In our case, we add zero mean noise, and its variance is v = var(I(:))/sqrt(10) . Full code:
%// 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);
Explanation: we use var(I(:)) to handle the variance calculation of all the samples in image I (instead of var(I) , which calculates the variance along the 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 is the result:
