卷积
卷积是一种对图像执行的操作,用于从图像中提取特征,在图像上应用称为内核的较小张量,如滑动窗口。根据卷积内核中的值,我们可以从图像中选取特定的模式。在下面的示例中,我们将演示使用适当的内核检测图像中的水平和垂直边缘。卷积是卷积神经网络背后的关键特征之一。
import numpy as np
import matplotlib.pyplot as plt
# let img1 be an image with no features
img1 = np.array([np.array([200, 200]), np.array([200, 200])])
img2 = np.array([np.array([200, 200]), np.array([0, 0])])
img3 = np.array([np.array([200, 0]), np.array([200, 0])])
kernel_horizontal = np.array([np.array([2, 2]), np.array([-2, -2])])
print(kernel_horizontal, 'is a kernel for detecting horizontal edges')
kernel_vertical = np.array([np.array([2, -2]), np.array([2, -2])])
print(kernel_vertical, 'is a kernel for detecting vertical edges')
# We will apply the kernels on the images by
# elementwise multiplication followed by summation
def apply_kernel(img, kernel):
return np.sum(np.multiply(img, kernel))
# Visualizing img1
plt.imshow(img1)
plt.axis('off')
plt.title('img1')
plt.show()
# Checking for horizontal and vertical features in image1
print('Horizontal edge confidence score:', apply_kernel(img1,
kernel_horizontal))
print('Vertical edge confidence score:', apply_kernel(img1,
kernel_vertical))
# Visualizing img2
plt.imshow(img2)
plt.axis('off')
plt.title('img2')
plt.show()
# Checking for horizontal and vertical features in image2
print('Horizontal edge confidence score:', apply_kernel(img2,
kernel_horizontal))
print('Vertical edge confidence score:', apply_kernel(img2,
kernel_vertical))
# Visualizing img3
plt.imshow(img3)
plt.axis('off')
plt.title('img3')
plt.show()
# Checking for horizontal and vertical features in image3
print('Horizontal edge confidence score:', apply_kernel(img3,
kernel_horizontal))
print('Vertical edge confidence score:', apply_kernel(img3,
kernel_vertical))








暂无数据