-
Notifications
You must be signed in to change notification settings - Fork 8
/
colorconv.py
415 lines (307 loc) · 9.96 KB
/
colorconv.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import scipy as sp
# ------------------------------------------------------------------------------
def gray_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
# grayscale conversion
out = 0.2989*arr[:,:,0] + \
0.5870*arr[:,:,1] + \
0.1141*arr[:,:,2]
#out.shape = out.shape + (1,)
return out
# ------------------------------------------------------------------------------
def opp_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
out = sp.empty_like(arr)
# red-green
out[:,:,0] = arr[:,:,0] - arr[:,:,1]
# blue-yellow
out[:,:,1] = arr[:,:,2] - arr[:,:,[0,1]].min(2)
# intensity
out[:,:,2] = arr.max(2)
return out
# ------------------------------------------------------------------------------
def oppnorm_convert(arr, threshold=0.1):
#assert(arr.min()>=0 and arr.max()<=1)
#out = sp.empty_like(arr)
arr = arr.astype('float32')
out = sp.empty(arr.shape[:2]+(2,), dtype='float32')
print out.shape
# red-green
out[:,:,0] = arr[:,:,0] - arr[:,:,1]
# blue-yellow
out[:,:,1] = arr[:,:,2] - arr[:,:,[0,1]].min(2)
# intensity
denom = arr.max(2)
mask = denom < threshold#*denom[:,:,2].mean()
out[:,:,0] /= denom
out[:,:,1] /= denom
sp.putmask(out[:,:,0], mask, 0)
sp.putmask(out[:,:,1], mask, 0)
return out
# ------------------------------------------------------------------------------
def chrom_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
opp = opp_convert(arr)
out = sp.empty_like(opp[:,:,[0,1]])
rg = opp[:,:,0]
by = opp[:,:,1]
intensity = opp[:,:,2]
lowi = intensity < 0.1*intensity.max()
rg[lowi] = 0
by[lowi] = 0
denom = intensity
denom[denom==0] = 1
out[:,:,0] = rg / denom
out[:,:,1] = by / denom
return out
# ------------------------------------------------------------------------------
def rg2_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
out = sp.empty_like(arr[:,:,[0,1]])
red = arr[:,:,0]
green = arr[:,:,1]
blue = arr[:,:,2]
intensity = arr.mean(2)
lowi = intensity < 0.1*intensity.max()
arr[lowi] = 0
denom = arr.sum(2)
denom[denom==0] = 1
out[:,:,0] = red / denom
out[:,:,1] = green / denom
return out
# ------------------------------------------------------------------------------
def hsv_convert(arr):
""" fast rgb_to_hsv using numpy array """
# adapted from Arnar Flatberg
# http://www.mail-archive.com/[email protected]/msg06147.html
# it now handles NaN properly and mimics colorsys.rgb_to_hsv output
import numpy as np
#assert(arr.min()>=0 and arr.max()<=1)
#arr = arr/255.
arr = arr.astype("float32")
out = np.empty_like(arr)
arr_max = arr.max(-1)
delta = arr.ptp(-1)
s = delta / arr_max
s[delta==0] = 0
# red is max
idx = (arr[:,:,0] == arr_max)
out[idx, 0] = (arr[idx, 1] - arr[idx, 2]) / delta[idx]
# green is max
idx = (arr[:,:,1] == arr_max)
out[idx, 0] = 2. + (arr[idx, 2] - arr[idx, 0] ) / delta[idx]
# blue is max
idx = (arr[:,:,2] == arr_max)
out[idx, 0] = 4. + (arr[idx, 0] - arr[idx, 1] ) / delta[idx]
out[:,:,0] = (out[:,:,0]/6.0) % 1.0
out[:,:,1] = s
out[:,:,2] = arr_max
# rescale back to [0, 255]
#out *= 255.
# remove NaN
out[np.isnan(out)] = 0
return out
# ------------------------------------------------------------------------------
def rgb_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
# force 3 dims
if arr.ndim == 2 or arr.shape[2] == 1:
arr_new = sp.empty(arr.shape[:2] + (3,), dtype="float32")
arr_new[:,:,0] = arr.copy()
arr_new[:,:,1] = arr.copy()
arr_new[:,:,2] = arr.copy()
arr = arr_new
return arr
# ------------------------------------------------------------------------------
def oppsande_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
r = arr[:,:,0]
g = arr[:,:,1]
b = arr[:,:,2]
out = sp.empty_like(arr)
out[:,:,0] = (r-g) / sp.sqrt(2.)
out[:,:,1] = (r+g-2.*b) / sp.sqrt(6.)
out[:,:,2] = (r+g+b) / sp.sqrt(3.)
return out
# ------------------------------------------------------------------------------
def rgbwhiten_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
r = arr[:,:,0]
rmean = r.mean()
rstd = r.std()
if rstd == 0: rstd = 1
g = arr[:,:,1]
gmean = g.mean()
gstd = g.std()
if gstd == 0: gstd = 1
b = arr[:,:,2]
bmean = b.mean()
bstd = b.std()
if bstd == 0: bstd = 1
out = sp.empty_like(arr)
out[:,:,0] = (r - rmean) / rstd
out[:,:,1] = (g - gmean) / gstd
out[:,:,2] = (b - bmean) / bstd
return out
# ------------------------------------------------------------------------------
def irg_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
r = arr[:,:,0]
g = arr[:,:,1]
b = arr[:,:,2]
intensity = arr.mean(2)
lowi = intensity < 0.1*intensity.max()
r[lowi] = 0
g[lowi] = 0
denom = intensity.copy()
denom[denom==0] = 1
out = sp.empty_like(arr)
out[:,:,0] = intensity
out[:,:,1] = r / denom
out[:,:,2] = g / denom
return out
# ------------------------------------------------------------------------------
def oppwalker_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
out = sp.empty_like(arr)
# red-green
out[:,:,0] = arr[:,:,0] - arr[:,:,1]
# blue-yellow
out[:,:,1] = arr[:,:,2] - arr[:,:,[0,1]].min(2)
# intensity
out[:,:,2] = arr.max(2)
return out
# # ------------------------------------------------------------------------------
# def chromi_convert(arr):
# #assert(arr.min()>=0 and arr.max()<=1)
# opp = oppwalker_convert(arr)
# out = sp.empty_like(opp[:,:,[0,1]])
# rg = opp[:,:,0]
# by lfini= opp[:,:,1]
# intensity = opp[:,:,2]
# lowi = intensity < 0.1*intensity.max()
# rg[lowi] = 0
# by[lowi] = 0
# denom = intensity
# denom[denom==0] = 1
# out[:,:,0] = rg / denom
# out[:,:,1] = by / denom
# return out
# ------------------------------------------------------------------------------
def invE_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
red = arr[:,:,0]
green = arr[:,:,1]
blue = arr[:,:,2]
out = sp.empty_like(arr)
out[:,:,0] = (red + green + blue) / 3.
out[:,:,1] = (red + green - 2.*blue) / 4.
out[:,:,2] = (red - 2.*green + blue) / 4.
return out
# ------------------------------------------------------------------------------
def invW_convert(arr):
#assert(arr.min()>=0 and arr.max()<=1)
invE = invE_convert(arr)
out = sp.empty_like(arr)
intensity = invE[:,:,0]
rg = invE[:,:,1]
yb = invE[:,:,2]
lowi = intensity < 0.1*intensity.max()
rg[lowi] = 0
yb[lowi] = 0
denom = intensity.copy()
denom[denom==0] = 1
out[:,:,0] = intensity
out[:,:,1] = rg / denom
out[:,:,2] = yb / denom
return out
# ------------------------------------------------------------------------------
def color_convert(arr, color_space):
#assert(arr.min()>=0 and arr.max()<=1)
# -- insure rgb
arr = rgb_convert(arr)
if color_space == 'gray':
out = gray_convert(arr)
out.shape = out.shape + (1,)
# elif color_space == 'rgb':
# pass
# elif color_space == 'hsv':
# pass
elif color_space == 'oppsande':
out = oppsande_convert(arr)
elif color_space == "invE":
out = invE_convert(arr)
elif color_space == "invW":
out = invW_convert(arr)
# elif color_space == 'oppwalker':
# pass
elif color_space == 'rgbwhiten':
out = rgbwhiten_convert(arr)
# elif color_space == 'rg':
# pass
elif color_space == 'irg':
out = irg_convert(arr)
# elif color_space == 'chrom':
# pass
elif color_space == 'chromi':
out = chromi_convert(arr)
else:
raise ValueError, "'color_space' not understood"
return out
# # -
# if color_space == 'rgb':
# arr_conv = arr
# # elif color_space == 'rg':
# # arr_conv = colorconv.rg_convert(arr)
# elif color_space == 'rg2':
# arr_conv = colorconv.rg2_convert(arr)
# elif color_space == 'gray':
# arr_conv = colorconv.gray_convert(arr)
# arr_conv.shape = arr_conv.shape + (1,)
# elif color_space == 'opp':
# arr_conv = colorconv.opp_convert(arr)
# elif color_space == 'chrom':
# arr_conv = colorconv.chrom_convert(arr)
# # elif color_space == 'opponent':
# # arr_conv = colorconv.opponent_convert(arr)
# # elif color_space == 'W':
# # arr_conv = colorconv.W_convert(arr)
# elif color_space == 'hsv':
# arr_conv = colorconv.hsv_convert(arr)
# else:
# raise ValueError, "'color_space' not understood"
# def rg_convert(arr):
# denom = arr.sum(2)
# denom[denom==0] = 1.
# out = arr / denom[:,:,None]
# out = out[:,:,[0,1]]
# return out
# def opponent_convert(arr):
# out = sp.empty_like(arr)
# r = arr[:,:,0]
# g = arr[:,:,1]
# b = arr[:,:,2]
# out[:,:,0] = (r-g) / sp.sqrt(2.)
# out[:,:,1] = (r+g-2.*b) / sp.sqrt(6.)
# out[:,:,2] = (r+g+b) / sp.sqrt(3.)
# return out
# def W_convert(arr):
# opp = opponent_convert(arr)
# out = sp.empty_like(opp[:,:,[0,1]])
# denom = opp[:,:,2]
# denom[denom==0] = 1.
# out[:,:,0] = opp[:,:,0] / denom
# out[:,:,1] = opp[:,:,1] / denom
# return out
# def W_convert2(arr):
# opp = opponent_convert(arr)
# out = sp.empty_like(opp[:,:,[0,1]])
# intensity = opp[:,:,2]
# low_intensity = intensity < intensity.max() / 10.
# intensity[intensity==0] = 1.
# out[:,:,0] = (opp[:,:,0] / intensity).clip(0, sp.inf)
# out[:,:,1] = (opp[:,:,1] / intensity).clip(0, sp.inf)
# out[:,:,:][low_intensity] = 0
# return out