Why do we divide the variance by 255^2 when adding Gaussian noise in MATLAB?

5 次查看(过去 30 天)
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!

采纳的回答

Nithin
Nithin 2025-2-24
When using "imnoise" function, if the image is not already of type "double", MATLAB converts it to "double", scaling the pixel values to the range "[0, 1]". As a result, a pixel value of "255" in "uint8" format becomes "1.0" in "double". Since "imnoise" applies noise assuming a "double" image with pixel values in "[0, 1]", the variance of the noise must be adjusted accordingly. To know more details on how the "imnoise" function handles different data types, please refer to this documentation: https://www.mathworks.com/help/images/ref/imnoise.html?searchHighlight=imnoise#mw_226e1fb2-f53a-4e49-9bb1-6b167fc2eac1
The example in the question might specify the variance for an image in "uint8" format, which is why you need to divide it by "255^2" to account for the scaling when the image is converted to "double". This behavior is consistent across MATLAB versions. The requirement to normalize the variance by "255^2" is due to how "imnoise" operates with different image classes and is not specific to a particular MATLAB version.
I hope this resolves your query.
  1 个评论
jiang yutong
jiang yutong 2025-3-6
Thank you for your detailed explanation! I now understand that the division is necessary because the imnoise function internally converts the image to double and scales the pixel values to the range [0,1]. This means the variance must be adjusted to match the scaled pixel values. Your reference to the MATLAB documentation was also very helpful in clarifying how imnoise handles different data types. I appreciate your clear explanation and confirmation that this behavior is consistent across MATLAB versions. This resolves my query perfectly!

请先登录,再进行评论。

更多回答(2 个)

Divyajyoti Nayak
Divyajyoti Nayak 2025-2-24
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
  1 个评论
jiang yutong
jiang yutong 2025-3-6
Thank you for taking the time to explain this in such detail! Your breakdown of the variance scaling and the mathematical reasoning behind it is very insightful. While I ultimately went with another explanation, I truly appreciate the effort you put into clarifying the concept and linking the documentation. Your response helped me better understand the underlying mechanics of how variance is handled in the imnoise function. Thanks again for your help!

请先登录,再进行评论。


Karan Singh
Karan Singh 2025-2-24
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
  1 个评论
jiang yutong
jiang yutong 2025-3-6
Thank you for your explanation! Your clarification about the variance scaling and how MATLAB handles the conversion of uint8 images to the [0, 1] range is very helpful. While I ended up selecting another answer, I truly appreciate the time and effort you took to explain the concept clearly. Your response added valuable insight into why the normalization step is necessary. Thanks again for your help!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by