Skip to content

Instantly share code, notes, and snippets.

@andrewgiessel
Created January 21, 2013 20:52
Show Gist options
  • Save andrewgiessel/4589258 to your computer and use it in GitHub Desktop.
Save andrewgiessel/4589258 to your computer and use it in GitHub Desktop.
histogram normalization with numpy
import numpy as np
def histeq(im,nbr_bins=256):
#get image histogram
imhist,bins = np.histogram(im.flatten(),nbr_bins,normed=True)
cdf = imhist.cumsum() #cumulative distribution function
cdf = 255 * cdf / cdf[-1] #normalize
#use linear interpolation of cdf to find new pixel values
im2 = np.interp(im.flatten(),bins[:-1],cdf)
return im2.reshape(im.shape), cdf
@Bhisham-Sharma
Copy link

how to interpolate a CDF at a value say 0.5 to get a scalar value as a result?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment