使用形态学操作对二值图像进行后处理#

形态学操作基于形状来转换图像;在这种情况下,我们通常指的是二值图像。

import numpy as np
from skimage.io import imread
import matplotlib.pyplot as plt
from skimage import morphology
from skimage import filters

核、足迹和结构元素#

如果我们使用scikit-image,许多形态学滤波器都有一个footprint参数。这个足迹就是滤波器核,在文献中你也会找到结构元素这个术语。

# creates a disk of 1 with radius = 3
disk = morphology.disk(3) 
disk
array([[0, 0, 0, 1, 0, 0, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 0, 0, 1, 0, 0, 0]], dtype=uint8)
plt.imshow(disk, cmap='gray')
<matplotlib.image.AxesImage at 0x225b0c88340>
../_images/2ce56700144d6f8989abd8050950954b7bd4789e6cd2a1099608849e30beabe7.png
# create a square with width and height = 3
square = morphology.square(3) 
square
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]], dtype=uint8)

二值形态学#

为了演示二值图像的形态学滤波,我们再次使用小的细胞核图像。

image_nuclei = imread('../../data/mitosis_mod.tif').astype(float)
image_binary = image_nuclei > filters.threshold_otsu(image_nuclei)

plt.imshow(image_binary, cmap='gray')
<matplotlib.image.AxesImage at 0x225b0d05490>
../_images/56ab869e45d7333845132a88738966b807dd35f12510c333bbe203c03660b693.png

腐蚀和膨胀#

要使黑色海洋中的白色岛屿变小,我们需要_腐蚀_它们的海岸线。

eroded = morphology.binary_erosion(image_binary, disk)

plt.imshow(eroded, cmap='gray')
<matplotlib.image.AxesImage at 0x225b0d80dc0>
../_images/d838c1304d54d3eaa1ff7f21cf123079d931e4f7dccf866c0345ea5f62330954.png

如果我们之后对图像进行膨胀,我们会得到看起来比原始二值图像更平滑的白色岛屿。

eroded_dilated = morphology.binary_dilation(eroded, disk)

plt.imshow(eroded_dilated, cmap='gray')
<matplotlib.image.AxesImage at 0x225b107f730>
../_images/7c55ef4ae2d61ff4f6963cd2545398174849e8f30a917c3925d3720cdfa4693b.png

连续调用腐蚀和膨胀是如此常见,以至于有一个专门的函数来执行这个操作。由于岛屿之间的间隙_打开_,这个操作被称为_开运算_。

opened = morphology.binary_opening(image_binary, disk)

plt.imshow(opened, cmap='gray')
<matplotlib.image.AxesImage at 0x225b10f9b20>
../_images/7c55ef4ae2d61ff4f6963cd2545398174849e8f30a917c3925d3720cdfa4693b.png

练习1#

还有一个_闭运算_操作。将其应用于二值图像。

练习2#

scikit-image文档中搜索最小和最大滤波器。将最小滤波器应用于二值图像,然后将最大滤波器应用于结果。将其与上面显示的图像进行比较。