Skip to content

Commit

Permalink
Quick refactor of ccm calculation logic
Browse files Browse the repository at this point in the history
-- 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 (: )
  • Loading branch information
sz3 committed Dec 10, 2023
1 parent fa9e21f commit 344bed9
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions cimbar/cimbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,41 @@ def _decode_symbols(ct, img):
yield i, best_bits, best_cell


def _calc_ccm(ct, color_lookups, cc_setting):
splits = 2 if cc_setting in (6, 7) else 0
if cc_setting in (3, 4, 5):
possible = possible_colors(ct.dark, BITS_PER_COLOR)
#if len(color_lookups[0]) < len(possible):
# return
exp = [color for i,color in enumerate(possible) if i in color_lookups[0]]
exp = numpy.array(exp)
observed = numpy.array([v for k,v in sorted(color_lookups[0].items())])
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
der = matrix_colour_correction_Cheung2004(observed, exp)

# not sure which of this would be better...
if ct.ccm is None or cc_setting == 4:
ct.ccm = der
else: # cc_setting == 3,5
ct.ccm = der.dot(ct.ccm)

if splits:
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
exp = numpy.array(possible_colors(ct.dark, BITS_PER_COLOR))
ccms = list()
i = 0
while i < splits:
observed = numpy.array([v for k,v in sorted(color_lookups[i].items())])
der = matrix_colour_correction_Cheung2004(observed, exp)
ccms.append(der)
i += 1

if ct.ccm is None or cc_setting == 7:
ct.ccm = ccms
else:
ct.ccm = [der.dot(ct.ccm) for der in ccms]


def _decode_iter(ct, img, color_img, state_info={}):
decoding = sorted(_decode_symbols(ct, img))
if use_split_mode():
Expand All @@ -306,39 +341,7 @@ def _decode_iter(ct, img, color_img, state_info={}):
#matrix_colour_correction_Cheung2004
#matrix_colour_correction_Finlayson2015

if cc_setting in (3, 4, 5):
exp = numpy.array(possible_colors(ct.dark, BITS_PER_COLOR))
observed = numpy.array([v for k,v in sorted(color_lookups[0].items())])
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
der = matrix_colour_correction_Cheung2004(observed, exp)

# not sure which of this would be better...
if ct.ccm is None or cc_setting == 4:
ct.ccm = der
else: # cc_setting == 3,5
ct.ccm = der.dot(ct.ccm)

if splits:
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
exp = numpy.array(possible_colors(ct.dark, BITS_PER_COLOR))
ccms = list()
i = 0
while i < splits:
observed = numpy.array([v for k,v in sorted(color_lookups[i].items())])
der = matrix_colour_correction_Cheung2004(observed, exp)
ccms.append(der)
i += 1

if ct.ccm is None or cc_setting == 7:
ct.ccm = ccms
else:
ct.ccm = [der.dot(ct.ccm) for der in ccms]

if cc_setting == 5:
ct.colors = color_lookups[0]
if cc_setting == 10:
ct.disable_color_scaling = True
ct.colors = color_lookups[0]
_calc_ccm(ct, color_lookups, cc_setting)

print('beginning decode colors pass...')
midX = conf.TOTAL_SIZE // 2
Expand Down

0 comments on commit 344bed9

Please sign in to comment.