TensorFlow课程04 卷积的效果

https://lodev.org/cgtutor/filtering.html

首先我们通过加载scipy中的ascent图像(这是一张scipy内置的有很多角和线的图像)来介绍卷积是如何工作的。

1
2
3
4
import cv2
import numpy as np
from scipy import misc
i = misc.ascent()

接下来,我们可以使用pyplot库来绘制图像,这样我们就知道它是什么样子了。

1
2
3
4
5
6
import matplotlib.pyplot as plt
plt.grid(False)
plt.gray()
plt.axis('off')
plt.imshow(i)
plt.show()

接下来我们可以将图像存储为一个numpy数组,我们可以通过复制该数组来创建转换后的图像。

1
2
3
i_transformed = np.copy(i)
size_x = i_transformed.shape[0]
size_y = i_transformed.shape[1]

接下来我们创建一个3x3的数组:

1
2
filter = [ [-1, -2, -1], [0, 0, 0], [1, 2, 1]]
weight = 1

执行卷积运算:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for x in range(1,size_x-1):
for y in range(1,size_y-1):
convolution = 0.0
convolution = convolution + (i[x - 1, y-1] * filter[0][0])
convolution = convolution + (i[x, y-1] * filter[0][1])
convolution = convolution + (i[x + 1, y-1] * filter[0][2])
convolution = convolution + (i[x-1, y] * filter[1][0])
convolution = convolution + (i[x, y] * filter[1][1])
convolution = convolution + (i[x+1, y] * filter[1][2])
convolution = convolution + (i[x-1, y+1] * filter[2][0])
convolution = convolution + (i[x, y+1] * filter[2][1])
convolution = convolution + (i[x+1, y+1] * filter[2][2])
convolution = convolution * weight
if(convolution<0):
convolution=0
if(convolution>255):
convolution=255
i_transformed[x, y] = convolution

将结果可视化:

1
2
3
4
5
6
# Plot the image. Note the size of the axes -- they are 512 by 512
plt.gray()
plt.grid(False)
plt.imshow(i_transformed)
#plt.axis('off')
plt.show()

执行max pooling并将结果可视化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
new_x = int(size_x/2)
new_y = int(size_y/2)
newImage = np.zeros((new_x, new_y))
for x in range(0, size_x, 2):
for y in range(0, size_y, 2):
pixels = []
pixels.append(i_transformed[x, y])
pixels.append(i_transformed[x+1, y])
pixels.append(i_transformed[x, y+1])
pixels.append(i_transformed[x+1, y+1])
newImage[int(x/2),int(y/2)] = max(pixels)

# Plot the image. Note the size of the axes -- now 256 pixels instead of 512
plt.gray()
plt.grid(False)
plt.imshow(newImage)
#plt.axis('off')
plt.show()