如何使用 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' 函数在控制台上显示原始图像和带有轮廓的图像。

更新于: 2020-12-10

989 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.