Problem to add gaussian noise to image

39 views (last 30 days)
I should add gaussian noise with standard deviation = 25 to the 'zebre.y' (attached) image. I use this code
clear all;close all;clc;
f=fopen('zebre.y','r');
x=fread(f,[321 481],'uint8');
x=double(x');
fclose(f);
figure();imshow(x,[]);
%x=1+(1/(min(min(x))-max(max(x)))).*(x-min(min(x)));
x_noisy=imnoise(x,'gaussian',0,5);
figure();imshow(x_noisy,[]);
but the problem is that the noisy image show only noise, and not the original image with noise added. I tried to scale the image to [0 1] to, but the result is not different. Where is my mistake?

Accepted Answer

Image Analyst
Image Analyst on 19 Apr 2014
Well you messed up. Look at my code to see how to do it correctly:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\diul\Documents\Temporary';
baseFileName = 'zebre.y';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
fileID = fopen(fullFileName, 'r');
uint8Image = fread(fileID,[481 321],'*uint8');
fclose(fileID);
% Display the original uint8 image.
subplot(2, 2, 1);
imshow(uint8Image,[]);
axis on;
title('Original uint8 Image', 'FontSize', fontSize);
% Add noise to the uing8 image.
noisyImage8 = imnoise(uint8Image, 'gaussian', 0, 0.05);
% Display the noisy uint8 image.
subplot(2, 2, 3);
imshow(noisyImage8,[]);
axis on;
title('Noisy uint8 Image', 'FontSize', fontSize);
% Convert to double.
dblImage = im2double(uint8Image');
% Display the double image.
subplot(2, 2, 2);
imshow(dblImage,[]);
axis on;
title('Original Double Image', 'FontSize', fontSize);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(dblImage);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Normalize image to the range 0-1
minGL = min(dblImage(:));
maxGL = max(dblImage(:));
dblImage = (dblImage-minGL) / (maxGL-minGL);
noisyImage = imnoise(dblImage, 'gaussian', 0, 0.05);
% Check to see that it did it correctly.
minGL = min(dblImage(:))
maxGL = max(dblImage(:))
subplot(2, 2, 4);
imshow(noisyImage, []);
title('Noisy Double Image', 'FontSize', fontSize);
  5 Comments
Image Analyst
Image Analyst on 10 Apr 2018
You need to add the variance assuming your image is in the range 0-1, not 0-255.
Deepa Abraham
Deepa Abraham on 31 May 2018
variance,v =(sigma/255)^2; Is this right?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!