The Moose comment that points to this blog post does the job quite nicely.
For completeness, I give an axample here, using more pleasant variable names and looped execution on 1000 96x96 images that are in a 4D array, as in the question. It is fast (1-2 seconds on my computer) and only NumPy is required.
import numpy as np
def image_histogram_equalization(image, number_bins=256):
image_histogram, bins = np.histogram(image.flatten(), number_bins, normed=True)
cdf = image_histogram.cumsum()
cdf = 255 * cdf / cdf[-1]
image_equalized = np.interp(image.flatten(), bins[:-1], cdf)
return image_equalized.reshape(image.shape), cdf
if __name__ == '__main__':
data = np.random.rand(1000, 1, 96, 96)
data_equalized = np.zeros(data.shape)
for i in range(data.shape[0]):
image = data[i, 0, :, :]
data_equalized[i, 0, :, :] = image_histogram_equalization(image)[0]