How can a foreground mean exclude a black background?
ImageStat.Stat accepts a mask that restricts per-band statistics to selected positions.
Pixel experiment and interpretation
Exclude the background in a strip containing 0, 20 and 80. The midpoint of the two selected readings is the mean. Keep genuinely dark subjects selected.
from PIL import Image, ImageStat
def row(values):
return Image.frombytes('L', (len(values), 1), bytes(values))
ImageStat.Stat(row([0,20,80]),row([0,255,255])).meanResult [50.0]
中文
统计前景均值时怎样排除黑色背景?
ImageStat.Stat 接收掩膜,仅把选中的位置计入各通道统计。
像素实验与适用边界
把 0、20、80 三个读数中的背景排除,均值是两个目标读数的中点。掩膜中也被覆盖的暗目标仍应计入。
from PIL import Image, ImageStat
def row(values):
return Image.frombytes('L', (len(values), 1), bytes(values))
ImageStat.Stat(row([0,20,80]),row([0,255,255])).mean结果 [50.0]