用于修正语义分割结果的众数滤波器#

在描述性统计中存在多种汇总指标。例如,均值滤波器和中值滤波器允许以不同方式对图像进行局部平均。众数滤波器不太常见,但在某些情况下仍然有用。像素在其邻域中的众数对应于现有强度中最常见的强度。因此,它可以用于消除语义分割结果中被错误分类的个别像素。

import numpy as np
import pyclesperanto_prototype as cle
import stackview

为了演示这个滤波器,我们创建了一个斑点的语义分割。

blobs = cle.imread("../../data/blobs.tif")
semantic_segmentation = (blobs > 70) + \
                        (blobs > 200) + 1

semantic_segmentation.astype(np.uint32)
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min1.0
max3.0

使用mode_spheremode_box函数,我们可以使结果更少噪声。

cle.mode_sphere(semantic_segmentation, radius_x=2, radius_y=2).astype(np.uint32)
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min1.0
max3.0
cle.mode_sphere(semantic_segmentation, radius_x=4, radius_y=4).astype(np.uint32)
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min1.0
max3.0

当半径变得越来越宽时,结果包含的局部信息就越来越少。

cle.mode_sphere(semantic_segmentation, radius_x=10, radius_y=10).astype(np.uint32)
cle._ image
shape(254, 256)
dtypeuint32
size254.0 kB
min1.0
max3.0

手动调整半径可能有助于找到一个好的配置。

def mode_sphere(image, radius:float = 1):
    return cle.mode_sphere(image, radius_x=radius, radius_y=radius).astype(np.uint32)

stackview.interact(mode_sphere, semantic_segmentation, zoom_factor=1.5)