|
1 |
| -# Convolutions on images |
| 1 | +# Convolutions on Images |
2 | 2 |
|
3 |
| -Unlike the section on [one-dimensional convolutions](../1d/1d.md), for this section, we will no longer be focusing on signals, but instead images. |
4 |
| -For the purposes of this chapter, an image will be an array filled with elements with some red, green, and blue value associated with it; however, for the code examples, greyscale images may be used where each array element is simply composed of some floating-point value. |
| 3 | +For this section, we will no longer be focusing on signals, but instead images, where an image is an array filled with elements of red, green, and blue values. |
| 4 | +That said, for the code examples, greyscale images may be used such that each array element is simply composed of some floating-point value. |
5 | 5 |
|
6 |
| -In this case, extending the one-dimensional convolution to two-dimensions is a relatively straightforward task, but indexing still requires some thought. |
| 6 | +The extension of one-dimensional convolutions to two dimensions requires a little thought about indexing and the like, but is ultimately the same operation. |
| 7 | +Here is an animation of a convolution for a two-dimensional image: |
7 | 8 |
|
8 |
| -ADD CODE AND VIDEO |
| 9 | +ADD ANIMATION |
9 | 10 |
|
10 |
| -# Common filters |
| 11 | +In this case, we convolved the image with a 3x3 square filter. |
| 12 | +In code, using simple boundaries, it would look like this: |
11 | 13 |
|
12 |
| -For image processing, there are quite a few relatively common filters to use for various tasks. |
13 |
| -For this section, we will cover 2 of them: Gaussian and Sobel. |
| 14 | +ADD CODE |
| 15 | + |
| 16 | +It is worth highlighting common filters used for convolutions of images. |
| 17 | +In particular, we will further discuss the Gaussian filter introduced in the section on [one-dimensional convolutions](../1d/1d.md), and then introduce another set of kernels known as Sobel operators, which are used for naive edge detection or image derivatives. |
14 | 18 |
|
15 | 19 | ## The Gaussian kernel
|
16 | 20 |
|
17 | 21 | The Gaussian kernel serves as an effective *blurring* operation for images.
|
18 | 22 | Like we showed with the triangle filter in the section on [one-dimensional convolutions](../1d/1d.md), the Gaussian kernel effectively adds a small amount of the neighboring elements to each pixel in an image.
|
19 | 23 |
|
| 24 | +As a reminder, the foruma for any Gaussian distribution is |
| 25 | + |
| 26 | +$$ |
| 27 | +g(x,y) = \frac{1}{2\pi\sigma^2}e^{-\frac{x^2+y^2}{2\sigma^2}}, |
| 28 | +$$ |
| 29 | + |
| 30 | +where $$\sigma$$ is the standard deviation and is a measure of the width of the Gaussian. |
| 31 | +A larger $$\sigma$$ means a larger Gaussian; however, Remember that the Gaussian must fit onto the filter! |
| 32 | +As a general rule of thumb, the larger the filter and standard deviation, the more "smeared" the final convolution will be. |
| 33 | + |
| 34 | +At this stage, it is important to write some code, so we will generate a simple function to return a Gaussian kernel with a specified standard deviation and filter size. |
| 35 | +As a note, all the kernels will be scaled at the end by the sum of all internal elements. |
| 36 | +This simply ensures that the output of the convolution does not have an obnoxious scale factor associated with it. |
| 37 | + |
| 38 | +ADD CODE |
| 39 | + |
| 40 | + |
| 41 | +Below are a few kernels generated with the above code along with their application to a ___ image. |
| 42 | + |
| 43 | +Add 1 + 2x4 image with original image going into each lane and then showing the convolution result for small, large, and somewhat in-between. |
| 44 | + |
| 45 | +In (a), we show a 3x3 kernel with a standard deviation of 1. |
| 46 | +In (b), we show a 10x10 kernel with a standard deviation of 5. |
| 47 | +In (c), we show a 3x3 kernel with a standard deviation of 5. |
| 48 | +Finally, in (d), we show a 10x10 kernel with a standard deviation of 1. |
| 49 | + |
| 50 | +We see that (a) and (d) are somewhat similar, (b) has the most blurred boundary, and (c) is partially blurred. |
| 51 | + |
| 52 | +There is a bit more to this, but I think this is a good place to wrap up the discussion on the Gaussian blurring operation before moving onto a slightly more complex convolutional method: the Sobel operator. |
20 | 53 |
|
21 | 54 | ## The Sobel operator
|
22 | 55 |
|
23 | 56 | The Sobel operator effectively performs a gradient operation on an image by highlighting areas where a large change has been made and can be considered a naive edge detector.
|
24 |
| -It is also the first non-trivial example of convolution. |
25 | 57 | That is to say that the $$n$$-dimensional Sobel operator is composed of $$n$$ separate gradient convolutions that are then combined together into one, output array.
|
26 |
| - |
| 58 | +Again, for the purposes of this chapter, we will stick to two dimensions, which will be composed of two separate gradients along the $$x$$ and $$y$$ directions. |
| 59 | +Each gradient will be created by convolving our image with their corresponding Sobel operator: |
| 60 | + |
| 61 | +$$ |
| 62 | +\begin{align} |
| 63 | +S_x &= \left(\begin{bmatrix} |
| 64 | +1 \\ |
| 65 | +2 \\ |
| 66 | +1 \\ |
| 67 | +\end{bmatrix} \otimes [1~0~-1] |
| 68 | +\right) = \begin{bmatrix} |
| 69 | +1 & 0 & -1 \\ |
| 70 | +2 & 0 & -2 \\ |
| 71 | +1 & 0 & -1 \\ |
| 72 | +\end{bmatrix}\\ |
| 73 | +
|
| 74 | +S_y &= \left( |
| 75 | +\begin{bmatrix} |
| 76 | +1 \\ |
| 77 | +0 \\ |
| 78 | +-1 \\ |
| 79 | +\end{bmatrix} \otimes [1~2~1] |
| 80 | +\right) = \begin{bmatrix} |
| 81 | +1 & 2 & 1 \\ |
| 82 | +0 & 0 & 0 \\ |
| 83 | +-1 & -2 & -1 \\ |
| 84 | +\end{bmatrix}. |
| 85 | +\end{align} |
| 86 | +$$ |
| 87 | + |
| 88 | +The gradients can then be found with a convolution, such that: |
| 89 | + |
| 90 | +$$ |
| 91 | +\begin{align} |
| 92 | +G_x &= S_x*A \\ |
| 93 | +G_y &= S_y*A. |
| 94 | +\end{align} |
| 95 | +$$ |
| 96 | + |
| 97 | +Finally, these gradients can be summed in quadrature to find the total Sobel operator or image gradient: |
| 98 | + |
| 99 | +$$ |
| 100 | +G_{\text{total}} = \sqrt{G_x^2 + G_y^2} |
| 101 | +$$ |
| 102 | + |
| 103 | +At this point, it does not likely make sense, so let us now show what it does in practice: |
| 104 | + |
| 105 | +ADD IMAGE |
| 106 | + |
| 107 | +Here, we see that the edges in the image are highlighted. |
| 108 | +This is why the Sobel operators are also known as naive edge detectors and are integral components to many more sophisticated edge detection methods like one proposed by Canny [CITE]. |
| 109 | + |
| 110 | +For now, it is a good idea to show what this operation might look like in code: |
| 111 | + |
| 112 | +ADD CODE |
| 113 | + |
| 114 | +With that, I believe we are ate a good place to stop discussions on two-dimensional convolutions. |
| 115 | +We will definitely return to this topic in the future as new algorithms require more information. |
27 | 116 |
|
28 | 117 | <script>
|
29 | 118 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
|
|
0 commit comments