[CT404]: Assignment 2 progress

This commit is contained in:
2024-11-04 00:41:03 +00:00
parent 316c5570ed
commit 418cd5b2a5
18 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,18 @@
# Task 1.2: Image Enhancement
import cv2
# read in chosen single-channel greyscale image
image = cv2.imread("./output/g_channel_greyscale.jpg", cv2.IMREAD_GRAYSCALE)
# apply histogram equalisation
equalised_image = cv2.equalizeHist(image)
equalised_image_contrast = equalised_image.std()
cv2.imwrite("./output/histogram_equalised.jpg", equalised_image)
# apply contrast stretching
stretched_image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX)
stretched_image_contrast = stretched_image.std()
cv2.imwrite("./output/contrast_stretched.jpg", stretched_image)
print("Histogram Equalisation Contrast: " + str(equalised_image_contrast))
print("Contrast Stretching Contrast: " + str(stretched_image_contrast))

View File

@ -0,0 +1,11 @@
# Task 1.3: Thresholding
import cv2
# read in chosen enhanced image
image = cv2.imread("./output/histogram_equalised.jpg", cv2.IMREAD_GRAYSCALE)
# perform otsu thresholding to find the optimal threshold
threshold_value, otsu_thresholded = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imwrite("./output/otsu.jpg", otsu_thresholded)
print("Threshold value used: " + str(threshold_value))

View File

@ -0,0 +1,14 @@
# Task 1.4: Noise Removal
import cv2
# read in thresholded image
image = cv2.imread("./output/otsu.jpg", cv2.IMREAD_GRAYSCALE)
# try several different sizes of structuring element (must be odd)
for kernel_size in range(1, 16, 2):
# define a disk-shaped structuring element
structuring_element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size))
# apply morphological opening to remove noise
opened_image = cv2.morphologyEx(image, cv2.MORPH_OPEN, structuring_element)
cv2.imwrite(f"./output/kernel_size_{kernel_size}.jpg", opened_image)

Binary file not shown.

After

Width:  |  Height:  |  Size: 308 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 277 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 310 KiB

View File

@ -134,5 +134,89 @@ My selected single-channel image is the greyscale version of the green-channel-o
\end{figure}
\subsection{Image Enhancement}
\begin{code}
\inputminted[linenos, breaklines, frame=single]{python}{../code/task1/2_image_enhancement.py}
\caption{\mintinline{python}{2_image_enhancement.py}}
\end{code}
\begin{figure}[H]
\centering
\includegraphics[width=\textwidth]{./images/2_image_enhancement_output.png}
\caption{Output of \mintinline{python}{2_image_enhancement.py}}
\end{figure}
I chose to use the histogram equalisation technique as it gave the best contrast, as seen from the calculated standard deviation in contrast above and in the output images below.
\begin{minipage}{0.49\textwidth}
\centering
\includegraphics[width=\textwidth]{../code/task1/output/histogram_equalised.jpg}
\captionof{figure}{Histogram-equalised image}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\centering
\includegraphics[width=\textwidth]{../code/task1/output/contrast_stretched.jpg}
\captionof{figure}{Contrast-stretched image}
\end{minipage}
\subsection{Thresholding}
\begin{code}
\inputminted[linenos, breaklines, frame=single]{python}{../code/task1/3_thresholding.py}
\caption{\mintinline{python}{3_thresholding.py}}
\end{code}
\begin{figure}[H]
\centering
\includegraphics[width=\textwidth]{./images/3_thresholding_output.png}
\caption{Output of \mintinline{python}{3_thresholding.py}}
\end{figure}
I used Otsu's algorithm to find the optimal threshold value that best separated the foreground (objects of interest) from the background.
As can be seen from the above output, the optimal value chosen was 129.
\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{../code/task1/output/otsu.jpg}
\caption{Image with Otsu thresholding}
\end{figure}
\subsection{Noise Removal}
\begin{code}
\inputminted[linenos, breaklines, frame=single]{python}{../code/task1/4_noise_removal.py}
\caption{\mintinline{python}{4_noise_removal.py}}
\end{code}
\noindent
\begin{minipage}{0.24\textwidth}
\centering
\includegraphics[width=\textwidth]{../code/task1/output/kernel_size_1.jpg}
\captionof{figure}{\mintinline{python}{kernel_size = 1}}
\includegraphics[width=\textwidth]{../code/task1/output/kernel_size_3.jpg}
\captionof{figure}{\mintinline{python}{kernel_size = 3}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
\centering
\includegraphics[width=\textwidth]{../code/task1/output/kernel_size_5.jpg}
\captionof{figure}{\mintinline{python}{kernel_size = 5}}
\includegraphics[width=\textwidth]{../code/task1/output/kernel_size_7.jpg}
\captionof{figure}{\mintinline{python}{kernel_size = 7}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
\centering
\includegraphics[width=\textwidth]{../code/task1/output/kernel_size_9.jpg}
\captionof{figure}{\mintinline{python}{kernel_size = 9}}
\includegraphics[width=\textwidth]{../code/task1/output/kernel_size_11.jpg}
\captionof{figure}{\mintinline{python}{kernel_size = 11}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
\centering
\includegraphics[width=\textwidth]{../code/task1/output/kernel_size_13.jpg}
\captionof{figure}{\mintinline{python}{kernel_size = 13}}
\includegraphics[width=\textwidth]{../code/task1/output/kernel_size_15.jpg}
\captionof{figure}{\mintinline{python}{kernel_size = 15}}
\end{minipage}
\end{document}