## Matlab Noise Implementations

Poisson Noise:

Matlab’s Algorithm:

Adding Poisson Noise into an Image using ‘imnoise’

I = imread('2.jpg');

J = rgb2gray(I);

J = IMNOISE(J,'poisson')

I = imread('2.jpg');

a = rgb2gray(I);

a = im2double(a);

sizeA = size(a); % Matrix in MxN form

a = a(:);

b=zeros(size(a));

idx1=find(a<50);

% Matrix Positions whose pixels intensities are less than 50

t=ones(size(idx1));

% Matrix having all ones, the size is equal to idx size

em=-ones(size(idx1));

%Matrix having all -ones, the size is equal to idx size

idx2= (1:length(idx1))';

% Put values in idx2 equal to length of idx1

if (~isempty(idx1)) % if such pixels exists then

g=exp(-a(idx1)); % take Exponential of the values at those pixel

positions

while ~isempty(idx2)

em(idx2)=em(idx2)+1;

t(idx2)=t(idx2).*rand(size(idx2));

idx2 = idx2(t(idx2) > g(idx2));

end

b(idx1)=em;

end

idx1=find(a>=50);

% Cases where pixel intensities are more than 49 units

if (~isempty(idx1))

b(idx1)=round(a(idx1)+sqrt(a(idx1)).*randn(size(idx1)));

end

b = reshape(b,sizeA);

imshow(b)

Salt and pepper noise:

Matlab’s Algorithm:

Adding Salt n Pepper Noise into an Image using ‘imnoise’

I = imread('2.jpg');

J = rgb2gray(I);

K = IMNOISE(J,'salt & pepper',0.05);

figure, imshow(J), figure, imshow(K)

MANUAL Addition OF Salt n Pepper Noise

% p3 = density

J = rgb2gray(I);

p3= 0.05;

x = rand(size(J));

d = find(x < p3/2);

J(d) = 0; % Minimum value

d = find(x >= p3/2 & x < p3);

J(d) = 255; % Maximum (saturated) value

imshow(J)

Gaussian Noise:

Gaussian noise is an idealized form of white noise, which is caused by random

fluctuations in the signal. We can observe white noise by watching a television

which is slightly mistuned to a particular channel. Gaussian noise is white noise

which is normally distributed. The effect can again be demonstrated by the

imnoise function.

Matlab’s Algorithm:

b = a + sqrt(p4)*randn(sizeA) + p3;

ADDING GAUSSIAN NOISE INTO AN IMAGE USING IMNOISE

I = imread('2.jpg');

J = rgb2gray(I);

K = imnoise(J,'gaussian', 0.05);

figure, imshow(J), figure, imshow(K)

% p3 mean

% p4 variance

J = rgb2gray(I);

p3= 0;

p4 = 0.05;

J = im2double(J);

b = J + sqrt(p4)*randn(size(J)) + p3;

imshow(b)

Speckle Noise:

Matlab’s Algorithm:

% p3 = variance

J = rgb2gray(I);

p3= 0.05;

J = im2double(J);

b = J + sqrt(12*p3)*J.*(rand(size(J))-.5);

imshow(b)

I = imread('2.jpg');

J = rgb2gray(I);

K = imnoise(J,'speckle', 0.5);

figure, imshow(J), figure, imshow(K)

corr2(A,B)

corr2(A,A)=1

## Random Numbers: Normally Distributed OR Uniformly Distributed

Green line: Uniform distribution over the range [-5,5]

(MATLAB rand generates uniformly distributed pseudorandom numbers between 0 and 1)

Red Line: Normal distribution with mean of 0 and standard deviation of 1

(MATLAB randn generates normally distributed pseudorandom numbers between approximately -7 and +7)

## Discrete Cosine Transform

1D DCT:

Inverse 1D DCT:

2D DCT:

Inverse 2D DCT:

RGB = imread('lena.tif');
I = rgb2gray(RGB);
J = dct2(I);
figure, imshow(log(abs(J)),[]), colormap(jet(64)), colorbar
J(abs(J) < 10) = 0; %filter
K = idct2(J);
figure, imshow(I)
figure, imshow(K,[0 255])

Relationship between DCT and FFT

• DCT (Discrete Cosine Transform) is actually a cut-down version of the Fourier Transform or the Fast Fourier Transform (FFT):
• Only the real part of FFT
• Computationally simpler than FFT
• DCT — Effective for Multimedia Compression
• DCT MUCH more commonly used (than FFT) in Multimedia Image/Video Compression
• Cheap MPEG Audio Variant

## Discrete Fourier Transform in 2D

General Discrete Fourier Transform:

2D Discrete Fourier Transform:

Inverse 2D Discrete Fourier Transform:

>> a = [1 2 5;6 7 11]
a = 1 2 5
6 7 11
>> ffta = fft2(a)
ffta = 32.0000 -5.5000 + 6.0622i -5.5000 - 6.0622i
-16.0000 0.5000 - 0.8660i 0.5000 + 0.8660i
>> ifft2(ffta)
ans = 1 2 5
6 7 11
>> imshow(uint8(ans))