使用 Python 的卷积简介
在本文中,我们将了解如何在 Python 3.x 及更早版本中进行卷积。本文属于神经网络与特征提取类别。
推荐的 IDE − Jupyter notebook
先决条件− 已安装 Numpy,已安装 Matplotlib
安装
>>> pip install numpy >>>pip install matplotlib
卷积
卷积是一种操作,可对图像执行此操作,方法是通过将一个称为内核/坐标容器的较小容器(如滑动窗口)应用于图像,以从中提取特征。根据卷积坐标容器中的值,我们可以从图像中汲取特定的模式/特征。在此处,我们将学习如何使用适当的坐标容器检测图像中的水平和垂直端点。
现在,我们来看看实际实现。
示例
import numpy as np
from matplotlib import pyplot
# initializing the images
img1 = np.array([np.array([100, 100]), np.array([80, 80])])
img2 = np.array([np.array([100, 100]), np.array([50, 0])])
img3 = np.array([np.array([100, 50]), np.array([100, 0])])
coordinates_horizontal = np.array([np.array([3, 3]), np.array([-3, -3])])
print(coordinates_horizontal, 'is a coordinates for detecting horizontal end points')
coordinates_vertical = np.array([np.array([3, -3]), np.array([3, - 3])])
print(coordinates_vertical, 'is a coordinates for detecting vertical end points')
#his will be an elemental multiplication followed by addition
def apply_coordinates(img, coordinates):
return np.sum(np.multiply(img, coordinates))
# Visualizing img1
pyplot.imshow(img1)
pyplot.axis('off')
pyplot.title('sample 1')
pyplot.show()
# Checking for horizontal and vertical features in image1
print('Horizontal end points features score:',
apply_coordinates(img1, coordinates_horizontal))
print('Vertical end points features score:',
apply_coordinates(img1,coordinates_vertical))
# Visualizing img2
pyplot.imshow(img2)
pyplot.axis('off')
pyplot.title('sample 2')
pyplot.show()
# Checking for horizontal and vertical features in image2
print('Horizontal end points features score:',
apply_coordinates(img2, coordinates_horizontal))
print('Vertical end points features score:',
apply_coordinates(img2, coordinates_vertical))
# Visualizing img3
pyplot.imshow(img3)
pyplot.axis('off')
pyplot.title('sample 3')
pyplot.show()
# Checking for horizontal and vertical features in image1
print('Horizontal end points features score:',
apply_coordinates(img3,coordinates_horizontal))
print('Vertical end points features score:',
apply_coordinates(img3,coordinates_vertical))输出

结论
在本文中,我们了解了使用 Python 3.x 及更早版本进行卷积简介及其实现。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP