如何使用 Python 中的 scikit-learn 查詢影像的輪廓?
Scikit-learn,通常稱為 sklearn,是 Python 中用於實現機器學習演算法的庫。它是一個開源庫,因此可以免費使用。該庫構建在 Numpy、SciPy 和 Matplotlib 庫之上。
“行進方塊”方法用於查詢影像中的輪廓。使用 'skimage' 庫的 'measure' 類中的 'find_contours' 函式。在此,陣列中的值以線性方式進行插值。
這樣,輸出影像中輪廓的精度會更好。如果影像中的輪廓相交,則輪廓是開放的,否則它們是閉合的。
讓我們瞭解如何使用 scikit-learn 庫查詢影像中的輪廓 -
示例
import numpy as np import matplotlib.pyplot as plt from skimage import measure x, y = np.ogrid[-6.7:np.pi:215j, -1.2:np.pi:215j] r = np.sin(np.exp((np.sin(x)**3 + np.cos(y)**2))) contours = measure.find_contours(r, 0.8) fig, ax = plt.subplots() ax.imshow(r, cmap=plt.cm.gray) for contour in contours: ax.plot(contour[:, 1], contour[:, 0], linewidth=2) ax.axis('Image') ax.set_xticks([]) ax.set_yticks([]) plt.show()
輸出
解釋
將所需的包匯入到環境中。
藉助 NumPy 包生成資料。
使用 'find_contours' 函式確定影像的輪廓。
使用 'subplot' 函式在控制檯上顯示原始影像和帶有輪廓的影像。
廣告