[CT404]: Assignment 2 progress

This commit is contained in:
2024-11-13 00:10:34 +00:00
parent 1fac1124e1
commit fdba91dbf9
5 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,31 @@
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Task 2.1: Spatial Domain
image = cv2.imread("../../Task2.jpg")
kernel_size = (15, 15)
variance = 2
smoothed_image = cv2.GaussianBlur(image, kernel_size, variance)
cv2.imwrite("./output/1_spatial_domain.jpg", smoothed_image)
# Task 2.2: Frequency Domain Low-Pass Filter
gaussian_kernel = cv2.getGaussianKernel(kernel_size[0], variance)
gaussian_kernel_2d = gaussian_kernel @ gaussian_kernel.T
fft_gaussian = np.fft.fft2(gaussian_kernel_2d)
# shift zero frequency component to center
fft_gaussian_shifted = np.fft.fftshift(fft_gaussian)
# calculate the magnitude spectrum for visualization
magnitude_spectrum = np.log(np.abs(fft_gaussian_shifted) + 1)
# Plot the magnitude spectrum (Frequency Domain Representation)
plt.imshow(magnitude_spectrum, cmap='gray')
plt.axis('off')
plt.savefig("./output/2_frequency_domain_low-pass_filter.jpg", bbox_inches='tight', pad_inches=0)
# Task 2.3: Frequency Domain Filtering

View File

@ -295,6 +295,39 @@ I chose to go with \mintinline{python}{kernel_size = 25} as it seemed to give th
\subsection{Extraction of Binary Regions of Interest / Connected Components}
\newpage
\section{Filtering of Images in Spatial \& Frequency Domains}
\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{../Task2.jpg}
\caption{Original Facial Image}
\end{figure}
\subsection{Spatial Domain}
\begin{code}
\inputminted[firstline=5, lastline=13, linenos, breaklines, frame=single]{python}{../code/task2/task2.py}
\caption{Task 2.1 section of \mintinline{python}{task2.py}}
\end{code}
After some experimentation, I chose parameter values of \mintinline{python}{kernel_size = (15,15)} and \mintinline{python}{variance = 2} as, in my opinion, these yielded the best balance between blurring imperfections like wrinkles without causing the entire image to become too blurry.
\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{../code/task2/output/1_spatial_domain.jpg}
\caption{Output of \mintinline{python}{1_spatial_domain.jpg}}
\end{figure}
\subsection{Frequency Domain Filtering}
\begin{code}
\inputminted[firstline=15, lastline=29, linenos, breaklines, frame=single]{python}{../code/task2/task2.py}
\caption{Task 2.2 section of \mintinline{python}{task2.py}}
\end{code}
\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{../code/task2/output/2_frequency_domain_low-pass_filter.jpg}
\caption{Zero-centered low-pass filter of Gaussian Kernel}
\end{figure}
\end{document}