2 Dimensional Local Histogram Equalization
In [1]:
### Run below if necessary
##import sys
##sys.path.append('/usr/local/lib/python2.7/site-packages')
import math
import csv,gc
import matplotlib
import numpy as np
import cv2
#%matplotlib
BINS = 32
In [2]:
import matplotlib.pyplot as plt
%matplotlib inline
In [4]:
from skimage import data, img_as_float
from skimage import exposure
import cv2
In [19]:
img = [
[52, 55, 61, 66, 70, 61, 64, 73],
[63, 59, 55, 90, 109, 85, 69, 72],
[62, 59, 68, 113, 144, 104, 66, 73],
[63, 58, 71, 122, 154, 106, 70, 69],
[67, 61, 68, 104, 126, 88, 68, 70],
[79, 65, 60, 70, 77, 68, 58, 75],
[85, 71, 64, 59, 55, 61, 65, 83],
[87, 79, 69, 68, 65, 76, 78, 94]
]
img = np.asarray(img)
print img
print " "
print img[0]
print img[1]
print " "
imgflat = img.reshape(-1)
print imgflat
print imgflat.sum()
print " "
fig = plt.hist(imgflat, bins=255)
plt.title('Histogram')
plt.show()
print " "
#clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe = cv2.createCLAHE()
img_grey = np.array(img * 255, dtype = np.uint8)
#threshed = cv2.adaptiveThreshold(img_grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 0)
cl1 = clahe.apply(img_grey)
#cv2.imwrite('clahe_2.jpg',cl1)
#cv2.startWindowThread()
#cv2.namedWindow("adaptive")
#cv2.imshow("adaptive", cl1)
#cv2.imshow("adaptive", threshed)
#plt.imshow(threshed)
print " "
localimgflat = cl1.reshape(-1)
print localimgflat
print localimgflat.sum()
print " "
fig = plt.hist(localimgflat, bins=255)
plt.title('Locally Equalized Histogram')
plt.show()
In [18]:
imgnorm = imgflat*1.0/sum(imgflat)
print imgnorm
plt.hist(imgnorm, bins = 255)
plt.title('Normalized Histogram')
plt.show
Out[18]:
In [ ]: