Skip to content

Instantly share code, notes, and snippets.

@lizecillie
Last active December 21, 2015 10:59
Show Gist options
  • Save lizecillie/6295604 to your computer and use it in GitHub Desktop.
Save lizecillie/6295604 to your computer and use it in GitHub Desktop.
Histogram equalization
from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
from skimage import io,color, img_as_float
def brighten(A):
gamma = 0.4
c = 255
A = c*(A**gamma)
return A
def histogram_equalization(A):
rows = A.shape[0]
columns = A.shape[1]
histogram, bin_edges = np.histogram(A, range=(0,255), bins=255)
c = (np.cumsum(histogram/np.max(histogram)))*255
c = (c*255)/c[-1]
for i in xrange(0, rows):
for j in xrange(0, columns):
A[i,j] = c[int(A[i, j])]
return A
#def contrast_stretching(A, c1, c2, c3, c4):
# A = c1*np.tanh(c2*A - c3) + c4
# return A
#def point_transformation(A):
# A = contrast_stretching(brighten(A))
# return A
A = (img_as_float(io.imread('thecar.jpg', plugin='pil')))*255
B = (img_as_float(io.imread('thecar.jpg', plugin='pil')))*255
C = np.empty_like(A)
C[:] = A
bright_A = brighten(A)
equal_A = histogram_equalization(C)
f, (ax0, ax1) = plt.subplots(1, 2)
f.tight_layout()
ax0.imshow(A, cmap=plt.cm.gray)
ax0.set_title('Input image')
ax1.imshow(bright_A, cmap=plt.cm.gray)
ax1.set_title('Image brightened, gamma = , c = ')
f1, (ax0, ax1, ax2, ax3) = plt.subplots(1, 4)
f1.tight_layout()
ax0.imshow(A, cmap=plt.cm.gray)
ax0.set_title('Input image')
ax1.hist(A.flatten(), bins=255, range=(0, 255), histtype='bar')
ax1.set_title('Histogram of input image')
ax2.imshow(equal_A, cmap=plt.cm.gray)
ax2.set_title('Output image')
ax3.hist(equal_A.flatten(), bins=255, range=(0, 255), histtype='bar')
ax3.set_title('Histogram of output image')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment