% dip08_1.m                     gr_sep16
%    Random noise generation
clear all;

m = 300;  n = 300;
d = rand(m,n)*150+50;       % uniform noise in [50,200]
figure(1); imshow(d,[0 255]); 
figure(2); hist(d(:),256);
return;
%%
d = randn(m,n)*20;              %  Gaussian noise of variance 400
figure(1); imshow(d+128,[0 255]);     % shifted by 128 for display
figure(2); hist(d(:),256);
%%
p0 = .1;  p1 = .025;      % impulse noise;  p0+p1 must be <1
z = rand(m,n);
d = zeros(m,n)+128;       % non-noise points are assigned value 128
d(z<p0) = 0;
d(z>1-p1) = 255;    % points which become 255 are different from those which become 0
figure(1); imshow(d,[0 255]); 
figure(2); hist(d(:),256);
%%
a = 80;  b = .3;     % lognormal noise. Note: not the same parameters as in slide 3
d = a*exp(b*randn(m,n));
% max(d(:)), min(d(:)), mean(d(:)), var(d(:))
figure(1); imshow(d,[0 255]); 
figure(2); hist(d(:),100);
%%
a = .02;      %  exponential noise. Note: variance is low if a is large
d = (-1/a) * log(1-rand(m,n));
figure(1); imshow(d,[0 255]); 
figure(2); hist(d(:),100);

%%
f = double(imread('.\img\lena.tif'));
[m,n] = size(f);
g = f + randn(m,n)*20;     %  Gaussian additive noise of variance 400
figure(1); imshow(g,[0 255]); 
%%
g = f .* ((rand(m,n)-.5)*.8 + 1); % uniform MULTIPLICATIVE noise in [0.6, 1.4]
figure(1); imshow(g,[0 255]); 
%%
a = 3;          %  exponential MULTIPLICATIVE noise
d = (-1/a) * log(1-rand(m,n));   % noise mean is 1/a
d = d - 1/a + 1;     % bring noise to mean=1 before multiplying
g = f .* d;          % speckle noise
figure(1); imshow(g,[0 255]); 
%%
g = f;           % impulse SUBSTITUTIVE noise, probability 2*.1, values 0 and 255
z = rand(m,n); 
g(z<.1) = 0;
g(z>1-.1) = 255;  
figure(1); imshow(g,[0 255]); 

% help imnoise

return;
