0% found this document useful (0 votes)
9 views10 pages

Assignment

assignment of matlab

Uploaded by

2023ee353
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Assignment

assignment of matlab

Uploaded by

2023ee353
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Department of Electrical, Electronics and Tele-

Communication Engineering

Submitted to:
Mam Munaza
Assignment
Cleaning Noisy Security Footage Using Frequency Domain Filtering

Subject Name and Code EE-220 Signal and System


Submitted by Sarah Arif(2023-EE-361)
Date 24-Dec-2024
Code:
Function Code:

function [U, V] = dismat(M, N)

% DISMAT - Computes the distance matrix for frequency domain filters

% This function is used to compute the distance matrix for designing frequency

% Vector of size M for rows (horizontal axis)

u = single(0:(M-1));

% Vector of size N for columns (vertical axis)

v = single(0:(N-1));

% Find indices where row vector elements are greater than half of M

idx = find(u > (M/2));

% Adjusting elements by subtracting M to center the frequency range

u(idx) = u(idx) - M;

% Find indices where column vector elements are greater than half of N

idy = find(v > (N/2));

% Adjusting elements by subtracting N to center the frequency range

v(idy) = v(idy) - N;
% Two 2D matrices U and V using meshgrid

[U, V] = meshgrid(v, u);

end

Main Code:

% Clearing Noisy Security Footage Using Frequency Domain Filtering

clc % Clear the command window

clear % Clear all variables in the workspace


close all % Close all open figure windows

%% Ideal Filter

% Step 1: Read the input image (security footage frame)

f = imread('D:\third semester\signal and systems\salt_and_pepper_noise.jpg');

f = im2double(f); % Convert the image to double precision for processing

%% Add Noise

% Step 2: Simulate Gaussian noise in the image (to mimic noisy security footage)
f = imnoise(f, 'gaussian', 0.001); % Add Gaussian noise with a variance of 0.001

%% Frequency Domain

% Step 3: Convert the image to the frequency domain using FFT

F = fft2(f); % Compute the 2D Fast Fourier Transform of the noisy image

Sigma = 0.02; % Standard deviation for the Gaussian filter

[U, V] = dismat(size(f, 1), size(f, 2)); % Compute distance matrices for the frequency domain

D = hypot(U, V); % Compute the Euclidean distance in the frequency domain

%% Make Filter

% Step 4: Create a Gaussian low-pass filter to suppress high-frequency noise

H = exp(-(D.^2) / 2 * Sigma^2); % Gaussian filter formula

%% Apply Filter

% Step 5: Apply the Gaussian filter in the frequency domain

G = H .* F; % Element-wise multiplication of the filter with the FFT of the image

g = ifft2(G); % Perform the inverse FFT to get the filtered image in the spatial domain
%% Show Results

% Step 6: Visualize the results

subplot(221), imshow(f) % Display the noisy image

subplot(222), imshow(g) % Display the filtered image

subplot(223), imshow(log(1 + abs(fftshift(F))), [])

subplot(224), imshow(log(1 + abs(fftshift(G))), [])

Output:
Explanation:

Function Purpose

The function dismat calculates two matrices, U and V, which represent the horizontal and
vertical distances from the center of the frequency domain. These matrices are essential for
creating frequency domain filters like Gaussian filters, low-pass filters, or high-pass filters.

The frequency domain is typically represented in a 2D Cartesian coordinate system, where the
origin (center) corresponds to the zero frequency. To design filters, we often need to compute
distances from this center.

Code Breakdown

1. Function Definition
function [U, V] = dismat(M, N)

a. Inputs:
i. M: Number of rows (height of the frequency domain matrix, corresponding
to the image size).
ii. N: Number of columns (width of the frequency domain matrix,
corresponding to the image size).
b. Outputs:
i. U: A matrix representing horizontal distances from the center of the
frequency domain.
ii. V: A matrix representing vertical distances from the center of the
frequency domain.

2. Create Frequency Vectors


u = single(0:(M-1));
v = single(0:(N-1));

a. Here, two vectors are created:


i. u is a 1D vector with values ranging from 0 to M-1, representing
frequency indices along the rows.
ii. v is a 1D vector with values ranging from 0 to N-1, representing
frequency indices along the columns.
b. The single function is used to store these values in single-precision floating-
point format, which is more memory-efficient than double precision.

3. Center the Frequency Domain (Horizontal)


idx = find(u > (M/2));
u(idx) = u(idx) - M;

