Which resize filter keeps label IDs intact?

Nearest-neighbor resizing picks one source pixel per output position and preserves discrete label values.

Pixel experiment and interpretation

Double a strip labeled 1 and 9 and inspect its distinct labels. Downsampling can still drop a tiny object even with valid IDs.

from PIL import Image

def row(values):
    return Image.frombytes('L', (len(values), 1), bytes(values))

list(row([1,9]).resize((4,1),Image.Resampling.NEAREST).tobytes())

Result [1, 1, 9, 9]

中文

缩放类别掩膜怎样避免产生新类别?

最近邻缩放为每个目标位置选择一个源像素,适合保存离散编号。

像素实验与适用边界

把 1 号与 9 号类别各放大两倍,检查结果只含 1 和 9。很小的目标在缩小时即使编号正确也可能消失。

from PIL import Image

def row(values):
    return Image.frombytes('L', (len(values), 1), bytes(values))

list(row([1,9]).resize((4,1),Image.Resampling.NEAREST).tobytes())

结果 [1, 1, 9, 9]

References / 参考来源

Keep reading / 继续阅读