调整掩码
通过增加或减少像素,可以对不完美的掩码进行调整。SciPy 提供了多种有用的方法来实现这些目的,包括:
binary_dilation:沿边缘增加像素binary_erosion:沿边缘去除像素binary_opening:先腐蚀再膨胀,用于"打开"边缘附近的区域binary_closing:先膨胀再腐蚀,用于"填充"孔洞
在本练习中,请先创建一个骨骼掩码,然后对其进行调整以包含更多像素。
在接下来的练习中,我们已为您运行如下导入:
import scipy.ndimage as ndi
本练习是课程的一部分
Python 中的生物医学图像分析
练习说明
- 通过从
im中选择大于或等于145的像素来创建骨骼掩码。 - 使用
ndi.binary_dilation()来增大mask_bone的尺寸。将iterations设置为5,以重复执行多次膨胀。 - 使用
ndi.binary_closing()来填充mask_bone中的孔洞。将iterations设置为5,可填充宽度最多约 10 个像素的孔洞。 - 绘制原始掩码和调整后的掩码。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create and tune bone mask
mask_bone = ____
mask_dilate = ndi.binary_dilation(____, ____)
mask_closed = ____
# Plot masked images
fig, axes = plt.subplots(1,3)
axes[0].imshow(mask_bone)
axes[1].imshow(mask_dilate)
____
format_and_render_plot()