a. This part centers the frequency range for the horizontal direction:
i. Step 1: Identify indices in u where values are greater than half the total
number of rows (M/2).
ii. Step 2: Adjust these values by subtracting M. This wraps the higher
frequencies around, effectively centering the frequency range around zero.
After this step:
1. Values in u range from -M/2 to M/2.

4. Center the Frequency Domain (Vertical)

idy = find(v > (N/2));


v(idy) = v(idy) - N;

a. Similarly, this part centers the frequency range for the vertical direction:
i. Step 1: Identifies indices in v where values are greater than half the total
number of columns (N/2).
ii. Step 2: Adjust these values by subtracting N. After this step, values in v
range from -N/2 to N/2.

5. Generate Distance Matrices


[U, V] = meshgrid(v, u);

a. The meshgrid function generates two 2D matrices:


i. U contains replicated rows of the vector u.
ii. V contains replicated columns of the vector v.
b. These matrices represent the Cartesian coordinate system of the frequency
domain:
i. Each element in U represents the horizontal distance from the center.
ii. Each element in V represents the vertical distance from the center.

6. End of the Function


end

a. The function returns the matrices U and V as outputs.

Initialization

clc
clear
close all

1. clc: Clears the command window, removing any previous outputs or messages.
2. clear: Removes all variables from the workspace to free up memory and ensure no
leftover variables affect the script.
3. close all: Closes all open figure windows to start fresh.

Ideal Filter Section

Read Image

f=imread('D:\third semester\signal and system\


salt_and_pepper_noise

.jpg');

 Reads an image file named salt_and_pepper_noise.jpg located in the specified directory


(D:\third semester\signal and system) and stores its pixel data in the variable f for
further processing..
 This image will serve as the input for filtering.

f = im2double(f);

 Converts the image f to double-precision values scaled between 0 and 1. This is essential
for mathematical operations, as images are typically read as integers.
Add Noise

f = imnoise(f, 'gaussian', 0.001);

 Adds Gaussian noise to the image f with a noise variance of 0.001.


 Noise is commonly added for testing the robustness of filters.

Frequency Domain Conversion

F = fft2(f);

 Computes the 2D Fast Fourier Transform (FFT) of the noisy image f, resulting in F,
which is the image in the frequency domain.

Sigma = 0.02;

 Sets the standard deviation (Sigma) for the Gaussian filter. Smaller values result in a
tighter filter, while larger values spread it out.

[U, V] = dismat(size(f, 1), size(f, 2));

 Calls the dismat function (explained earlier) to compute the distance matrices U and V
for the frequency domain grid based on the dimensions of f.

D = hypot(U, V);

 Calculates the Euclidean distance matrix D from the origin in the frequency domain using
the matrices U and V.
 Each element in D represents the distance from the center of the frequency domain.

Make Filter

H = exp(-(D.^2) / (2 * Sigma^2));

 Creates a Gaussian filter H in the frequency domain.


 The filter attenuates (reduces) high-frequency components based on their distance from
the center (D).
 The closer the frequency to the center, the less attenuation it experiences, making it an
ideal low-pass filter.

Apply Filter

G = H .* F;

 Multiplies the Gaussian filter H with the frequency domain representation F.


 This step applies the filter in the frequency domain, suppressing high-frequency
components.

g = ifft2(G);

 Computes the 2D Inverse FFT of the filtered frequency domain data G to transform it
back into the spatial domain.
 g is the filtered image in the spatial domain.

Show Results

subplot(221), imshow(f)

 Displays the noisy input image f in the top-left subplot.

subplot(222), imshow(g)

 Displays the filtered image g in the top-right subplot.

subplot(223), imshow(log(1 + abs(fftshift(F))), [])

 Visualizes the magnitude spectrum of the original frequency domain representation F


(noisy image) in the bottom-left subplot:
o fftshift(F): Shifts the zero frequency component to the center of the spectrum.
o log(1 + abs(...)): Applies a logarithmic scale for better visualization of small
values.
subplot(224), imshow(log(1 + abs(fftshift(G))), [])

 Visualizes the magnitude spectrum of the filtered frequency domain representation G in


the bottom-right subplot.

Conclusion:

In this assignment, frequency domain filtering was applied to enhance noisy security footage
using a Gaussian low-pass filter. The process effectively reduced high-frequency noise while
preserving critical image details, demonstrating the utility of frequency domain techniques in
image processing. This approach is practical for improving image quality in security systems and
can be extended to address other noise types.

You might also like