移除对象#

scikit-image 提供了几种从 N 维图像中移除对象的方法。在这里,“对象”(和“孔洞”)被定义为具有相同标签值的样本组,这些样本与背景和其他对象不同。

此示例展示了如何根据对象的大小或它们与其他对象的距离来移除对象。

labeled objects, large objects, spaced objects (nearest removed), small objects
import matplotlib.pyplot as plt
import skimage as ski

# Extract foreground by thresholding an image taken by the Hubble Telescope
image = ski.color.rgb2gray(ski.data.hubble_deep_field())
foreground = image > ski.filters.threshold_li(image)
objects = ski.measure.label(foreground)

# Separate objects into regions larger and smaller than 100 pixels
large_objects = ski.morphology.remove_small_objects(objects, min_size=100)
small_objects = objects ^ large_objects

# Remove objects until remaining ones are at least 100 pixels apart.
# By default, larger ones take precedence.
spaced_objects = ski.morphology.remove_objects_by_distance(objects, min_distance=100)

# Plot the results
fig, ax = plt.subplots(2, 2, figsize=(10, 10), sharex=True, sharey=True)
ax[0, 0].set_title("labeled objects")
ax[0, 0].imshow(ski.color.label2rgb(objects, image=image, bg_label=0))
ax[0, 1].set_title("large objects")
ax[0, 1].imshow(ski.color.label2rgb(large_objects, bg_label=0))
ax[1, 1].set_title("small objects")
ax[1, 1].imshow(ski.color.label2rgb(small_objects, bg_label=0))
ax[1, 0].set_title("spaced objects (nearest removed)")
ax[1, 0].imshow(ski.color.label2rgb(spaced_objects, bg_label=0))
plt.show()

脚本总运行时间: (0 分钟 0.586 秒)

由 Sphinx-Gallery 生成的图库