Skip to content

Instantly share code, notes, and snippets.

@TimSC
Last active April 10, 2020 21:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimSC/6f429dfacf523f5c9a58c3b629f0540e to your computer and use it in GitHub Desktop.
Save TimSC/6f429dfacf523f5c9a58c3b629f0540e to your computer and use it in GitHub Desktop.
Equalize histogram using numpy/python
import numpy as np
def EqualizeHistogram(a, bins):
a = np.array(a)
hist, bins2 = np.histogram(a, bins=bins)
#Compute CDF from histogram
cdf = np.cumsum(hist, dtype=np.float64)
cdf = np.hstack(([0], cdf))
cdf = cdf / cdf[-1]
#Do equalization
binnum = np.digitize(a, bins, True)-1
neg = np.where(binnum < 0)
binnum[neg] = 0
aeq = cdf[binnum] * bins[-1]
return aeq
if __name__ == "__main__":
maxval = 255.0
img = np.array([[52, 55, 61, 59, 70, 61, 76, 61],
[62, 59, 55, 104, 94, 85, 59, 71],
[63, 65, 66, 113, 144, 104, 63, 72],
[64, 70, 70, 126, 154, 109, 71, 69],
[67, 73, 68, 106, 122, 88, 68, 68],
[68, 79, 60, 79, 77, 66, 58, 75],
[69, 85, 64, 58, 55, 61, 65, 83],
[70, 87, 69, 68, 65, 73, 78, 90]])
#Define 256 bins, therefore we need 257 bin boundaries
bins = np.linspace(0.0, maxval, 257)
imgeq = EqualizeHistogram(img, bins)
print (imgeq)
values = [0.5514496764156089, 0.6611494976657062, 0.7406485845799674, 0.7811561047780778, 0.78259113907824, 0.750813173170488,
0.6977147420450983, 0.6342792520243044, 0.5679639053696388, 0.5, 0.4320360946303611, 0.3619052191760549, 0.28969028937628044,
0.21851495931285048, 0.15343071458227583, 0.09995297853938098, 0.059445458341270546, 0.03198907316974207, 0.015335933758330735,
0.006297484289310597, 0.0019077643998203508, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0019077643998203508, 0.006297484289310597, 0.015335933758330733, 0.03198907316974207,
0.059445458341270546, 0.09995297853938098, 0.15343071458227583, 0.21660719491303013, 0.2833928050869699, 0.3446615210179038,
0.3937495371713084, 0.42521860790039867, 0.4360218536605158, 0.4252186079003987, 0.3937495371713084, 0.3446615210179038,
0.28339280508696985, 0.21660719491303013, 0.15343071458227583, 0.09995297853938098, 0.059445458341270546, 0.03198907316974207,
0.015335933758330735, 0.006297484289310597, 0.0019077643998203508, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0019077643998203508, 0.006297484289310597, 0.015335933758330733, 0.03198907316974207, 0.059445458341270546, 0.09995297853938098,
0.15343071458227583, 0.21660719491303013, 0.2853005694867902, 0.35286676970703473, 0.41538295521894975, 0.4725436148284715,
0.5274563851715285, 0.5846170447810503, 0.6471332302929652, 0.7146994305132098, 0.78339280508697, 0.8465692854177241, 0.900047021460619,
0.9405545416587294, 0.968010926830258, 0.9846640662416694, 0.9937025157106895, 0.9980922356001797, 1.0, 0.9961844712003592,
0.9874050314213787, 0.9693281324833385, 0.9360218536605158, 0.8811090833174589, 0.800094042921238, 0.6931385708354483,
0.5667856101739397, 0.43321438982606025, 0.30686142916455167, 0.19990595707876196, 0.11889091668254109, 0.06397814633948413,
0.03067186751666147, 0.012594968578621194, 0.0038155287996407016, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0019077643998203508, 0.006297484289310597,
0.015335933758330733, 0.03198907316974207, 0.059445458341270546, 0.09995297853938098, 0.15343071458227583, 0.21660719491303013,
0.2833928050869699, 0.3465692854177242, 0.40004702146061905, 0.4405545416587295, 0.46801092683025797, 0.4846640662416693,
0.4937025157106894, 0.4980922356001796, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.4980922356001796, 0.49370251571068935,
0.48466406624166924, 0.4680109268302579, 0.4405545416587294, 0.400047021460619, 0.34656928541772414, 0.28339280508696985,
0.21660719491303013, 0.15343071458227583, 0.09995297853938098, 0.059445458341270546, 0.03198907316974207, 0.015335933758330735,
0.006297484289310597, 0.0019077643998203508, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
#Define 100 bins, therefore we need 101 bin boundaries
concensus = EqualizeHistogram(values, np.linspace(0.0, 1.0, 101))
print(concensus)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment