-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathintroduction.py
More file actions
20 lines (16 loc) · 851 Bytes
/
introduction.py
File metadata and controls
20 lines (16 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""
Small script describing how to display an image based on some input array as well as explaining how to apply the Sobel
operator/filter to a small patch of image.
"""
import numpy as np
import matplotlib.pyplot as plt
# Example of a grayscale image patch
A = np.array([[119, 80, 122], [177, 154, 212], [89, 25, 152]])
plt.imshow(A, cmap=plt.get_cmap('gray'))
# Next we define the matrices associated with the Sobel filter
Gx = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]])
Gy = np.array([[1.0, 2.0, 1.0], [0.0, 0.0, 0.0], [-1.0, -2.0, -1.0]])
# The output of the kernel operation is given by the following
GxA, GyA = np.sum(np.multiply(Gx, A)), np.sum(np.multiply(Gy, A))
output_value = np.sqrt(GxA**2 + GyA**2) # equal to the "hypotenuse" of the values in the x and y directions
print("output pixel value =", output_value)