How can we apply a gaussian noise to a image?

74 ビュー (過去 30 日間)
Vishnu R
Vishnu R 2016 年 2 月 27 日
編集済み: DGM 2024 年 12 月 11 日 19:47
I want to apply gaussian noise to my input image? Is it possible to enhance the gaussian noise added image?

回答 (3 件)

John BG
John BG 2016 年 2 月 27 日
編集済み: John BG 2016 年 2 月 27 日
Vishnu
Gaussian and Normal distributions are the same.
array_gaussian_noise=mu+randn(size_1,size_2)*sigma
where mu is the mean value, when generating noise this is usually 0.
[size_1 size_2] is the size of the image
sigma is the standard deviation
Bear in mind that unlike randi, where you know the range of the uniformly distributed random outcome, with gaussian, as small as the noise may be, some noise may spike out of specs or even show negative. To match the noise to the uint8 type of RGB images, constrain the above with for instance:
array_gaussian_noise=mu+uint8(floor(randn(size_1,size_2)*sigma))
or
array_gaussian_noise=mu+uint8(abs(floor(randn(size_1,size_2)*sigma)))
The first one would simply remove all negative noise, the second one, brings to positive all negative noise values.
You really have to generate 3 of these arrays, 3 different noise matrices, to add each to RGB image components respectively.
Play with sigma and mu, on each image RGB layer to do whatever you mean by 'enhance' the added noise.
If you have the Image Processing Toolbox, it's straight forward with command imnoise, for instance:
A=imread('imagefile.jpg')
sig=100; V=(sig/256)^2 %
A_added_noise=imnoise(A,'gaussian',0,V)
If you find this answer of any help solving this question, please click on the thumbs-up vote link,
thanks in advance
John

VISHWANATH
VISHWANATH 2024 年 12 月 11 日 18:35
% Read the noisy color image
noisyImage = imread('GN.jpg');
% Check if the image is indeed a color image
if size(noisyImage, 3) ~= 3
error('The input image must be a color image (RGB).');
end
% Initialize the restored image
restoredImage = noisyImage;
% Apply Wiener filter to each color channel
for channel = 1:3
% Extract the current channel
currentChannel = noisyImage(:,:,channel);
% Apply Wiener filter
restoredChannel = wiener2(currentChannel, [5 5]);
% Store the restored channel
restoredImage(:,:,channel) = restoredChannel;
end
% Calculate PSNR for the first channel (you can choose any channel)
psnrValue = psnr(restoredImage, noisyImage);
disp(['PSNR for Gaussian Noise: ', num2str(psnrValue)]);
% Display the original noisy, restored images and their histograms
figure;
% Display the noisy image
subplot(2, 3, 1);
imshow(noisyImage);
title('Noisy Image');
% Display the restored image
subplot(2, 3, 2);
imshow(restoredImage);
title('Restored Image');
% Display histogram of each channel of the noisy image
subplot(2, 3, 3);
imhist(noisyImage(:,:,1)); % Red channel
title('Histogram of Noisy Image (Red)');
subplot(2, 3, 4);
imhist(noisyImage(:,:,2)); % Green channel
title('Histogram of Noisy Image (Green)');
subplot(2, 3, 5);
imhist(noisyImage(:,:,3)); % Blue channel
title('Histogram of Noisy Image (Blue)');
% Display histogram of each channel of the restored image
subplot(2, 3, 6);
imhist(restoredImage(:,:,1)); % Red channel
title('Histogram of Restored Image (Red)');
  1 件のコメント
DGM
DGM 2024 年 12 月 11 日 19:12
編集済み: DGM 2024 年 12 月 11 日 19:26
Is this supposed to be an answer to the question? Is it an answer to part of the question? What part of the question? If you want to provide an answer, why not format it as an answer? Why not articulate the relevance of the code?
Dumping copy-pasted code with little effort makes this seem like less of an earnest answer and more like careless spam.

サインインしてコメントする。


DGM
DGM 2024 年 12 月 11 日 19:25
編集済み: DGM 2024 年 12 月 11 日 19:47
If you want to add Gaussian noise to an image, just use imnoise(). That way, there's no need to jump through extra hoops to deal with the class and scale of the input image.
It helps to know that the parameters are given on a fixed scale. The input image is handled internally as unit-scale double, so the specified mean and variance are relative to the same unit-scale used by the working image. That makes the behavior consistent across input classes.
% a grayscale image in uint8
inpict = imread('cameraman.tif'); % [0 255]
% add noise with mu=0 and variance=0.01
% these parameters are relative to unit-scale
% even though the input may not be unit-scale
% output class is inherited from the input (uint8)
outpict = imnoise(inpict,'gaussian',0,0.01);
imshow([inpict outpict])
% let's try that again with the same image in int16
inpict = im2int16(inpict); % [-32768 32767]
% since the parameters are always relative to a fixed scale
% they don't depend on the class of the input!
outpict = imnoise(inpict,'gaussian',0,0.01); % same parameters
imshow([inpict outpict]) % same relative noise
If we had reset the RNG prior to each imnoise call and then cast the outputs to the narrowest common integer class, the results would be exactly identical.
Aside from Gaussian noise, imnoise() can apply various other kinds of noise to an image. Just read the docs for imnoise().
As to how one would try to remove the applied Gaussian noise, using a Wiener filter like @VISHWANATH demonstrates is one possibility.

カテゴリ

Help Center および File ExchangeImage Filtering and Enhancement についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by