-
Notifications
You must be signed in to change notification settings - Fork 0
/
clahe.py
197 lines (178 loc) · 6.3 KB
/
clahe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import numpy as np
import matplotlib.pyplot as plt
import cv2
#INTERPOLATION FUNCTION
def interpolate(subBin,LU,RU,LB,RB,subX,subY):
subImage = np.zeros(subBin.shape)
num = subX*subY
for i in range(subX):
inverseI = subX-i
for j in range(subY):
inverseJ = subY-j
val = subBin[i,j].astype(int)
subImage[i,j] = np.floor((inverseI*(inverseJ*LU[val] + j*RU[val])+ i*(inverseJ*LB[val] + j*RB[val]))/float(num))
return subImage
#CLAHE FUNCTION
#ALL UTILITY FUNCTIONS COMBINED INTO ONE FUNCTION
def clahe(img,clipLimit,nrBins=128,nrX=0,nrY=0):
'''img - Input image
clipLimit - Normalized clipLimit. Higher value gives more contrast
nrBins - Number of graylevel bins for histogram("dynamic range")
nrX - Number of contextial regions in X direction
nrY - Number of Contextial regions in Y direction'''
h,w = img.shape
if clipLimit==1:
return
nrBins = max(nrBins,128)
if nrX==0:
#Taking dimensions of each contextial region to be a square of 32X32
xsz = 32
ysz = 32
nrX = np.ceil(h/xsz).astype(int)#240
#Excess number of pixels to get an integer value of nrX and nrY
excX= int(xsz*(nrX-h/xsz))
nrY = np.ceil(w/ysz).astype(int)#320
excY= int(ysz*(nrY-w/ysz))
#Pad that number of pixels to the image
if excX!=0:
img = np.append(img,np.zeros((excX,img.shape[1])).astype(int),axis=0)
if excY!=0:
img = np.append(img,np.zeros((img.shape[0],excY)).astype(int),axis=1)
else:
xsz = round(h/nrX)
ysz = round(w/nrY)
nrPixels = xsz*ysz
xsz2 = round(xsz/2)
ysz2 = round(ysz/2)
claheimg = np.zeros(img.shape)
if clipLimit > 0:
clipLimit = max(1,clipLimit*xsz*ysz/nrBins)
else:
clipLimit = 50
#makeLUT
print("...Make the LUT...")
minVal = 0 #np.min(img)
maxVal = 255 #np.max(img)
#maxVal1 = maxVal + np.maximum(np.array([0]),minVal) - minVal
#minVal1 = np.maximum(np.array([0]),minVal)
binSz = np.floor(1+(maxVal-minVal)/float(nrBins))
LUT = np.floor((np.arange(minVal,maxVal+1)-minVal)/float(binSz))
#BACK TO CLAHE
bins = LUT[img]
print(bins.shape)
#makeHistogram
print("...Making the Histogram...")
hist = np.zeros((nrX,nrY,nrBins))
print(nrX,nrY,hist.shape)
for i in range(nrX):
for j in range(nrY):
bin_ = bins[i*xsz:(i+1)*xsz,j*ysz:(j+1)*ysz].astype(int)
for i1 in range(xsz):
for j1 in range(ysz):
hist[i,j,bin_[i1,j1]]+=1
#clipHistogram
print("...Clipping the Histogram...")
if clipLimit>0:
for i in range(nrX):
for j in range(nrY):
nrExcess = 0
for nr in range(nrBins):
excess = hist[i,j,nr] - clipLimit
if excess>0:
nrExcess += excess
binIncr = nrExcess/nrBins
upper = clipLimit - binIncr
for nr in range(nrBins):
if hist[i,j,nr] > clipLimit:
hist[i,j,nr] = clipLimit
else:
if hist[i,j,nr]>upper:
nrExcess += upper - hist[i,j,nr]
hist[i,j,nr] = clipLimit
else:
nrExcess -= binIncr
hist[i,j,nr] += binIncr
if nrExcess > 0:
stepSz = max(1,np.floor(1+nrExcess/nrBins))
for nr in range(nrBins):
nrExcess -= stepSz
hist[i,j,nr] += stepSz
if nrExcess < 1:
break
#mapHistogram
print("...Mapping the Histogram...")
map_ = np.zeros((nrX,nrY,nrBins))
#print(map_.shape)
scale = (maxVal - minVal)/float(nrPixels)
for i in range(nrX):
for j in range(nrY):
sum_ = 0
for nr in range(nrBins):
sum_ += hist[i,j,nr]
map_[i,j,nr] = np.floor(min(minVal+sum_*scale,maxVal))
#BACK TO CLAHE
#INTERPOLATION
print("...interpolation...")
xI = 0
for i in range(nrX+1):
if i==0:
subX = int(xsz/2)
xU = 0
xB = 0
elif i==nrX:
subX = int(xsz/2)
xU = nrX-1
xB = nrX-1
else:
subX = xsz
xU = i-1
xB = i
yI = 0
for j in range(nrY+1):
if j==0:
subY = int(ysz/2)
yL = 0
yR = 0
elif j==nrY:
subY = int(ysz/2)
yL = nrY-1
yR = nrY-1
else:
subY = ysz
yL = j-1
yR = j
UL = map_[xU,yL,:]
UR = map_[xU,yR,:]
BL = map_[xB,yL,:]
BR = map_[xB,yR,:]
#print("CLAHE vals...")
subBin = bins[xI:xI+subX,yI:yI+subY]
#print("clahe subBin shape: ",subBin.shape)
subImage = interpolate(subBin,UL,UR,BL,BR,subX,subY)
claheimg[xI:xI+subX,yI:yI+subY] = subImage
yI += subY
xI += subX
if excX==0 and excY!=0:
return claheimg[:,:-excY]
elif excX!=0 and excY==0:
return claheimg[:-excX,:]
elif excX!=0 and excY!=0:
return claheimg[:-excX,:-excY]
else:
return claheimg
image = cv2.imread('small.jpg')
from filters import average_filter
image1 = average_filter(image[:,:,0])
image2 = average_filter(image[:,:,1])
image3 = average_filter(image[:,:,2])
clahe_img1 = clahe(image1,8,0,0)
clahe_img2 = clahe(image2,8,0,0)
clahe_img3 = clahe(image3,8,0,0)
out = cv2.merge((clahe_img3,clahe_img2,clahe_img1))
#clipLimit = 8 gave decent results on eyePACs Dataset
#and setting xsz and ysz = 32
#and calculating nrX and nrY
fig,axs = plt.subplots(1,2,figsize=(200,100))
axs[0].imshow(image,cmap='gray')
axs[1].imshow(out,cmap='gray')
plt.show()