Assignment
Assignment
Communication Engineering
Submitted to:
Mam Munaza
Assignment
Cleaning Noisy Security Footage Using Frequency Domain Filtering
% This function is used to compute the distance matrix for designing frequency
u = single(0:(M-1));
v = single(0:(N-1));
% Find indices where row vector elements are greater than half of M
u(idx) = u(idx) - M;
% Find indices where column vector elements are greater than half of N
v(idy) = v(idy) - N;
% Two 2D matrices U and V using meshgrid
end
Main Code:
%% Ideal Filter
%% 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
[U, V] = dismat(size(f, 1), size(f, 2)); % Compute distance matrices for the frequency domain
%% Make Filter
%% Apply Filter
g = ifft2(G); % Perform the inverse FFT to get the filtered image in the spatial domain
%% Show Results
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.
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.
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.
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.
Read Image
.jpg');
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 = 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.
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));
Apply Filter
G = H .* F;
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)
subplot(222), imshow(g)
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.