Skip to content

Commit 2672cf1

Browse files
committed
Merge branch 'release/0.2.0'
2 parents 86acffd + b861b5f commit 2672cf1

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

.idea/workspace.xml

Lines changed: 19 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Images/grayscale_patch.PNG

8.64 KB
Loading

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
1-
This is a tutorial about applying Sobel filters to images for edge detection.
2-
It is currently under construction!
1+
# Sobel Filter/Operator (Edge Detection)
2+
3+
Let's create a tool to detect edges of an image! To do so we'll use the Sobel operator/filter (there are other techniques out there, e.g. Canny edge detector). The matrices associated with the kernels we'll be using are given by:
4+
5+
<img src="https://latex.codecogs.com/gif.latex?G_x=\begin{bmatrix}&space;1&space;&&space;0&space;&&space;-1&space;\\&space;2&space;&&space;0&space;&&space;-2&space;\\&space;1&space;&&space;0&space;&&space;-1&space;\end{bmatrix}\quad\text{and}\quad&space;G_y=\begin{bmatrix}&space;1&space;&&space;2&space;&&space;1&space;\\&space;0&space;&&space;0&space;&&space;0&space;\\&space;-1&space;&&space;-2&space;&&space;-1&space;\end{bmatrix}" title="G_x=\begin{bmatrix} 1 & 0 & -1 \\ 2 & 0 & -2 \\ 1 & 0 & -1 \end{bmatrix}\quad\text{and}\quad G_y=\begin{bmatrix} 1 & 2 & 1 \\ 0 & 0 & 0 \\ -1 & -2 & -1 \end{bmatrix}" />
6+
7+
Think of Gx and Gy as maps of the form <img src="https://latex.codecogs.com/gif.latex?\mathbb{R}^{3\,\times\,3}\rightarrow\mathbb{R}" />, where we take in a 3 by 3 patch of image (denoted by <img src="https://latex.codecogs.com/gif.latex?$$A\in\mathbb{R}^{3\,\times\,3$$" />), do some operation on it (using the matrices shown above), and return a real number (the value of the pixel for the output image).
8+
9+
So, for example, if we are given the following patch of grayscale image:
10+
11+
![alt-text](Images/grayscale_patch.PNG "Example of a grayscale patch")
12+
13+
which is really the matrix:
14+
15+
<img src="https://latex.codecogs.com/gif.latex?A=\begin{bmatrix}&space;119&space;&&space;80&space;&&space;122&space;\\&space;177&space;&&space;154&space;&&space;212&space;\\&space;89&space;&&space;25&space;&&space;152&space;\end{bmatrix}" />
16+
17+
Then the output from the operations is:
18+
19+
<img src="https://latex.codecogs.com/gif.latex?G_x(A)=1\cdot&space;119+0\cdot&space;80-1\cdot&space;122+2\cdot&space;177+0\cdot&space;154-2\cdot&space;212+1\cdot&space;89+0\cdot&space;25-1\cdot&space;152" />
20+
<img src="https://latex.codecogs.com/gif.latex?\therefore\quad&space;G_x(A)=-136" />
21+
22+
23+
and
24+
25+
<img src="https://latex.codecogs.com/gif.latex?G_x(A)=1\cdot&space;119+2\cdot&space;80+1\cdot&space;122+0\cdot&space;177+0\cdot&space;154+0\cdot&space;212-1\cdot&space;89-2\cdot&space;25-1\cdot&space;152" />
26+
<img src="https://latex.codecogs.com/gif.latex?\therefore\quad&space;G_y(A)=110" />
27+
28+
To apply the Sobel operation to that patch of image we want to calculate:
29+
30+
<img src="https://latex.codecogs.com/gif.latex?\sqrt{\big[G_x(A)\big]^2+\big[G_y(A)\big]^2}\quad\text{which&space;in&space;the&space;case&space;of&space;this&space;example&space;is&space;equal&space;to&space;}\,174.917" />
31+
32+
This number will be the pixel contained in the output image. For now it's ok to have values which are out of the [0,255] range because at the very end we will normalized the image values. Notice that the output will be always greater than or equal to zero.
33+
34+
We do this operation for the entire input image and create a new image based on these outputs just describe above.
35+
36+
This completes the application of the edge detection technique.

display_image.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
# Example of grayscale patch
5+
A = np.array([[119, 80, 122], [177, 154, 212], [89, 25, 152]])
6+
plt.imshow(A.astype('int32'), cmap=plt.get_cmap('gray'))
7+
8+
9+
# This chunk of code defines the kernel operations and outputs image
10+
Gx = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]])
11+
Gy = np.array([[1.0, 2.0, 1.0], [0.0, 0.0, 0.0], [-1.0, -2.0, -1.0]])
12+
13+
# The output of the kernel operation is
14+
15+
GxA = np.sum(np.multiply(Gx, A))
16+
GyA = np.sum(np.multiply(Gy, A))
17+
18+
output_value = np.sqrt(GxA**2 + GyA**2)
19+
print(output_value)

0 commit comments

Comments
 (0)