-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoecf_class.py
341 lines (237 loc) · 8.94 KB
/
oecf_class.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# -*- coding: utf-8 -*-
import math
from colormath.color_conversions import convert_color
from colormath.color_objects import LabColor, LCHabColor
#from scipy.signal import savgol_filter
import numpy as np
class GetOECFClass:
def __init__(self, img_rgb, img_lab, ref_lab):
self.imgRGB = img_rgb
#self.refRGB = ref_rgb
self.refLab = ref_lab
self.imgLab = img_lab
self.density = self.get_density_name(ref_lab)
def get_density_name(self, reference):
patchName = []
for patch in reference:
patchName.append(patch["D_VIS"])
return patchName
def new_order_by_list(self, dest):
destino = np.array(dest)
origen = np.array(self.density)
inds = origen.argsort()
sortedList = destino[inds]
return sortedList.tolist()
def getGain(self):
total = len(self.imgLab)
orig = []
ref = []
for x in range(total):
lOrig = self.imgLab[x]["LAB"][0]
lRef = self.refLab[x]["LAB_L"]
orig.append(lOrig)
ref.append(lRef)
npOrig = np.array(orig)
npRef = np.array(ref)
firstDerivative = np.diff(npOrig) / np.diff(npRef)
o = firstDerivative.tolist()
#o = savgol_filter(o, total, 3)
s = self.get_oecf_stats(o, "LGAIN")
sorted(self.density)
str_density = []
for d in self.density:
str_density.append(str(d))
return {"curve": o, "stats":s, "x_axis": str_density }
def get_oecf_values(self, mode):
o = []
#t = []
#to =[]
#meter aqui una validación de numero de parches!!!!
#count_rgb_img = len(self.imgRGB)
#count_rgb_ref = len(self.imgRGB)
total = len(self.imgRGB)
for x in range(total):
Ri = self.imgRGB[x]["RGB"][0]
Gi = self.imgRGB[x]["RGB"][1]
Bi = self.imgRGB[x]["RGB"][2]
luma_img = self.imgRGB[x]["RGB_LUMA"]
Rr = self.refLab[x]["RGB_R"]
Gr = self.refLab[x]["RGB_G"]
Br = self.refLab[x]["RGB_R"]
luma_ref = self.refLab[x]["LUMA"]
#print(self.imgLab[x])
if mode == "WB":
color1 = LabColor(lab_l=self.refLab[x]["LAB_L"], lab_a=self.refLab[x]["LAB_A"], lab_b=self.refLab[x]["LAB_B"] )
#print(self.refLab[x]["LAB_L"])
color2 = LabColor(lab_l=self.imgLab[x]["LAB"][0], lab_a=self.imgLab[x]["LAB"][1], lab_b=self.imgLab[x]["LAB"][2])
lch1 = convert_color(color1, LCHabColor)
lch2 = convert_color(color2, LCHabColor)
DeC = abs(lch1.lch_c - lch2.lch_c)
o.append(DeC)
if mode == "L-OECF":
L_REF = self.refLab[x]["LAB_L"]
L_IMG = self.imgLab[x]["LAB"][0]
o.append([round(L_IMG, 0), round(L_REF, 0)])
if mode == "OECF":
#Co = self.value_to_percent(self.imgRGB[x]["RGB"])
#Ci = self.value_to_percent(self.refRGB[x])
Yo = luma_img
Yi = luma_ref
o.append([round(Yo, 0), round(Yi, 0)])
elif mode == "RGB":
o.append([Ri, Gi, Bi])
elif mode == "RED":
o.append([Ri, Rr])
elif mode == "GREEN":
o.append([Gi, Gr])
elif mode == "BLUE":
o.append([Bi, Br])
elif mode == "DEV":
Yo = luma_img
Yi = luma_ref
dev = self.get_delta_ev(round(Yo, 1), round(Yi, 1))
o.append(dev)
#s = self.get_oecf_stats(o, mode)
o = self.new_order_by_list(o)
s = self.get_oecf_stats(o, mode)
#if mode == "OECF":
# slope2 = self.best_fit_slope(o)
# print("slope2", slope2)
sorted(self.density)
str_density = []
for d in self.density:
str_density.append(str(d))
return {"curve": o, "stats":s, "x_axis": str_density }
'''
def best_fit_slope(self, o):
from statistics import mean
curva = self.get_curves(o)
ys = np.array(curva, dtype=np.float64)
ys.sort()
print("Y", ys)
xs = np.array(self.desity_trans(self.density), dtype=np.float64)
xs.sort()
print("X",xs)
slope, intercept = np.polyfit(np.log(xs), np.log(ys), 1)
print("slope", slope)
#https://pythonprogramming.net/how-to-program-best-fit-line-slope-machine-learning-tutorial/
m = (((mean(xs) * mean(ys)) - mean(xs * ys)) /
((mean(xs) ** 2) - mean(xs ** 2)))
return m
'''
def desity_trans(self,d):
o = []
for x in d:
o.append(math.pow(10, -1 * float(x)))
print("transmison", o)
return o
def get_curves(self,c):
o = []
for x in c:
o.append(x[0]/255)
print("curvaY", o)
return o
def get_oecf_stats(self, arr, mode):
t = []
# print(arr)
for x in range(len(arr)):
# print(len(arr[x]) )
if type(arr[x]) is list:
# is OECF
if len(arr[x]) == 2:
d = abs(arr[x][0] - arr[x][1])
t.append(d)
# is RGB
if len(arr[x]) == 3:
# print(self.stddev( arr[x], ddof=0))
t.append(self.stddev(arr[x], ddof=0))
else:
# is DEV or WB
t.append(arr[x])
if mode == "WB":
o = {"units": "∆C",
"Err Avg": str(round(self.mean(t), 2)),
"Err Max": str(round(max(t), 2)),
"Err Min": str(round(min(t), 2)),
"Err Desv": str(round(self.stddev(t, ddof=0), 2))
}
if mode == "LGAIN":
o = {"units": "",
"Err Avg": str(round(self.mean(t), 2)),
"Err Max": str(round(max(t), 2)),
"Err Min": str(round(min(t), 2))
}
if mode == "L-OECF":
o = {"units": "L*",
"Err Avg": str(round(self.mean(t), 2)),
"Err Max": str(round(max(t), 2)),
"Err Min": str(round(min(t), 2)),
"Err Desv": str(round(self.stddev(t, ddof=0), 2))
}
if mode == "OECF":
o = {"units": "cv",
"Err Avg": str(round(self.mean(t), 2)),
"Err Max": str(round(max(t), 2)),
"Err Min": str(round(min(t), 2)),
"Err Desv": str(round(self.stddev(t, ddof=0), 2))
}
elif mode == "RED" or mode == "GREEN" or mode == "BLUE":
o = {"units": "cv",
"Err Avg": str(round(self.mean(t), 2)),
"Err Max": str(round(max(t), 2)),
"Err Min": str(round(min(t), 2)),
"Err Desv": str(round(self.stddev(t, ddof=0), 2))
}
elif mode == "RGB":
o = {"units": "cv",
"Desv Avg": str(round(self.mean(t), 2)),
"Desv Max": str(round(max(t), 2)),
"Desv Min": str(round(min(t), 2))
}
elif mode == "DEV":
o = {"units": "∆EV",
"Err Average": str(round(self.mean(t), 2)),
"Err Max": str(round(max(t), 2)),
"Err Min": str(round(min(t), 2)),
"Err Desv": str(round(self.stddev(t, ddof=0), 2))
}
return o
def gain_modulation(self,sI, SI1, rI, rI1):
o = (sI - SI1) / (rI - rI1)
return o
def value_to_percent(self, color):
o = []
for x in color:
c = (x / 255) * 100
o.append(c)
return o
def get_luma(self, RGB):
if len(RGB) > 1:
y = 0.2126 * RGB[0] + 0.7152 * RGB[1] + 0.0722 * RGB[2]
else:
y = RGB[0]
return y
def get_delta_ev(self, Yo, Yi):
return (math.log(Yo) - math.log(Yi)) / math.log(2)
def mean(self, data):
"""Return the sample arithmetic mean of data."""
n = len(data)
if n < 1:
raise ValueError('mean requires at least one data point')
# print(data)
return sum(data) / float(n) # in Python 2 use sum(data)/float(n)
def _ss(self, data):
"""Return sum of square deviations of sequence data."""
c = self.mean(data)
ss = sum((x - c) ** 2 for x in data)
return ss
def stddev(self, data, ddof=0):
"""Calculates the population standard deviation
by default; specify ddof=1 to compute the sample
standard deviation."""
n = len(data)
if n < 2:
raise ValueError('variance requires at least two data points')
ss = self._ss(data)
pvar = ss / (n - ddof)
return pvar ** 0.5