How can a histogram count exactly white pixels?

An L-mode histogram has 256 bins indexed by intensity, each holding a pixel count.

Pixel experiment and interpretation

Use the last bin as a saturation counter. It counts exactly 255; almost-white 254 goes in a separate bin.

from PIL import Image

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

h=row([255,254,0,255]).histogram()
(h[254],h[255],sum(h))

Result (1, 2, 4)

中文

怎样从灰度直方图直接统计纯白像素?

L 模式直方图有 256 个桶,桶下标是灰度值,桶内是像素数。

像素实验与适用边界

把最右边的桶当作饱和计数器。它只统计恰好 255 的像素,接近白色的 254 会进另一个桶。

from PIL import Image

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

h=row([255,254,0,255]).histogram()
(h[254],h[255],sum(h))

结果 (1, 2, 4)

References / 参考来源

Keep reading / 继续阅读