Trigonometric Functions in Matlab

sin(30*pi/180)
cos(30*pi/180)
tan(30*pi/180)
cot(30*pi/180)
Advertisements

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')

Adding Poisson Noise Manually

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

I = imread('2.jpg');

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)

MANUAL ADDITION OF GAUSSIAN NOISE

% p3 mean

% p4 variance

I = imread('2.jpg');

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:

MANUAL ADDITION OF SPECKLE NOISE

% p3 = variance

I = imread('2.jpg');

J = rgb2gray(I);

p3= 0.05;

J = im2double(J);

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

imshow(b)

ADDING SPECKE NOISE AUTOMATICALLY

I = imread('2.jpg');

J = rgb2gray(I);

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

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

Random Numbers: Normally Distributed OR Uniformly Distributed

uniform-normalGreen 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:

dct1

Inverse 1D DCT:

dct2

2D DCT:

dct3

Inverse 2D DCT:

dct4

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