Why do we divide the variance by 255^2 when adding Gaussian noise in MATLAB?
Show older comments
I came across a piece of code on GitHub that adds Gaussian noise to an image in MATLAB, and I noticed the following line:
code in Matlab R2018a
% Depend on the MATLAB algorithm, we must divide to 255^2
% See: https://www.mathworks.com/matlabcentral/answers/390682-code-for-white-gaussian-noise-for-image
I = imread(WatermarkedImagePath);
AttackedWatermarkedImage = imnoise(I, 'gaussian', 0, variance / 255^2);
I is a color image.
The comment suggests dividing the variance by 255^2, but I'm not quite sure why this is necessary. Can someone explain why this normalization step is required when applying Gaussian noise to an image?
Is this step required because of the specific version of MATLAB? Does this normalization need to be applied for images in newer MATLAB versions like 2024a?
Thanks in advance!
Accepted Answer
More Answers (2)
Divyajyoti Nayak
on 24 Feb 2025
0 votes
I went through the documentation of the 'imnoise' function to understand this, here's the link:
First of all, the input argument 'I' needs to be a grayscale image. The 'imnoise' function expects pixel values of this grayscale image to be of type double and in the range between 0 and 1. If the pixel values are in the range between 0-255 (which most images are) then the function converts it to the expected range (0-1 by dividing 255) before adding the noise with the desired variance.
So the variance argument needs to be the equivalent variance for a image with pixel range 0-1. For example if the desired variance is 10 for image with pixel range 0-255 then the equivalent variance for image with pixel range 0-1 would be 10/255^2.
This can be understood by looking at the formula for variance:
var_0to255 = sum((px_0to255 - mean_px_0to255) ^ 2) / NumberOfPixels
var_0to1 = sum((px_0to1 - mean_px_0to1) ^ 2) / NumberOfPixels
Now px_0to1 = px_0to255 / 255, substituting this in var_0to1 gives
var_0to1 = sum(((px_0to255 - mean_px_0to255) / 255) ^ 2) / NumberOfPixels
var_0to1 = var_0to255 / 255^2
Karan Singh
on 24 Feb 2025
I think when you call
AttackedWatermarkedImage = imnoise(I, 'gaussian', 0, variance / 255^2);
the variance parameter you supply is expected to be on a normalized scale that is, for images whose pixel values lie in the interval [0, 1]. However, most color images loaded with imread are of type uint8 with values ranging from 0 to 255. When you pass a uint8 image to imnoise, MATLAB automatically converts it to a double in [0, 1] before adding noise.
In other words, if you have a variance defined on the original 0–255 scale, you must convert it to the [0, 1] scale by dividing by 255² (since variance scales as the square of the intensity). This conversion ensures that the noise added is consistent with the expected normalized intensity range.
Karan
Categories
Find more on Image Transforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!