diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/2_image_enhancement.py b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/2_image_enhancement.py new file mode 100644 index 00000000..b5e37b9d --- /dev/null +++ b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/2_image_enhancement.py @@ -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)) diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/3_thresholding.py b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/3_thresholding.py new file mode 100644 index 00000000..b5e53ea4 --- /dev/null +++ b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/3_thresholding.py @@ -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)) diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/4_noise_removal.py b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/4_noise_removal.py new file mode 100644 index 00000000..85ca5c5a --- /dev/null +++ b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/4_noise_removal.py @@ -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) diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/contrast_stretched.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/contrast_stretched.jpg new file mode 100644 index 00000000..aac12be4 Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/contrast_stretched.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/histogram_equalised.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/histogram_equalised.jpg new file mode 100644 index 00000000..bcfe3f7f Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/histogram_equalised.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_1.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_1.jpg new file mode 100644 index 00000000..9fc1e077 Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_1.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_11.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_11.jpg new file mode 100644 index 00000000..0077594f Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_11.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_13.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_13.jpg new file mode 100644 index 00000000..f4d0238d Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_13.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_15.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_15.jpg new file mode 100644 index 00000000..909ca0c4 Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_15.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_3.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_3.jpg new file mode 100644 index 00000000..87a3fcbf Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_3.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_5.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_5.jpg new file mode 100644 index 00000000..169a7406 Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_5.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_7.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_7.jpg new file mode 100644 index 00000000..37b927dc Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_7.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_9.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_9.jpg new file mode 100644 index 00000000..3617a18a Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/kernel_size_9.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/otsu.jpg b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/otsu.jpg new file mode 100644 index 00000000..adeb64e4 Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/code/task1/output/otsu.jpg differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/CT404-Assignment-2.pdf b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/CT404-Assignment-2.pdf index 0e5584be..0d45725d 100644 Binary files a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/CT404-Assignment-2.pdf and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/CT404-Assignment-2.pdf differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/CT404-Assignment-2.tex b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/CT404-Assignment-2.tex index 4947e5b2..bfa9163e 100644 --- a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/CT404-Assignment-2.tex +++ b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/CT404-Assignment-2.tex @@ -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} diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/images/2_image_enhancement_output.png b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/images/2_image_enhancement_output.png new file mode 100644 index 00000000..e8aec589 Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/images/2_image_enhancement_output.png differ diff --git a/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/images/3_thresholding_output.png b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/images/3_thresholding_output.png new file mode 100644 index 00000000..de2869ca Binary files /dev/null and b/year4/semester1/CT404: Graphics & Image Processing/assignments/assignment2/latex/images/3_thresholding_output.png differ