How to add to an image white Gaussian noise of zero mean and standard deviation of certain gray levels?

조회 수: 12 (최근 30일)
Hello everyone, How can we add white Gaussian noise to an image with zero mean and standard deviation of 64 gray levels? I do know how to add noise of zero mean and variance using imnoise but I do not know about standard deviation of 64 gray levels.

채택된 답변

Image Analyst
Image Analyst 2018년 7월 12일
Did you try imnoise() or randn()? If not, why not? They're so easy that you should be able to figure them out on your own.
  댓글 수: 5
Image Analyst
Image Analyst 2018년 7월 13일
편집: Image Analyst 2019년 12월 13일
OK, I sense that you tried but couldn't do it, so here is a full demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Make a gray scale image of brightness 128 gray levels.
grayImage = 128 * ones(480, 640, 'uint8');
% Make a noise image of standard deviation 64 gray levels.
noiseOnlyImage = 64 * randn(480, 640);
% Add the noise image to the gray scale image.
noiseAddedImage = double(grayImage) + noiseOnlyImage;
% Compute the standard deviation of the three images.
sdGray = std(double(grayImage(:)))
sdNoiseOnly = std(noiseOnlyImage(:))
sdNoiseAdded = std(noiseAddedImage(:))
% Compute the means of the three images.
meanGray = mean(double(grayImage(:)))
meanNoiseOnly = mean(noiseOnlyImage(:))
meanNoiseAdded = mean(noiseAddedImage(:))
%======================================================
% Now plot everything.
subplot(2, 3, 1);
imshow(grayImage);
title('Original Image', 'FontSize', 20);
% Display its histogram
subplot(2, 3, 4);
imhist(grayImage);
grid on;
caption = sprintf('Histogram of Original Image\nMean = %.2f, SD = %.2f', meanGray, sdGray);
title(caption, 'FontSize', 20);
subplot(2, 3, 2);
imshow(noiseOnlyImage, []);
title('Noise-Only Image', 'FontSize', 20);
% Display its histogram
subplot(2, 3, 5);
histogram(noiseOnlyImage, 'EdgeColor', 'none');
grid on;
caption = sprintf('Histogram of Original Image\nMean = %.2f, SD = %.2f', meanNoiseOnly, sdNoiseOnly);
title(caption, 'FontSize', 20);
subplot(2, 3, 3);
imshow(noiseAddedImage, []);
title('Noise-Added Image', 'FontSize', 20);
% Display its histogram
subplot(2, 3, 6);
histogram(noiseAddedImage, 'EdgeColor', 'none');
grid on;
caption = sprintf('Histogram of Noise-Added Image\nMean = %.2f, SD = %.2f', meanNoiseAdded, sdNoiseAdded);
title(caption, 'FontSize', 20);
Mohsin Shah
Mohsin Shah 2018년 7월 13일
Thank you so much, your answers are always fascinating and I learn more than I expect. I got my answer.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

lakpa tamang
lakpa tamang 2019년 12월 13일
why is the mean not 0 in your code, yet he is asking for awgn?
  댓글 수: 1
Image Analyst
Image Analyst 2019년 12월 13일
I used randn() to get 640*480 = 307,200 samples. Since these are RANDOM, the mean will not necessarily be exactly at zero. Imagine if you asked for only 4 values. Would you expect the value to be at exactly zero:
>> r=randn(1, 4)
r =
-0.740261712090743 -0.384816596337627 -0.581927647800475 1.27720101511378
>> mean(r)
ans =
-0.107451235278765
See, not exactly zero even though randn() draws from a standard normal distribution.
I don't know how important it was to him to have a mean of exactly zero versus having random numbers drawn from a distribution. I'd imagine having the random numbers is fine and the fact that they don't have a mean of exactly zero doesn't really matter to him. If it did, he could subtract the mean or something like that.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by