[CT404]: Add Lab 2

This commit is contained in:
2024-12-01 00:15:00 +00:00
parent 2f43ed9285
commit e348572fd7
24 changed files with 433 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 219 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 451 KiB

View File

@ -0,0 +1,167 @@
close all;
clear all;
clc;
% Load necessary package
pkg load image;
% Load and preprocess the image
img = imread('truck.jpg'); % Replace with the path to your image
if size(img, 3) == 3
img = rgb2gray(img); % Convert to grayscale if the image is in color
end
% Resize the image to 128x64 for standardization
img = imresize(img, [128, 64]);
% Gamma normalization
gamma = 0.9; % Example gamma value
img = im2double(img) .^ gamma; % Convert to double and apply gamma normalization
figure;
imshow(img);
title('Gamma-normalized Image');
% Parameters for HOG
cell_size = 4; % Size of each cell (4x4 pixels)
block_size = 2; % Number of cells per block (2x2 cells)
num_bins = 9; % Number of orientation bins (0° to 180°)
bin_size = 180 / num_bins;
% Compute gradients using Sobel operator on the entire image
Gx = imfilter(double(img), [-1 0 1; -2 0 2; -1 0 1], 'conv'); % Horizontal gradient
Gy = imfilter(double(img), [-1 -2 -1; 0 0 0; 1 2 1], 'conv'); % Vertical gradient
% Compute gradient magnitude and orientation
magnitude = sqrt(Gx.^2 + Gy.^2);
orientation = atan2d(Gy, Gx);
orientation(orientation < 0) += 180; % Convert orientation to [0, 180] range
% Display gradient magnitude and orientation
figure;
imshow(magnitude, []);
title('Gradient Magnitude of the Image');
% Display gradient orientation using quiver
figure;
imshow(img); % Show original image as background
hold on;
% Downsample the quiver plot for better visualization, e.g., every 4th pixel
step = 4; % Adjust the step to make the plot less dense if needed
% Plot gradient orientations using Gx and Gy as the vector components
[x, y] = meshgrid(1:step:size(Gx, 2), 1:step:size(Gy, 1));
quiver(x, y, Gx(1:step:end, 1:step:end), Gy(1:step:end, 1:step:end), 'color', 'r', 'LineWidth', 0.5);
title('Gradient Orientation Visualization Using Quiver');
hold off;
% Calculate number of cells in each direction
num_cells_row = floor(size(img, 1) / cell_size);
num_cells_col = floor(size(img, 2) / cell_size);
% Preallocate the HOG feature array
hog_features = zeros(1, num_cells_row * num_cells_col * num_bins);
% Calculate HOG features for each cell
counter = 1;
for row = 1:cell_size:(size(img, 1) - cell_size + 1)
for col = 1:cell_size:(size(img, 2) - cell_size + 1)
% Extract cell gradient magnitudes and orientations
cell_magnitude = magnitude(row:row+cell_size-1, col:col+cell_size-1);
cell_orientation = orientation(row:row+cell_size-1, col:col+cell_size-1);
% Calculate histogram for the cell manually
cell_histogram = zeros(1, num_bins);
for bin = 1:num_bins
% Define the orientation range for the bin
angle_min = (bin - 1) * bin_size;
angle_max = bin * bin_size;
% Find pixels within the bin range
mask = (cell_orientation >= angle_min) & (cell_orientation < angle_max);
% Sum the magnitudes of pixels within the current orientation bin
cell_histogram(bin) = sum(cell_magnitude(mask));
end
% Store cell histogram in hog_features
hog_features(counter:counter+num_bins-1) = cell_histogram;
counter = counter + num_bins;
end
end
% Block normalization: Divide the HOG features into blocks of 2x2 cells and normalize
block_histograms = [];
for row = 1:num_cells_row - block_size + 1
for col = 1:num_cells_col - block_size + 1
% Compute the index in hog_features for the current block
start_index = ((row - 1) * num_cells_col + col - 1) * num_bins + 1;
end_index = start_index + block_size * block_size * num_bins - 1;
% Extract the block of HOG features
block = hog_features(start_index:end_index);
% Normalize the block histogram
norm_block = block / sqrt(sum(block .^ 2) + 1e-6);
% Append the normalized block to block_histograms
block_histograms = [block_histograms, norm_block];
end
end
% Final HOG descriptor for the entire image
hog_descriptor = block_histograms;
% Display results
disp("HOG Descriptor for the entire image:");
disp(size(hog_descriptor));
disp("Length of HOG descriptor vector:");
disp(length(hog_descriptor));
% Visualization of HOG Features on a Blank Canvas
figure;
imshow(ones(size(img)), []); % Blank figure with same size as image
hold on;
% Visualize dominant gradient direction per cell
for row = 1:cell_size:(size(img, 1) - cell_size + 1)
for col = 1:cell_size:(size(img, 2) - cell_size + 1)
% Get cell gradient data
cell_magnitude = magnitude(row:row+cell_size-1, col:col+cell_size-1);
cell_orientation = orientation(row:row+cell_size-1, col:col+cell_size-1);
% Calculate histogram for the cell manually
cell_histogram = zeros(1, num_bins);
for bin = 1:num_bins
% Define the orientation range for the bin
angle_min = (bin - 1) * bin_size;
angle_max = bin * bin_size;
% Find pixels within the bin range
mask = (cell_orientation >= angle_min) & (cell_orientation < angle_max);
% Sum the magnitudes of pixels within the current orientation bin
cell_histogram(bin) = sum(cell_magnitude(mask));
end
% Find the dominant orientation
[max_value, dominant_bin] = max(cell_histogram);
dominant_orientation = (dominant_bin - 1) * bin_size + bin_size / 2; % Center of the bin
% Convert the orientation to radians for plotting
angle_rad = deg2rad(dominant_orientation);
% Plot the dominant gradient direction using quiver
scale_factor = 0.3;
quiver(col + cell_size / 2, row + cell_size / 2, ...
scale_factor * max_value * cos(angle_rad), ...
scale_factor * max_value * sin(angle_rad), ...
'color', 'w', 'LineWidth', 1);
end
end
title('HOG Features Visualization for Dominant Orientation per Cell');
hold off;

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 KiB

