# Global Contrast Normalization
# 1.作用
全局对比度归一化是为了防止图像的不同区域有不同量级的对比度。
# 2.计算方式
对于每个图像计算其像素均值,并根据其标准差归一化
GCN
的计算方式可表示为:
其中
# 3.示例代码
import numpy
import scipy
import scipy.misc
from PIL import Image
def global_contrast_normalization(filename, s, lmda, epsilon):
X = numpy.array(Image.open(filename))
# replacement for the loop
X_average = numpy.mean(X)
print('Mean: ', X_average)
X = X - X_average
# `su` is here the mean, instead of the sum
contrast = numpy.sqrt(lmda + numpy.mean(X**2))
X = s * X / max(contrast, epsilon)
# scipy can handle it
scipy.misc.imsave('result.jpg', X)
global_contrast_normalization("cat.jpg", 1, 10, 0.000000001)
# 补充
GCN的应用可见Deep SVDD
的代码 (opens new window),其中对数据Normalize
之前,先对数据进行了GCN
,按类别求出了GCN
后每个图像像素数据的最大最小值。
# Deep-SVDD-PyTorch/src/datasets/mnist.py line:20-30
# Pre-computed min and max values (after applying GCN) from train data per class
min_max = [(-0.8826567065619495, 9.001545489292527),
(-0.6661464580883915, 20.108062262467364),
(-0.7820454743183202, 11.665100841080346),
(-0.7645772083211267, 12.895051191467457),
(-0.7253923114302238, 12.683235701611533),
(-0.7698501867861425, 13.103278415430502),
(-0.778418217980696, 10.457837397569108),
(-0.7129780970522351, 12.057777597673047),
(-0.8280402650205075, 10.581538445782988),
(-0.7369959242164307, 10.697039838804978)]