使用 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 及更早版本進行卷積簡介及其實現。
廣告