Skip to content

Commit 344bed9

Browse files
committed
Quick refactor of ccm calculation logic
-- not sure what to do when the sampling data is bad, I *think* we might just want to bail. (has implications for 8-color though (: )
1 parent fa9e21f commit 344bed9

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

cimbar/cimbar.py

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,41 @@ def _decode_symbols(ct, img):
285285
yield i, best_bits, best_cell
286286

287287

288+
def _calc_ccm(ct, color_lookups, cc_setting):
289+
splits = 2 if cc_setting in (6, 7) else 0
290+
if cc_setting in (3, 4, 5):
291+
possible = possible_colors(ct.dark, BITS_PER_COLOR)
292+
#if len(color_lookups[0]) < len(possible):
293+
# return
294+
exp = [color for i,color in enumerate(possible) if i in color_lookups[0]]
295+
exp = numpy.array(exp)
296+
observed = numpy.array([v for k,v in sorted(color_lookups[0].items())])
297+
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
298+
der = matrix_colour_correction_Cheung2004(observed, exp)
299+
300+
# not sure which of this would be better...
301+
if ct.ccm is None or cc_setting == 4:
302+
ct.ccm = der
303+
else: # cc_setting == 3,5
304+
ct.ccm = der.dot(ct.ccm)
305+
306+
if splits:
307+
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
308+
exp = numpy.array(possible_colors(ct.dark, BITS_PER_COLOR))
309+
ccms = list()
310+
i = 0
311+
while i < splits:
312+
observed = numpy.array([v for k,v in sorted(color_lookups[i].items())])
313+
der = matrix_colour_correction_Cheung2004(observed, exp)
314+
ccms.append(der)
315+
i += 1
316+
317+
if ct.ccm is None or cc_setting == 7:
318+
ct.ccm = ccms
319+
else:
320+
ct.ccm = [der.dot(ct.ccm) for der in ccms]
321+
322+
288323
def _decode_iter(ct, img, color_img, state_info={}):
289324
decoding = sorted(_decode_symbols(ct, img))
290325
if use_split_mode():
@@ -306,39 +341,7 @@ def _decode_iter(ct, img, color_img, state_info={}):
306341
#matrix_colour_correction_Cheung2004
307342
#matrix_colour_correction_Finlayson2015
308343

309-
if cc_setting in (3, 4, 5):
310-
exp = numpy.array(possible_colors(ct.dark, BITS_PER_COLOR))
311-
observed = numpy.array([v for k,v in sorted(color_lookups[0].items())])
312-
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
313-
der = matrix_colour_correction_Cheung2004(observed, exp)
314-
315-
# not sure which of this would be better...
316-
if ct.ccm is None or cc_setting == 4:
317-
ct.ccm = der
318-
else: # cc_setting == 3,5
319-
ct.ccm = der.dot(ct.ccm)
320-
321-
if splits:
322-
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
323-
exp = numpy.array(possible_colors(ct.dark, BITS_PER_COLOR))
324-
ccms = list()
325-
i = 0
326-
while i < splits:
327-
observed = numpy.array([v for k,v in sorted(color_lookups[i].items())])
328-
der = matrix_colour_correction_Cheung2004(observed, exp)
329-
ccms.append(der)
330-
i += 1
331-
332-
if ct.ccm is None or cc_setting == 7:
333-
ct.ccm = ccms
334-
else:
335-
ct.ccm = [der.dot(ct.ccm) for der in ccms]
336-
337-
if cc_setting == 5:
338-
ct.colors = color_lookups[0]
339-
if cc_setting == 10:
340-
ct.disable_color_scaling = True
341-
ct.colors = color_lookups[0]
344+
_calc_ccm(ct, color_lookups, cc_setting)
342345

343346
print('beginning decode colors pass...')
344347
midX = conf.TOTAL_SIZE // 2

0 commit comments

Comments
 (0)