理解卷积神经网络(CNN)四个基础概念一:卷积

卷积是为了提取特征

import numpy as np

image = np.array([
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
])

filter_vertical = np.array([
    [-1, 0, 1],
    [-1, 0, 1],
    [-1, 0, 1],
])

filter_horizontal = np.array([
    [0, 1, 0],
    [0, 1, 0],
    [0, 1, 0],
])

region = image[1:4, 1:4]
vertical_result = np.sum(region * filter_vertical)
horizontal_result = np.sum(region * filter_horizontal)

print('中间区域:\n', region)
print('垂直滤波结果:', vertical_result)
print('水平滤波结果:', horizontal_result)

执行结果:

中间区域:
 [[0 0 0]
 [1 1 1]
 [0 0 0]]
垂直滤波结果: 0
水平滤波结果: 1

Leave a Reply