-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gaussian_Filter.py
113 lines (92 loc) · 2.76 KB
/
Gaussian_Filter.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# import packages
import numpy as np
from PIL import Image
from scipy.signal import convolve2d as conv2
import matplotlib.pyplot as plt
import gauss_module
def rgb2gray(rgb):
r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
# function gauss (Question 1.a)
sigma = 4.0
[Gx, x] = gauss_module.gauss(sigma)
plt.figure(1)
plt.plot(x, Gx, '.-')
plt.show()
# function gaussianfilter (Question 1.b)
img = rgb2gray(np.array(Image.open('graf.png')))
smooth_img = gauss_module.gaussianfilter(img, sigma)
plt.figure(2)
ax1 = plt.subplot(1, 2, 1)
ax2 = plt.subplot(1, 2, 2)
plt.sca(ax1)
plt.imshow(img, cmap='gray', vmin=0, vmax=255)
plt.sca(ax2)
plt.imshow(smooth_img, cmap='gray', vmin=0, vmax=255)
plt.show()
# function gaussdx (Question 1.c)
sigma = 4.0
[Gx, x] = gauss_module.gauss(sigma)
[Dx, x] = gauss_module.gaussdx(sigma)
plt.figure(5)
plt.plot(x, Gx, 'b.-')
plt.plot(x, Dx, 'r-')
plt.legend(('gauss', 'gaussdx'))
plt.show()
# function gaussdx (Question 1.d)
img_imp = np.zeros([27, 27])
img_imp[13, 13] = 1.0
plt.figure(6), plt.imshow(img_imp, cmap='gray')
sigma = 7.0
[Gx, x] = gauss_module.gauss(sigma)
[Dx, x] = gauss_module.gaussdx(sigma)
Gx = Gx.reshape(1, Gx.size)
Dx = Dx.reshape(1, Dx.size)
plt.figure(7)
plt.subplot(2, 3, 1)
plt.imshow(conv2(conv2(img_imp, Gx, 'same'), Gx.T, 'same'), cmap='gray')
plt.subplot(2, 3, 2)
plt.imshow(conv2(conv2(img_imp, Gx, 'same'), Dx.T, 'same'), cmap='gray')
plt.subplot(2, 3, 3)
plt.imshow(conv2(conv2(img_imp, Dx.T, 'same'), Gx, 'same'), cmap='gray')
plt.subplot(2, 3, 4)
plt.imshow(conv2(conv2(img_imp, Dx, 'same'), Dx.T, 'same'), cmap='gray')
plt.subplot(2, 3, 5)
plt.imshow(conv2(conv2(img_imp, Dx, 'same'), Gx.T, 'same'), cmap='gray')
plt.subplot(2, 3, 6)
plt.imshow(conv2(conv2(img_imp, Gx.T, 'same'), Dx, 'same'), cmap='gray')
plt.show()
# function gaussderiv (Question 1.e)
img_c = np.array(Image.open('graf.png')).astype('double')
img = rgb2gray(img_c)
[imgDx, imgDy] = gauss_module.gaussderiv(img, 7.0)
plt.figure(8)
ax1 = plt.subplot(1, 3, 1)
ax2 = plt.subplot(1, 3, 2)
ax3 = plt.subplot(1, 3, 3)
plt.sca(ax1)
plt.imshow(imgDx, cmap='gray')
plt.sca(ax2)
plt.imshow(imgDy, cmap='gray')
plt.sca(ax3)
imgmag = np.sqrt(imgDx**2 + imgDy**2)
plt.imshow(imgmag, cmap='gray')
plt.show()
sigma = 7
img_1 = rgb2gray(np.array(Image.open('graf.png')))
img_2 = rgb2gray(np.array(Image.open('gantrycrane.png')))
plt.figure(9)
img1_dx, img1_dy = gauss_module.gaussderiv(img_1, sigma)
plt.subplot(1, 2, 1)
plt.imshow(img1_dx, cmap='gray')
plt.subplot(1, 2, 2)
plt.imshow(img1_dy, cmap='gray')
plt.show()
plt.figure(10)
img2_dx, img2_dy = gauss_module.gaussderiv(img_2, sigma)
plt.subplot(1, 2, 1)
plt.imshow(img2_dx, cmap='gray')
plt.subplot(1, 2, 2)
plt.imshow(img2_dy, cmap='gray')
plt.show()