How can you spot unexpected IDs in a label mask?
getcolors lists counts with their colors and returns None if the distinct-color limit is exceeded.
Pixel experiment and interpretation
Budget for two labels. A third distinct value triggers the overflow signal; raising the cap identifies the intruder.
from PIL import Image
def row(values):
return Image.frombytes('L', (len(values), 1), bytes(values))
im=row([0,0,1,9])
(im.getcolors(2),sorted(im.getcolors(3)))Result (None, [(1, 1), (1, 9), (2, 0)])
中文
怎样识别类别掩膜中的意外取值?
getcolors 枚举颜色及出现次数,超过 maxcolors 时返回 None。
像素实验与适用边界
预期两类标签时给两格计数预算,第三种取值使检查失败。提高上限后可定位是哪一类混入。
from PIL import Image
def row(values):
return Image.frombytes('L', (len(values), 1), bytes(values))
im=row([0,0,1,9])
(im.getcolors(2),sorted(im.getcolors(3)))结果 (None, [(1, 1), (1, 9), (2, 0)])