凸包#

二元影像的凸包是包含在最小凸多邊形中的像素集合,該多邊形圍繞輸入中的所有白色像素。

Steve Eddin 的部落格中提供了該演算法的良好概述。

import matplotlib.pyplot as plt

from skimage.morphology import convex_hull_image
from skimage import data, img_as_float
from skimage.util import invert

# The original image is inverted as the object must be white.
image = invert(data.horse())

chull = convex_hull_image(image)

fig, axes = plt.subplots(1, 2, figsize=(8, 4))
ax = axes.ravel()

ax[0].set_title('Original picture')
ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].set_axis_off()

ax[1].set_title('Transformed picture')
ax[1].imshow(chull, cmap=plt.cm.gray)
ax[1].set_axis_off()

plt.tight_layout()
plt.show()
Original picture, Transformed picture

我們準備第二個圖表來顯示差異。

chull_diff = img_as_float(chull.copy())
chull_diff[image] = 2

fig, ax = plt.subplots()
ax.imshow(chull_diff, cmap=plt.cm.gray)
ax.set_title('Difference')
plt.show()
Difference

腳本的總執行時間: (0 分鐘 0.429 秒)

由 Sphinx-Gallery 產生的圖庫