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!
0 个评论
采纳的回答
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.
更多回答(2 个)
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
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!