Histogram Equalization
Fredrik Lundh | Updated May 21, 1997
Histogram equalization can be used to improve the visual appearance of an image. Peaks in the image histogram (indicating commonly used grey levels) are widened, while the valleys are compressed.
Figure: original image, with corresponding histogram
Figure: same image, after equalization
Source
# histogram equalization
import operator
def equalize(h):
lut = []
for b in range(0, len(h), 256):
# step size
step = reduce(operator.add, h[b:b+256]) / 255
# create equalization lookup table
n = 0
for i in range(256):
lut.append(n / step)
n = n + h[i+b]
return lut
#
# test stuff
if __name__ == "__main__":
import Image
im = Image.open("/usr/iv/tip/images/clenna.im")
# calculate lookup table
lut = equalize(im.histogram())
# map image through lookup table
im = im.point(lut)
im.save("out.ppm")