View File

@ -0,0 +1,266 @@
%! TeX program = lualatex
\documentclass[a4paper]{article}
% packages
\usepackage{microtype} % Slightly tweak font spacing for aesthetics
\usepackage[english]{babel} % Language hyphenation and typographical rules
\usepackage[final, colorlinks = true, urlcolor = black, linkcolor = black]{hyperref}
\usepackage{changepage} % adjust margins on the fly
\usepackage{fontspec}
\setmainfont{EB Garamond}
\setmonofont[Scale=MatchLowercase]{Deja Vu Sans Mono}
\usepackage{minted}
\usemintedstyle{algol_nu}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{width=\textwidth,compat=1.9}
\usepackage{caption}
\newenvironment{code}{\captionsetup{type=listing}}{}
\captionsetup[listing]{skip=0pt}
\setlength{\abovecaptionskip}{5pt}
\setlength{\belowcaptionskip}{5pt}
\usepackage[yyyymmdd]{datetime}
\renewcommand{\dateseparator}{--}
\usepackage{titlesec}
% \titleformat{\section}{\LARGE\bfseries}{}{}{}[\titlerule]
% \titleformat{\subsection}{\Large\bfseries}{}{0em}{}
% \titlespacing{\subsection}{0em}{-0.7em}{0em}
%
% \titleformat{\subsubsection}{\large\bfseries}{}{0em}{$\bullet$ }
% \titlespacing{\subsubsection}{1em}{-0.7em}{0em}
% margins
\addtolength{\hoffset}{-2.25cm}
\addtolength{\textwidth}{4.5cm}
\addtolength{\voffset}{-3.25cm}
\addtolength{\textheight}{5cm}
\setlength{\parskip}{0pt}
\setlength{\parindent}{0in}
% \setcounter{secnumdepth}{0}
\begin{document}
\hrule \medskip
\begin{minipage}{0.295\textwidth}
\raggedright
\footnotesize
\begin{tabular}{@{}l l} % Define a two-column table with left alignment
Name: & Andrew Hayes \\
Student ID: & 21321503 \\
\end{tabular}
\end{minipage}
\begin{minipage}{0.4\textwidth}
\centering
\vspace{0.4em}
\LARGE
\textsc{ct404} \\
\end{minipage}
\begin{minipage}{0.295\textwidth}
\raggedleft
\footnotesize
\begin{tabular}{@{}l l} % Define a two-column table with left alignment
Name: & Brian Moyles \\
Student ID: & 21333461\\
\end{tabular}
\end{minipage}
\smallskip
\hrule
\begin{center}
\normalsize
Lab Assignment 2: HOG
\end{center}
\hrule
\section{HOG Feature Extraction on Your Own Image (\mintinline{matlab}{cell_size=4}, \mintinline{matlab}{block_size=2})}
\noindent
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/1_gamma-normalised.png}
\caption{Gamma-normalised image}
\end{figure}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/1_gradient_magnitude.png}
\caption{Gradient magnitude of the image}
\end{figure}
\end{minipage}
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/1_gradient_orientation.png}
\caption{Gradient orientation visualisation using Quiver}
\end{figure}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/1_hog_features.png}
\caption{HOG features visualisation for the dominant orientation per cell}
\end{figure}
\end{minipage}
\section{Experiment with HOG Parameters}
\subsection{\mintinline{matlab}{cell_size = 8}, \mintinline{matlab}{block_size=2}}
\noindent
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/2_1_gamma-normalised.png}
\caption{Gamma-normalised image}
\end{figure}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/2_1_gradient_magnitude.png}
\caption{Gradient magnitude of the image}
\end{figure}
\end{minipage}
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/2_1_gradient_orientation.png}
\caption{Gradient orientation visualisation using Quiver}
\end{figure}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/2_1_hog_features.png}
\caption{HOG features visualisation for the dominant orientation per cell}
\end{figure}
\end{minipage}
\subsection{\mintinline{matlab}{cell_size = 16}, \mintinline{matlab}{block_size=4}}
\noindent
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/2_2_gamma-normalised.png}
\caption{Gamma-normalised image}
\end{figure}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/2_2_gradient_magnitude.png}
\caption{Gradient magnitude of the image}
\end{figure}
\end{minipage}
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/2_2_gradient_orientation.png}
\caption{Gradient orientation visualisation using Quiver}
\end{figure}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/2_2_hog_features.png}
\caption{HOG features visualisation for the dominant orientation per cell}
\end{figure}
\end{minipage}
\subsection{How does changing the cell size \& block size affect the HOG visualisation?}
A smaller cell size captures finer details \& small-scale gradients in the image, resulting in more local features and a dense \& detailed visualisation.
However, it's also more sensitive to noise, especially around the hair area where there are less prominent edges.
A larger cell size, on the other hand, results in a coarser representation with reduced sensitivity to small details \& noise, but with potential to miss finer-detailed structures like ears, and an overall sparser \& less detailed visualisation.
\\\\
A smaller block size focuses on local normalisation of gradient features, maintaining fine details and ensuring that local contrast variations are accounted for which can improve robustness in areas with significant intensity variation, such as around the eyes.
A larger block size averages features over a broader area, resulting in more ``global'' normalisation that can make it less sensitive to fine details \& noise.
\subsection{Which cell size \& block size provide the most visually distinguishable features?}
We would recommend the 8$\times$8 cell size with 2$\times$2 block size as it captures the important details \& overall structure of the face without introducing too much noise into the visualisation, without losing too much detail.
The original results obtained with \mintinline{matlab}{cell_size = 4}, \mintinline{matlab}{block_size=2} had a lot of noise compared to results obtained with \mintinline{matlab}{cell_size = 8}, \mintinline{matlab}{block_size=2}.
Furthermore, the results obtained with \mintinline{matlab}{cell_size = 16}, \mintinline{matlab}{block_size=4} were far too coarse to be visually distinguishable, and left out a lot of important details.
\section{HOG Feature Extraction on a Car or Truck Image}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{../code/truck.jpg}
\caption{Original truck image}
\end{figure}
\noindent
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/3_gamma-normalised.png}
\caption{Gamma-normalised image}
\end{figure}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/3_gradient_magnitude.png}
\caption{Gradient magnitude of the image}
\end{figure}
\end{minipage}
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/3_gradient_orientation.png}
\caption{Gradient orientation visualisation using Quiver}
\end{figure}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/3_hog_features.png}
\caption{HOG features visualisation for the dominant orientation per cell}
\end{figure}
\end{minipage}
\subsection{Differences in HOG Representation for Truck Image vs Face Image}
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/1_hog_features.png}
\caption{HOG features visualisation for the face image}
\end{figure}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{./images/3_hog_features.png}
\caption{HOG features visualisation for the truck image}
\end{figure}
\end{minipage}
The truck image contains a more straight lines than the face image, as it is an inorganic, regular object while the face image has more curved lines due to organic facial structure.
A large blank area can be seen in the HOG visualisation for the truck as the blank white side panel of the truck contains no edges or changes in intensity.
There is significantly less noise in the HOG visualisation for the truck due to the comparatively simpler \& more regular shape \& colours of the truck image.
\section{Conclusion}
\subsection{What effect did changing the \mintinline{matlab}{cell_size} \& \mintinline{matlab}{block_size} parameters have on the HOG features?}
Increasing the cell sizes \& block sizes reduced noise in the resulting HOG visualisations, but began to also remove important details \& features of the image as the cell size \& block size were further increased.
\subsection{How did the HOG features of your face compare to those of a car/truck?}
The HOG features of the face were significantly noisier, had greater curvature, and were less regular than the HOG features of the truck image due to one being a natural object and the other being man-made.
\subsection{Which parameter settings do you think would work best for distinguishing between different types of images?}
We would recommend the 8$\times$8 cell size with 2$\times$2 block size as it captures the important details \& overall structure of the face without introducing too much noise into the visualisation, without losing too much detail.
\end{document}

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB