詹惠儿

2018-12-16   阅读量: 576

数据分析师 Python编程

Python的卷积是什么?

扫码加入数据分析学习群

卷积

卷积是一种对图像执行的操作,用于从图像中提取特征,在图像上应用称为内核的较小张量,如滑动窗口。根据卷积内核中的值,我们可以从图像中选取特定的模式。在下面的示例中,我们将演示使用适当的内核检测图像中的水平和垂直边缘。卷积卷积神经网络背后的关键特征之一。

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))

添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
0.0000 0 1 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子