You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contents/convolutions/1d/1d.md
+13-6Lines changed: 13 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,3 @@
1
-
# TODO:
2
-
3
-
* Make sure cyclic convolution works with larger filter
4
-
* LICENSING
5
-
6
-
7
1
# Convolutions in 1D
8
2
As mentioned in the [introductory section for convolutions](../convolutions.md), convolutions are methods that allow mathematicians to "blend" two seemingly unrelated functions; however, this definition is not very rigorouos, so it might be better to think of a convolution as a method to apply a filter to a signal or image.
9
3
This, of course, brings up more questions: what is a filter? What is a signal? How is this all related to images?
The code examples are licensed under the MIT license (found in [LICENSE.md](https://github.com/algorithm-archivists/algorithm-archive/blob/master/LICENSE.md)).
351
345
346
+
##### Images/Graphics
347
+
- The image "[Square Wave](../res/square_wave.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
348
+
- The image "[Triangle Wave](../res/triangle_wave.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
349
+
- The video "[Triangle Square Convolution](../res/triangle_square_conv.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
350
+
- The video "[Gaussian Square Convolution](../res/1d_gaussian.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
351
+
- The video "[Gaussian Random Convolution](../res/1d_rand_gaussian_cyclic.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
352
+
- The video "[Double Convolution](../res/double_gaussian.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
353
+
- The image "[Sawtooth Wave](../res/sawtooth.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
354
+
- The video "[Sawtooth Square Convolution](../res/1d_sawtooth.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
355
+
- The video "[Full Random Convolution](../res/1d_rand_gaussian_full.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
356
+
- The video "[Simple Random Convolution](../res/1d_rand_gaussian_simple.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
357
+
358
+
352
359
##### Text
353
360
354
361
The text of this chapter was written by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
Copy file name to clipboardExpand all lines: contents/convolutions/2d/2d.md
+72-25Lines changed: 72 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -2,16 +2,29 @@
2
2
3
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
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
+
In addition, we will not be discussing boundary conditions too much in this chapter and will instead be using the simple boundaries introduced in the section on [one-dimensional convolutions](../1d/1d.md).
5
6
6
7
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
8
Here is an animation of a convolution for a two-dimensional image:
8
9
9
-
ADD ANIMATION
10
+
<divstyle="text-align:center">
11
+
<videostyle="width:90%"controlsloop>
12
+
<sourcesrc="../res/2d.mp4"type="video/mp4">
13
+
Your browser does not support the video tag.
14
+
</video>
15
+
</div>
10
16
11
-
In this case, we convolved the image with a 3x3 square filter.
12
-
In code, using simple boundaries, it would look like this:
17
+
In this case, we convolved the image with a 3x3 square filter, all filled with values of $$\frac{1}{9}$$.
18
+
This created a simple blurring effect, which is somewhat expected from the previous section.
19
+
In code, using simple boundaries, a two-dimensional convolution might look like this:
This code is very similar to what we have shown in previous sections; however, it essentially requires 4 iterable dimensions because we need to iterate through each element of the output domain *and* the filter.
27
+
Because the indexing is slightly non-trivial, we felt it was worth writing a separate chapter for two-dimensional convolutions.
15
28
16
29
It is worth highlighting common filters used for convolutions of images.
17
30
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.
@@ -21,40 +34,47 @@ In particular, we will further discuss the Gaussian filter introduced in the sec
21
34
The Gaussian kernel serves as an effective *blurring* operation for images.
22
35
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.
23
36
24
-
As a reminder, the foruma for any Gaussian distribution is
37
+
As a reminder, the formula for any Gaussian distribution is
where $$\sigma$$ is the standard deviation and is a measure of the width of the Gaussian.
31
44
A larger $$\sigma$$ means a larger Gaussian; however, Remember that the Gaussian must fit onto the filter!
45
+
As a note, some definitions of $$\sigma$$ allow users to have a separate deviation in $$x$$ and $$y$$, but for the purposes of this chapter, we will assume they are the same.
32
46
As a general rule of thumb, the larger the filter and standard deviation, the more "smeared" the final convolution will be.
33
47
34
48
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.
Though it is entirely possible to create a Gaussian kernel whose standard deviation is independent on the kernel size, we have decided to keep them the same for this chapter.
56
+
As a note, all the kernels will be scaled at the end by the sum of all internal elements.
57
+
This simply ensures that the output of the convolution does not have an obnoxious scale factor associated with it.
40
58
41
-
Below are a few kernels generated with the above code along with their application to a ___ image.
59
+
Below are a few kernels generated with the above code along with their application to a black and white heart image.
42
60
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.
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
65
50
-
We see that (a) and (d) are somewhat similar, (b) has the most blurred boundary, and (c) is partially blurred.
66
+
In (a), we show the original image, which is just a black circle at the center of a $$50\times 50$$ grid.
67
+
In (b), we show the image after convolution with a $$3\times 3$$ kernel.
68
+
In (c), we show the image after convolution with a $$20\times 20$$ kernel.
69
+
Here, we see that (c) is significantly fuzzier than (b), which is a direct consequence of the kernel size.
51
70
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.
71
+
There is a lot more that we could talk about, 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.
53
72
54
73
## The Sobel operator
55
74
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.
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.
75
+
The Sobel operator effectively performs a gradient operation on an image by highlighting areas where a large change has been made.
76
+
In essence, this means that this operation can be considered to be a naïve edge detector.
77
+
That is to say that the $$n$$-dimensional Sobel operator is composed of $$n$$ separate gradient convolutions (one for each dimension) that are then combined together into one, output array.
58
78
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
79
Each gradient will be created by convolving our image with their corresponding Sobel operator:
60
80
@@ -94,36 +114,63 @@ G_y &= S_y*A.
94
114
\end{align}
95
115
$$
96
116
117
+
Here, $$A$$ is out input array or image.
97
118
Finally, these gradients can be summed in quadrature to find the total Sobel operator or image gradient:
98
119
99
120
$$
100
121
G_{\text{total}} = \sqrt{G_x^2 + G_y^2}
101
122
$$
102
123
103
-
At this point, it does not likely make sense, so let us now show what it does in practice:
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].
130
+
In this diagram, we start with the circle image on the right, and then use the $$S_x$$ and $$S_y$$ operators on them to find the gradients along $$x$$ and $$y$$ before summing them in quadrature to get the final image gradient.
131
+
Here, we see that the edges in the image are highlighted, showing the outline of our circle image.
132
+
This is why the Sobel operator is also known as naïve edge detection and is an integral component to many more sophisticated edge detection methods like one proposed by Canny {{ "canny1986computational" | cite }}.
109
133
110
-
For now, it is a good idea to show what this operation might look like in code:
134
+
In code, applying the Sobel operator involves first finding the operator in $$x$$ and $$y$$ and then applying them with a traditional convolution:
With that, I believe we are ate a good place to stop discussions on two-dimensional convolutions.
141
+
With that, I believe we are at a good place to stop discussions on two-dimensional convolutions.
115
142
We will definitely return to this topic in the future as new algorithms require more information.
116
143
144
+
## Example Code
145
+
146
+
For the full code in this section, we have modified the code from the [one-dimensional convolution chapter](../1d/1d.md) to add a two-dimensional variant for an image of random white noise.
147
+
We have also added code to create the Gaussian kernel and Sobel operator.
The code examples are licensed under the MIT license (found in [LICENSE.md](https://github.com/algorithm-archivists/algorithm-archive/blob/master/LICENSE.md)).
126
167
168
+
##### Images/Graphics
169
+
- The image "[8bit Heart](../res/heart_8bit.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
170
+
- The image "[Circle Blur](../res/circle_blur.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
171
+
- The image "[Sobel Filters](../res/sobel_filters.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
172
+
- The video "[2D Convolution](../res/2d.mp4)" was created by [James Schloss](https://github.com/leios) and [Grant Sanderson](https://github.com/3b1b) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
173
+
127
174
##### Text
128
175
129
176
The text of this chapter was written by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
0 commit comments