Skip to content

Commit 78cc189

Browse files
committed
adding first visualizations and rough draft for chapters
1 parent 550edaf commit 78cc189

File tree

10 files changed

+329
-82
lines changed

10 files changed

+329
-82
lines changed

contents/convolutions/1d/1d.md

Lines changed: 200 additions & 45 deletions
Large diffs are not rendered by default.

contents/convolutions/2d/2d.md

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,118 @@
1-
# Convolutions on images
1+
# Convolutions on Images
22

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.
55

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:
78

8-
ADD CODE AND VIDEO
9+
ADD ANIMATION
910

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:
1113

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.
1418

1519
## The Gaussian kernel
1620

1721
The Gaussian kernel serves as an effective *blurring* operation for images.
1822
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.
1923

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.
2053

2154
## The Sobel operator
2255

2356
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.
2557
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.
27116

28117
<script>
29118
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);

contents/convolutions/convolutional_theorem/convolutional_theorem.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
# Convolutional Theorem
22

3-
Important note: this particular section will be expanded upon after the Fourier transform and fast Fourier transform chapters have been revised.
3+
Important note: this particular section will be expanded upon after the Fourier transform and fast Fourier transform (FFT) chapters have been revised.
44

55
Now, let me tell you about a bit of computational magic:
66

77
**Convolutions can be performed with Fourier Transforms!**
88

9-
That is crazy, but it's also incredibly hard to explain, so let me do my best.
9+
This is crazy, but it is also incredibly hard to explain, so let me do my best.
1010
As described in the chapter on [Fourier Transforms](../cooley_tukey/cooley_tukey.md), Fourier Transforms allow programmers to move from real space to frequency space.
11-
When we transform a wave into frequency space, we see a single peak in frequency space related to the frequency of that wave.
11+
When we transform a wave into frequency space, we can see a single peak in frequency space related to the frequency of that wave.
1212
No matter what function we send into a Fourier Transform, the frequency-space image can be interpreted as a series of different waves with a specified frequency.
13+
Each of these waves is parameterized by another $$e^{2\pi i k n / N}$$ term, where $$k$$ is the element's value in the frequency domain, $$n$$ is its value in the time domain, and $$N$$ is the overall length of the signal.
14+
In this way, each wave can be seen as a complex exponential.
1315

1416
So here's the idea: if we take two functions $$f(x)$$ and $$g(x)$$ and move them to frequency space to be $$\hat f(\xi)$$ and $$\hat g(\xi)$$, we can then multiply those two functions and transform them back into a third function to blend the signals together.
1517
In this way, we will have a third function that relates the frequency-space images of the two input functions.
16-
This is precisely a convolution.
17-
18-
This is because of something known as the *convolution theorem* which looks something like this:
18+
This is known as the *convolution theorem* which looks something like this:
1919

2020
$$\mathcal{F}(f*g) = \mathcal{F}(f) \cdot \mathcal{F}(g)$$
2121

2222
Where $$\mathcal{F}$$ denotes the Fourier Transform.
23-
Now, by using a Fast Fourier Transform (FFT) in code, this can take a standard convolution on two arrays of length $$n$$, which is an $$\mathcal{O}(n^2)$$ process, to $$\mathcal{O}(n\log(n))$$.
24-
This means that the convolution theorem is fundamental to creating fast convolutional methods for large inputs, assuming that both of the input signals are similar sizes.
25-
That said, it is debatable whether the convolution theorem will be faster when the filter size is small.
26-
Also: depending on the language used, we might need to read in a separate library for FFT's.
23+
24+
At first, this might not seem particularly intuitive, but remember that frequency space is essentially a set of exponentials.
25+
As mentioned in the section about [convolutions in one dimension](../1d/1d.md), multiplication in base 10 space is also a convolution, therefore the convolutional theorem extends this concept into multiplication with *any* set of exponentials, not just base 10.
26+
Obviously, this description is still lacking a bit of explanation, but I promise we will add more when revising the Fourier transform sections!
27+
28+
By using a Fast Fourier Transform (FFT) in code, this can take a standard convolution on two arrays of length $$n$$, which is an $$\mathcal{O}(n^2)$$ process, to $$\mathcal{O}(n\log(n))$$.
29+
This means that the convolution theorem is fundamental to creating fast convolutional methods for certain large inputs.
2730

2831
{% method %}
2932
{% sample lang="jl" %}
30-
That said, Julia has an in-built fft routine, so the code for this method could not be simpler:
3133
[import:19-22, lang:"julia"](../code/julia/conv.jl)
32-
Where the `.*` operator is an element-wise multiplication.
3334
{% sample lang="hs" %}
3435
[import:11-14, lang:"haskell"](../code/haskell/convolution.hs)
35-
Where the `.*` operator is an element-wise multiplication.
3636
{% sample lang="c"%}
3737
[import:20-30, lang:"c"](../code/c/convolutions.c)
3838
{% sample lang="cpp"%}
@@ -42,9 +42,7 @@ Where the `.*` operator is an element-wise multiplication.
4242
{% endmethod %}
4343

