-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiEBMO.py
289 lines (244 loc) · 10.3 KB
/
iEBMO.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import cv2
import sys, math, time, random, os
from datetime import datetime
from copy import deepcopy
import numpy as np
import matplotlib.pyplot as plt
from skimage.metrics import structural_similarity as ssim
from sewar.full_ref import vifp
#===================================================================
def sigmoid(gamma): #convert to probability
if gamma < 0:
return 1 - 1/(1 + math.exp(gamma/5))
else:
return 1/(1 + math.exp(-gamma/5))
def Vfunction(gamma):
# return abs(np.tanh(gamma))
#another V-shaped function
val=(math.pi/2)*gamma
val=np.arctan(val)
val=(2/math.pi)*val
return abs(val)
def initialize(popSize,dimension):
population = np.zeros((popSize,dimension))
minn = 0
maxx = 255
for i in range(popSize):
for j in range(dimension):
random.seed( i + j + time.time() )
population[i][j] = random.randint(minn,maxx)
population[i].sort()
population[i][0] = 0
population[i][dimension-1] = 255
return population
def fitness(agentCurr,inputAgent,inputImage,iterNo):
#print("fitness function executing")
# laplacian = cv2.Laplacian(img,cv2.CV_64F)
img = deepcopy(inputImage)
img2 = transformImage(agentCurr,inputAgent,inputImage,iterNo)
edges = cv2.Canny(img2,100,200)
count=0
intsum=1.1
tmp=deepcopy(img2)
dimensions=img2.shape
for r in range(0,dimensions[0]):
for c in range(0,dimensions[1]):
if (edges[r,c]==255):
count+=1
tmp[r,c]=-1
intsum+=img2[r,c]
#entropy of image
flatImg = [x for sublist in img2 for x in sublist]
uniqImg = set(flatImg)
Hx = 0
for x in uniqImg:
p_x = flatImg.count(x) / len(uniqImg)
Hx += ((- x) * (math.log2(p_x)))
#Well, How about calling the in built entropy function?
#Yep'
# will try that one if this disappoints me
# print('sum = ',sum,end=' ')
# print('log(sum) = ',math.log(sum))
tmp=deepcopy(img2)
stepSize = 5
localcontrast=1.1
(w_width, w_height) = (5, 5)
for row in range(0, tmp.shape[0] - w_height, stepSize):
for col in range(0, tmp.shape[1] - w_width , stepSize):
tmp2 = tmp[row:row + w_width, col:col + w_height]
localcontrast+=((max(tmp2.flatten()[~np.isin(tmp2.flatten(),-1)]))-(min(tmp2.flatten()[~np.isin(tmp2.flatten(),-1)])))
fit=math.log(math.log(intsum))*Hx*(count)*(1.02**(-(np.mean(img2)-np.mean(img))**2))*((max(img2.flatten())-min(img2.flatten()))**2)*((localcontrast))
# print(fit)
return fit
def allfit(population,inputAgent,inputImage,iterNo):
x=np.shape(population)[0]
acc=np.zeros(x)
for i in range(x):
acc[i]=fitness(population[i],inputAgent,inputImage,iterNo)
# print(acc[i])
return acc
def transformImage(currAgent,inputAgent,inputImage,iterNo):
tarnsImage = inputImage.copy()
row = np.shape(inputImage)[0]
col = np.shape(inputImage)[1]
currAgent.sort()
for i in range(row):
for j in range(col):
k = inputAgent.index(tarnsImage[i][j])
tarnsImage[i][j] = currAgent[k]
# cv2.imwrite("intermediate/"+str(iterNo)+'_'+str(agentNo)+'.png',tarnsImage)
# cv2.imshow('new image',tarnsImage)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
return tarnsImage
#===================================================================================
def bmoIE(imageName,popSize,maxIter):
img = cv2.imread(imageName,0)
#####################
# contrast reduction
# alpha = 0.1 # contrast of input image = alpha * contrast of original image
# beta = ((1 - alpha )*img.sum())/(img.shape[0]*img.shape[1])
# for y in range(img.shape[0]):
# for x in range(img.shape[1]):
# img[y,x] = int(np.clip(alpha*img[y,x] + beta, 0, 255))
inputImageName = imageName.split('/')[-1]
# inputImageName = "i_"+inputImageName
# cv2.imwrite("inputDIBCO/"+inputImageName,img)
histr = cv2.calcHist([img],[0],None,[256],[0,256])
plt.plot(histr)
# plt.savefig("histogramsDIBCO/hi_"+imageNameList[i]) #histogram of output image
plt.clf()
#####################
inputImage = deepcopy(img)
#print(np.shape(img))
flatImg = [x for sublist in img for x in sublist]
# print(flatImg)
uniqImg = set(flatImg)
agentLength = len(uniqImg)
#print(agentLength)
inputAgent = list(uniqImg)
inputAgent.sort()
inputImage = deepcopy(img)
# print(inputAgent)
population = initialize(popSize,agentLength)
dimension = agentLength
start_time = datetime.now()
fitList = allfit(population,inputAgent,img,0)
# temp = [-x for x in fitList]
#sort agents
# population = [x for _,x in sorted(zip(temp,population))]
arr1inds = fitList.argsort()
fitList = fitList[arr1inds[::-1]]
population = population[arr1inds[::-1]]
fitList.sort()
fitList = list(fitList)
fitList.reverse()
receivedList = []
for currIter in range(1,maxIter+1):
area = int(0.5 * popSize)
random.seed(time.time() + 10 )
parent1 = random.randint(0,popSize-1)
random.seed(time.time() + 19 )
parent2 = random.randint(0,popSize-1)
while(parent2 == parent1 and parent2 in receivedList):
parent2 = random.randint(0,popSize-1)
receivedList.append(parent2)
# print(fitList[parent1],fitList[parent2])
random.seed(time.time() + 29 )
offspring = np.multiply(random.random(),population[parent2])
if abs(parent1 - parent2 )<= area:
p = random.uniform(0,1) # we can make this ratio based on their fitness
q = (1 - p)
offspring = np.add(np.multiply(p,population[parent1]),np.multiply(q,population[parent2]))
currFit = fitness(offspring,inputAgent,inputImage,currIter)
inx = 0
while inx<popSize and fitList[inx]>currFit:
inx += 1
if inx<popSize:
population = np.insert(population,inx,offspring,axis=0)
population = np.delete(population,popSize-1,axis=0)
if inx in receivedList:
receivedList.remove(inx)
fitList.insert(inx,currFit)
# bestAgent = population[0].copy();
# print(max(fitList))
# with open("BMO_fit5.csv","a") as f:
# print(imageName,currIter, max(fitList),sep=',',file=f)
bestAgent = population[0].copy()
bestImage = transformImage(bestAgent,inputAgent,inputImage,maxIter)
# transImageName = imageName.split('/')[-1]
# transImageName = "o_"+transImageName
# cv2.imwrite("output/"+transImageName,bestImage)
time_req = datetime.now() - start_time
# print("time req:", time_req)
# histr = cv2.calcHist([bestImage],[0],None,[256],[0,256])
# plt.plot(histr)
# plt.show()
return bestImage,fitList[0]
#============================================================
pSize = [30]
maxIter = 100
#imageNameList = ['kodim01.png', 'kodim02.png', 'kodim03.png', 'kodim04.png', 'kodim05.png','kodim06.png','kodim07.png','kodim08.png','kodim09.png','kodim10.png','kodim11.png','kodim12.png','kodim13.png','kodim14.png','kodim15.png','kodim16.png','kodim17.png','kodim18.png','kodim19.png','kodim20.png','kodim21.png','kodim22.png','kodim23.png','kodim24.png']
# imageNameList = ['dibco1.png', 'dibco2.png', 'dibco3.png', 'dibco4.png', 'dibco5.png', 'dibco6.png', 'dibco7.png', 'dibco8.png', 'dibco9.png', 'dibco10.png']
#imageNameList = ['a0027.tiff', 'a0037.tiff', 'a0050.tiff']
#imageNameList = ['a0027.png', 'a0037.png', 'a0050.png']
#print(imageNameList)
# imageNameList = ['kodim01.png', 'kodim02.png', 'kodim03.png', 'kodim04.png', 'kodim05.png','kodim06.png','kodim07.png','kodim08.png','kodim09.png']
# imageNameList = ['lena_gray_256.tif','liftingbody.png','boy.jpg']
# imageNameList = ['InputBoy.jpg']
imageNameList = ['boy.jpg','lena.jpg','liftingbody.jpg','zebra.jpg']
for i in range(len(imageNameList)):
averagePsnr = 0
averageSsim = 0
averageVif = 0
print(imageNameList[i])
for popSize in pSize:
print(popSize)
# inputName = "GroundTruth/kodakDataset/"+imageNameList[i]
truthName = imageNameList[i]
trImg = cv2.imread(truthName,0)
# cv2.imshow('input',trImg)
histr = cv2.calcHist([trImg],[0],None,[256],[0,256])
plt.plot(histr)
# plt.savefig("histogramsDIBCO/ht_"+imageNameList[i]) #histogram of ground truth
plt.clf()
# cv2.imwrite("truthDIBCO/t_"+imageNameList[i],trImg)
bestImage = deepcopy(trImg)
maxPsnr = 0
maxSsim = 0
maxVif = 0
maxfit = -1000
for iteration in range(10):
outputImage,fitval = bmoIE(truthName,popSize,maxIter)
truthImage = cv2.imread(truthName,0)
psnrval = cv2.PSNR(outputImage,truthImage)
ssimval = ssim(outputImage,truthImage)
vifval = vifp(outputImage,truthImage)
#print(iteration,psnrval,ssimval,vifval,int((psnrval+ssimval+vifval)*100))
# print(psnrval, ssimval, vifval)
# continue
if (psnrval+ssimval+vifval)>maxfit:
maxfit = psnrval+ssimval+vifval
maxPsnr = psnrval
maxSsim = ssimval
maxVif = vifval
bestImage = deepcopy(outputImage)
# cv2.imshow(str(iteration+1),bestImage)
print(maxPsnr, maxSsim, maxVif)
# averagePsnr += maxPsnr
# averageSsim += maxSsim
# averageVif += maxVif
# print(i+1,averagePsnr,averageSsim,averageVif)
# histr = cv2.calcHist([bestImage],[0],None,[256],[0,256])
# plt.plot(histr)
# plt.savefig("histogramsDIBCO/ho_"+imageNameList[i]) #histogram of output image
# plt.clf()
transImageName = "o_"+imageNameList[i]
cv2.imwrite("outputDIBCO/"+transImageName,bestImage)
# averagePsnr /= len(imageNameList)
# averageSsim /= len(imageNameList)
# averageVif /= len(imageNameList)
# print("final")
# print(averagePsnr)
# print(averageSsim)
# print(averageVif)