Image Equalization


First of all, take the maximum intensity value in the image:

max_int_val = np.max(image)

Calculate the number of bits:

bits = int(math.log2(max_int_val)) + 1

Calculate the number of bins:

bins = 2**bits

Calculate Histogram:

histogram = np.zeros((bins,1))

for i in range(bins):

histogram[i] = len(image[image == i])

Calculate the sum of histogram:

sum_hist = np.sum(histogram)

Calculate probability density function:

  pdf = np.zeros((bins,1))

for i in range(bins):

pdf[i] = histogram[i]/sum_hist

Calculate cumulative distribution function:

 cdf = np.zeros((bins,1))

for i in range(bins):

if i == 0:

cdf[i] = pdf[i]


cdf[i] = cdf[i-1] + pdf[i]

Calculate equalized histogram:

equalized_hist = cdf*(bins-1)

Apply equalized histogram to the image:

 for i in range(bins):

image[image == i] = equalized_hist[i]

That’s it for now. See you later.