|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Lab 2\n", |
| 9 | + "\n", |
| 10 | + "In this lab exercise, you will look at applying different noise removal filters and investigate the effect on edge detection.\n", |
| 11 | + "\n", |
| 12 | + "**You are asked to write a short (no more than 2 pages) report of your work**, answering specific questions, and showing example images. This work is not assessed (it will not count toward your module mark) but you will get formative feedback" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": null, |
| 18 | + "metadata": {}, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "# Imports\n", |
| 22 | + "import skimage\n", |
| 23 | + "import scipy\n", |
| 24 | + "import time\n", |
| 25 | + "from matplotlib import pyplot as plt\n", |
| 26 | + "import numpy as np\n", |
| 27 | + "from filters import gaussian_filter_3x3, gaussian_filter_5x5 \n", |
| 28 | + "from utils import show_rgb_image, show_binary_image, sample_gaussian, zero_cross" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "attachments": {}, |
| 33 | + "cell_type": "markdown", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "### Task 1\n", |
| 37 | + "\n", |
| 38 | + "- Load the shakey image using the ```skimage.io.imread``` function\n", |
| 39 | + "\n", |
| 40 | + "- Using the built-in procedure ```scipy.signal.convolve2D``` convolve the image with the 3x3 Gaussian filter, and then the 5x5 filter. Can you see any difference between them? Try applying an edge filter to each and thresholding. Refer to the previous assignment to understand the convolve2d function.\n", |
| 41 | + "\n", |
| 42 | + "**QUESTION 1: Can you describe the effect in comparison with applying the edge filter to the image directly?**" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": null, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "# Complete Task 1 here\n" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "Applying the gaussian filter results in a smoother image. When the original edge image image is compared to the gaussian filtered edge image, there is far less noise in the gaussian filtered edge image." |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "attachments": {}, |
| 63 | + "cell_type": "markdown", |
| 64 | + "metadata": {}, |
| 65 | + "source": [ |
| 66 | + "### Task 2\n", |
| 67 | + "\n", |
| 68 | + "Using the function ```sample_gaussian(std_dev,mean,vec)``` you can create a discrete sample from a Gaussian (Normal) density. You need to specify the mean(keep it at 0, think about why) and the standard deviation **std_dev**. You should also specify an input vector in the form of ```np.arange(min=-x,max=x+1,step=1,dtype=np.float32)``` which will produce a sample of size 2x+1.\n", |
| 69 | + "\n", |
| 70 | + "So you can create larger and smaller vectors by altering the step size or the limits of the vector (the min and max values). \n", |
| 71 | + "\n", |
| 72 | + "Now try creating a 9x9 Gaussian mask. To do this you will need to use matrix multiplication in the right way. \n", |
| 73 | + "\n", |
| 74 | + "Try some initial exploratory experiments with this, what happens to the image as you increase the size of the mask? What happens as you increase the size of **std_dev**? Make detailed notes as you proceed about what you did and what you observed.\n", |
| 75 | + "\n", |
| 76 | + "Now apply gradient operators such as the Sobel operators to the blurred images. What happens to the edges in the heavily blurred case?\n", |
| 77 | + "\n", |
| 78 | + "**Question 2: What is the effect of increasing the size of the Gaussian Filter (3x3 versus 5x5 for example)?** \n", |
| 79 | + "**What is the effect of changing the standard deviation s? Why do you see what you see?**" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "code", |
| 84 | + "execution_count": null, |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [], |
| 87 | + "source": [ |
| 88 | + "\n", |
| 89 | + "# Complete Task 2 here" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "markdown", |
| 94 | + "metadata": {}, |
| 95 | + "source": [ |
| 96 | + "Increasing the standard deviation shifts the weighting of the convolution away from the centre pixel and gives increased weighting to the pixels furtuer out. This results in the image appearing increasingly blurred. " |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "attachments": {}, |
| 101 | + "cell_type": "markdown", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "### Task 3\n", |
| 105 | + "\n", |
| 106 | + "Now compare the speed of applying two large 1D Gaussian filters in sequence, with applying a single equivalent 2D Gaussian filter that results from their multiplication. To test the CPU time used you can use a function called [time.monotonic()](https://docs.python.org/3/library/time.html#time.monotonic) . \n", |
| 107 | + "\n", |
| 108 | + "- Can you detect differences in the CPU times as the mask sizes increase? \n", |
| 109 | + "- You should check that the results are the same by examining areas of the image matrix in detail. \n", |
| 110 | + "- Are there any effects due to small floating point errors? " |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": null, |
| 116 | + "metadata": {}, |
| 117 | + "outputs": [], |
| 118 | + "source": [ |
| 119 | + "# Complete Task 3 here" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "markdown", |
| 124 | + "metadata": {}, |
| 125 | + "source": [ |
| 126 | + "The execution time of applying a 2D convolution is far greater than that of applying two 1D filters. These differences increase as the size of the filter increases. When we compare the number of pixels which are the same we notice that only 7% of pixels are unchanged. However, visually the differences are very hard to distinguish. " |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "attachments": {}, |
| 131 | + "cell_type": "markdown", |
| 132 | + "metadata": {}, |
| 133 | + "source": [ |
| 134 | + "### Task 4\n", |
| 135 | + "\n", |
| 136 | + "- Look at your Lecture notes and produce a 2D Laplacian filter. \n", |
| 137 | + "\n", |
| 138 | + "- Now try applying the Laplacian operator to the Shakey image. You will need to calculate the zero-crossing for edges: you can use the provided zero_corss function, where I_in is the image convolved with the Laplacian, and I_out is the calculated edges. \n", |
| 139 | + "\n", |
| 140 | + "- Think about the result. Why does it produce a poor result compared to the other operators?" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": null, |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [], |
| 148 | + "source": [ |
| 149 | + "# Complete Task 4 here" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "markdown", |
| 154 | + "metadata": {}, |
| 155 | + "source": [ |
| 156 | + "The resulting image is very noisy. This because Laplacian filter is the sum of two second order derivatives in x-and y- axis which amplifies noise easily." |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "attachments": {}, |
| 161 | + "cell_type": "markdown", |
| 162 | + "metadata": {}, |
| 163 | + "source": [ |
| 164 | + "# Task 5\n", |
| 165 | + "\n", |
| 166 | + "I mentioned the Laplacian of the Gaussian in the lecture. How could you combine the idea of \n", |
| 167 | + "the Laplacian operator with the idea of Gaussian smoothing? Try out your ideas." |
| 168 | + ] |
| 169 | + }, |
| 170 | + { |
| 171 | + "cell_type": "code", |
| 172 | + "execution_count": null, |
| 173 | + "metadata": {}, |
| 174 | + "outputs": [], |
| 175 | + "source": [ |
| 176 | + "# Complete Task 5 here" |
| 177 | + ] |
| 178 | + } |
| 179 | + ], |
| 180 | + "metadata": { |
| 181 | + "kernelspec": { |
| 182 | + "display_name": "venv", |
| 183 | + "language": "python", |
| 184 | + "name": "python3" |
| 185 | + }, |
| 186 | + "language_info": { |
| 187 | + "codemirror_mode": { |
| 188 | + "name": "ipython", |
| 189 | + "version": 3 |
| 190 | + }, |
| 191 | + "file_extension": ".py", |
| 192 | + "mimetype": "text/x-python", |
| 193 | + "name": "python", |
| 194 | + "nbconvert_exporter": "python", |
| 195 | + "pygments_lexer": "ipython3", |
| 196 | + "version": "3.12.0" |
| 197 | + }, |
| 198 | + "orig_nbformat": 4, |
| 199 | + "vscode": { |
| 200 | + "interpreter": { |
| 201 | + "hash": "74a34817a5cd2140ee5dc8be93178d611a51b0c87e7dbd285328f223a7951b22" |
| 202 | + } |
| 203 | + } |
| 204 | + }, |
| 205 | + "nbformat": 4, |
| 206 | + "nbformat_minor": 2 |
| 207 | +} |
0 commit comments