-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnevaluate.py
212 lines (161 loc) · 5.02 KB
/
nevaluate.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
'''
Metrics for unferwater image quality evaluation.
Author: Xuelei Chen
Email: chenxuelei@hotmail.com
Usage:
python evaluate.py RESULT_PATH
'''
import numpy as np
from skimage.measure import compare_psnr, compare_ssim
import math
import sys
from skimage import io, color, filters
import os
import math
def nmetrics(a):
rgb = a
lab = color.rgb2lab(a)
gray = color.rgb2gray(a)
# UCIQE
c1 = 0.4680
c2 = 0.2745
c3 = 0.2576
l = lab[:,:,0]
#1st term
chroma = (lab[:,:,1]**2 + lab[:,:,2]**2)**0.5
uc = np.mean(chroma)
sc = (np.mean((chroma - uc)**2))**0.5
#2nd term
top = np.int(np.round(0.01*l.shape[0]*l.shape[1]))
sl = np.sort(l,axis=None)
isl = sl[::-1]
conl = np.mean(isl[:top])-np.mean(sl[:top])
#3rd term
satur = []
chroma1 = chroma.flatten()
l1 = l.flatten()
for i in range(len(l1)):
if chroma1[i] == 0: satur.append(0)
elif l1[i] == 0: satur.append(0)
else: satur.append(chroma1[i] / l1[i])
us = np.mean(satur)
uciqe = c1 * sc + c2 * conl + c3 * us
# UIQM
p1 = 0.0282
p2 = 0.2953
p3 = 3.5753
#1st term UICM
rg = rgb[:,:,0] - rgb[:,:,1]
yb = (rgb[:,:,0] + rgb[:,:,1]) / 2 - rgb[:,:,2]
rgl = np.sort(rg,axis=None)
ybl = np.sort(yb,axis=None)
al1 = 0.1
al2 = 0.1
T1 = np.int(al1 * len(rgl))
T2 = np.int(al2 * len(rgl))
rgl_tr = rgl[T1:-T2]
ybl_tr = ybl[T1:-T2]
urg = np.mean(rgl_tr)
s2rg = np.mean((rgl_tr - urg) ** 2)
uyb = np.mean(ybl_tr)
s2yb = np.mean((ybl_tr- uyb) ** 2)
uicm =-0.0268 * np.sqrt(urg**2 + uyb**2) + 0.1586 * np.sqrt(s2rg + s2yb)
#2nd term UISM (k1k2=8x8)
Rsobel = rgb[:,:,0] * filters.sobel(rgb[:,:,0])
Gsobel = rgb[:,:,1] * filters.sobel(rgb[:,:,1])
Bsobel = rgb[:,:,2] * filters.sobel(rgb[:,:,2])
Rsobel=np.round(Rsobel).astype(np.uint8)
Gsobel=np.round(Gsobel).astype(np.uint8)
Bsobel=np.round(Bsobel).astype(np.uint8)
Reme = eme(Rsobel)
Geme = eme(Gsobel)
Beme = eme(Bsobel)
uism = 0.299 * Reme + 0.587 * Geme + 0.114 * Beme
#3rd term UIConM
uiconm = logamee(gray)
uiqm = p1 * uicm + p2 * uism + p3 * uiconm
return uiqm,uciqe
def eme(ch,blocksize=8):
num_x = math.ceil(ch.shape[0] / blocksize)
num_y = math.ceil(ch.shape[1] / blocksize)
eme = 0
w = 2. / (num_x * num_y)
for i in range(num_x):
xlb = i * blocksize
if i < num_x - 1:
xrb = (i+1) * blocksize
else:
xrb = ch.shape[0]
for j in range(num_y):
ylb = j * blocksize
if j < num_y - 1:
yrb = (j+1) * blocksize
else:
yrb = ch.shape[1]
block = ch[xlb:xrb,ylb:yrb]
blockmin = np.float(np.min(block))
blockmax = np.float(np.max(block))
# # old version
# if blockmin == 0.0: eme += 0
# elif blockmax == 0.0: eme += 0
# else: eme += w * math.log(blockmax / blockmin)
# new version
if blockmin == 0: blockmin+=1
if blockmax == 0: blockmax+=1
eme += w * math.log(blockmax / blockmin)
return eme
def plipsum(i,j,gamma=1026):
return i + j - i * j / gamma
def plipsub(i,j,k=1026):
return k * (i - j) / (k - j)
def plipmult(c,j,gamma=1026):
return gamma - gamma * (1 - j / gamma)**c
def logamee(ch,blocksize=8):
num_x = math.ceil(ch.shape[0] / blocksize)
num_y = math.ceil(ch.shape[1] / blocksize)
s = 0
w = 1. / (num_x * num_y)
for i in range(num_x):
xlb = i * blocksize
if i < num_x - 1:
xrb = (i+1) * blocksize
else:
xrb = ch.shape[0]
for j in range(num_y):
ylb = j * blocksize
if j < num_y - 1:
yrb = (j+1) * blocksize
else:
yrb = ch.shape[1]
block = ch[xlb:xrb,ylb:yrb]
blockmin = np.float(np.min(block))
blockmax = np.float(np.max(block))
top = plipsub(blockmax,blockmin)
bottom = plipsum(blockmax,blockmin)
m = top/bottom
if m ==0.:
s+=0
else:
s += (m) * np.log(m)
return plipmult(w,s)
def main():
result_path = sys.argv[1]
result_dirs = os.listdir(result_path)
sumuiqm, sumuciqe = 0.,0.
N=0
for imgdir in result_dirs:
if '.png' in imgdir:
#corrected image
corrected = io.imread(os.path.join(result_path,imgdir))
uiqm,uciqe = nmetrics(corrected)
sumuiqm += uiqm
sumuciqe += uciqe
N +=1
with open(os.path.join(result_path,'metrics.txt'), 'a') as f:
f.write('{}: uiqm={} uciqe={}\n'.format(imgdir,uiqm,uciqe))
muiqm = sumuiqm/N
muciqe = sumuciqe/N
with open(os.path.join(result_path,'metrics.txt'), 'a') as f:
f.write('Average: uiqm={} uciqe={}\n'.format(muiqm, muciqe))
if __name__ == '__main__':
main()