如何使用Python中的scikit-learn找到图像的轮廓?
Scikit-learn通常称为sklearn,它是Python中用于实现机器学习算法的库。它是一个开源库,因此可以免费使用。该库建立在Numpy、SciPy和Matplotlib库上。
使用“marching squares”方法来查找图像中的轮廓。使用“skimage”库中“measure”类中的“find_contours”函数。在其中,数组中的值以线性方式进行插值。
这样,输出图像中的轮廓的精度将大大提高。如果图像中的轮廓相交,则轮廓是开放的,否则是闭合的。
让我们了解如何使用scikit-learn库查找图像中的轮廓-
阅读更多:Python 教程
示例
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”函数用于在控制台上显示原始图像和带有轮廓的图像。
极客教程