4444
This method also has the added advantage that it will *always output an array of the size of your signal*; however, if your signals are not of equal size, we need to pad the smaller signal with zeros.
45-
Also note that the Fourier Transform is a periodic or cyclical operation, so there are no real edges in this method, instead the arrays "wrap around" to the other side.
46-
For this reason, this convolution is often called a *cyclic convolution* instead of a *linear convolution* like above.
47-
Note that cyclic convolutions can definitely still be done without Fourier Transforms and we can do linear convolutions with Fourier Transforms, but it makes the code slightly more complicated than described above.
45+
Also note that the Fourier Transform is a periodic or cyclical operation, so there are no real edges in this method, instead the arrays "wrap around" to the other side, creating a cyclic convolution like we showed in the periodic bounday condition case for the [one-dimensional convolution](../1d/1d.md).
4846

4947
<script>
5048
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);

contents/convolutions/convolutions.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
# Convolutions
22
To put it bluntly, convolutions can be confusing.
3-
Not only are they hard to describe, but if they are not used in practice, it's hard to understand why they woudl ever be needed.
3+
Some might even call them *convoluted*!
4+
(Get it? Because we are talking about *convolutions*? A wise man once told me that all good jokes need additional clarification.)
5+
6+
Not only are convolutions hard to describe, but if they are not used in practice, it is hard to understand why they would ever be needed.
47
I'm going to do what I can to describe them in an intuitive way; however, I may need to come back to this in the future.
5-
Let me know if there is anything here that is unclear, and I'll do what I can to clear it up.
8+
Let me know if there is anything here that is unclear, and I will do what I can to clear it up.
69

710
As always, we should start at the start.
8-
If you take two functions $$f(x)$$ and $$g(x)$$, there are a number of ways you can combine them.
11+
If you take two functions $$f$$ and $$g$$, there are a number of ways you can combine them.
912
All basic operations can do this (addition, subtraction, multiplication, and division), but there are also special operations that only work with functions and do not work on standard variables or numbers.
10-
For example, $$f \circ g$$ is a *composition* of the two functions, where you plug $$g(x)$$ into $$f(x)$$.
13+
For example, $$f \circ g$$ is a *composition* of the two functions, where you plug $$g(x)$$ into $$f$$.
1114
A convolution is another function-related operation, and is often notated with a star ($$*$$) operator, where
1215

13-
$$f(x)*g(x)=c(x)$$
16+
$$
17+
f*g=c
18+
$$
1419

15-
provides a third function $$c(x)$$ that is a blended version of $$f(x)$$ and $$g(x)$$.
16-
This concept is known as a *convolution*, and as a rather important side-note: there is an incredibly similar operator known as a *correlation* which will be discussed in the near future.
17-
Now we are left with a rather vague question: How do we *blend* functions?
20+
provides a third function $$c$$ that is a blended version of $$f$$ and $$g$$.
21+
This concept is known as a *convolution*, and as a rather important side-note: there is an incredibly similar operation known as a *correlation* which will be discussed in the near future.
22+
Now we are left with a rather vague question: how do we *blend* functions?
1823

19-
To answer this question, we will need to show off a few simple graphics or animations in the [Convolutions in 1D](1d/1d.md) section before discussing the mathematical definition.
20-
We will then move on to the typical application of convolutions to images in the [Convolutions of images](2d/2d.md) section, where we will discuss two important filters: the Gaussian kernel and the Sobel operator.
21-
As a note: convolutions can be extended to $n$-dimensions, but after seeing how it is extended to two dimensions, it should be possible for the reader to extend it to three dimensions and beyond if that is needed, so we will not be covering that in great detail here unless is is useful for another algorithm.
24+
To answer this question, we will need to show off a few simple graphics or animations in the [Convolutions in 1D](1d/1d.md) section while also discussing the mathematical definition.
25+
We will then move on to the most stereotypical application of convolutions to images in the [Convolutions of Images](2d/2d.md) section, where we will discuss two important filters: the Gaussian kernel and the Sobel operator.
26+
As a note: convolutions can be extended to $$n$$-dimensions, but after seeing how they are extended to two dimensions, it should be possible for the reader to extend it to three dimensions and beyond if that is needed, so we will not cover that in great detail here unless is is useful for another algorithm.
2227
In addition, we will be touching on a rather difficult but powerful topic with the [Convolutional Theorem](convolutional_theorem/convolutional_therem.md) section where convolutions can be computed by using [Fourier transforms](../Cooley_tukey/cooley_tukey.md).
2328

2429
<script>
Binary file not shown.
403 KB
Binary file not shown.
Binary file not shown.
14.5 KB
Loading
Binary file not shown.
14.9 KB
Loading

0 commit comments

Comments
 (0)