-
Notifications
You must be signed in to change notification settings - Fork 5
/
edge-udemy-git-sa.py
57 lines (46 loc) · 1.27 KB
/
edge-udemy-git-sa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# https://deeplearningcourses.com/c/deep-learning-convolutional-neural-networks-theano-tensorflow
# https://udemy.com/deep-learning-convolutional-neural-networks-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
from scipy.signal import convolve2d
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# load the famous Lena image
img = mpimg.imread('lena.png')
print("original image", img.shape)
# make it B&W
bw = img.mean(axis=2)
print("black n white", bw.shape)
# Sobel operator - approximate gradient in X dir
Hx = np.array([
[-1, 0, 1],
[-2, 7, 2],
[-1, 0, 1],
], dtype=np.float32)
# Sobel operator - approximate gradient in Y dir
Hy = np.array([
[-1, -5, -1],
[3, 0, 0],
[1, 4, 1],
], dtype=np.float32)
Gx = convolve2d(bw, Hx)
print("Gx", Gx.shape)
plt.imshow(Gx, cmap='gray')
plt.show()
Gy = convolve2d(bw, Hy)
plt.imshow(Gy, cmap='gray')
print("Gy", Gy.shape)
plt.show()
# Gradient magnitude
G = np.sqrt(Gx*Gx + Gy*Gy)
print("G", G.shape)
plt.imshow(G, cmap='gray')
plt.show()
# The gradient's direction
theta = np.arctan2(Gy, Gx)
print("theta", theta.shape)
plt.imshow(theta, cmap='gray')
plt.show()