Skip to content

Instantly share code, notes, and snippets.

@louismullie
Created April 13, 2020 17:32
Show Gist options
  • Save louismullie/127f96261d1d3bc57c2ef941f76bbccb to your computer and use it in GitHub Desktop.
Save louismullie/127f96261d1d3bc57c2ef941f76bbccb to your computer and use it in GitHub Desktop.
import numpy as np
def image_histogram_equalization(image, number_bins=256):
# from http://www.janeriksolem.net/2009/06/histogram-equalization-with-python-and.html
# get image histogram
image_histogram, bins = np.histogram(image.flatten(), number_bins, density=True)
cdf = image_histogram.cumsum() # cumulative distribution function
cdf = 255 * cdf / cdf[-1] # normalize
# use linear interpolation of cdf to find new pixel values
image_equalized = np.interp(image.flatten(), bins[:-1], cdf)
return image_equalized.reshape(image.shape), cdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment