Skip to content

Commit

Permalink
smash the ccm step together?
Browse files Browse the repository at this point in the history
Inclined to think the two-pass may be better, though
  • Loading branch information
sz3 committed Dec 10, 2023
1 parent fa9e21f commit 38d7990
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions cimbar/cimbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,12 @@ def update(c, r, g, b):
else:
pos = [(67, 0), (0, 67), (conf.TOTAL_SIZE-79, 0), (0, conf.TOTAL_SIZE-79)]

colors = []
for x, y in pos:
iblock = img.crop((x, y, x + 4, y + 4))
r, g, b = avg_color(iblock, False)
update(cc, *avg_color(iblock, False))
ac = avg_color(iblock, False)
colors.append(ac)
update(cc, *ac)

print(f'tint is {cc}')
return cc['r'], cc['g'], cc['b']
Expand Down Expand Up @@ -307,8 +309,9 @@ def _decode_iter(ct, img, color_img, state_info={}):
#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())])
exp = numpy.array(possible_colors(ct.dark, BITS_PER_COLOR) + [(255,255,255)])
white = state_info['white']
observed = numpy.array([v for k,v in sorted(color_lookups[0].items())] + [white])
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
der = matrix_colour_correction_Cheung2004(observed, exp)

Expand All @@ -318,13 +321,14 @@ def _decode_iter(ct, img, color_img, state_info={}):
else: # cc_setting == 3,5
ct.ccm = der.dot(ct.ccm)

if splits:
if splits: # 6,7
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
exp = numpy.array(possible_colors(ct.dark, BITS_PER_COLOR))
exp = numpy.array(possible_colors(ct.dark, BITS_PER_COLOR) + [(255,255,255)])
white = state_info['white']
ccms = list()
i = 0
while i < splits:
observed = numpy.array([v for k,v in sorted(color_lookups[i].items())])
observed = numpy.array([v for k,v in sorted(color_lookups[i].items())] + [white])
der = matrix_colour_correction_Cheung2004(observed, exp)
ccms.append(der)
i += 1
Expand Down Expand Up @@ -369,22 +373,19 @@ def decode_iter(src_image, dark, should_preprocess, color_correct, deskew, auto_
img = _preprocess_for_decode(color_img) if should_preprocess else color_img

if color_correct:
from colormath.chromatic_adaptation import _get_adaptation_matrix
ct.ccm = white = _get_adaptation_matrix(numpy.array([*compute_tint(color_img, dark)]),
numpy.array([255, 255, 255]), 2, 'von_kries')
white = compute_tint(color_img, dark)
if color_correct == 2:
from colormath.chromatic_adaptation import _get_adaptation_matrix
ct.ccm = _get_adaptation_matrix(numpy.array([*white]),
numpy.array([255, 255, 255]), 2, 'von_kries')
for _ in _decode_iter(ct, img, color_img):
pass
clusters = ClusterSituation(ct.color_metrics, 2**BITS_PER_COLOR)
clusters.plot('/tmp/foo.png')
clusters.index = {i: ct.best_color(*k) for i,k in enumerate(clusters.centers())}
ct.color_clusters = clusters

'''observed = [c for _, c in ct.color_metrics]
exp = numpy.array(possible_colors(dark, BITS_PER_COLOR))
from colour.characterisation.correction import matrix_colour_correction_Cheung2004
der = matrix_colour_correction_Cheung2004(observed, exp)
ct.ccm = der.dot(white)'''
else:
state_info['white'] = white

yield from _decode_iter(ct, img, color_img, state_info)

Expand Down

0 comments on commit 38d7990

Please sign in to comment.