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
|
import numpy as np, scipy.optimize as optimize
from colour import *
from colour.difference import *
# The same wavelength grid is used throughout
wvl = np.arange(360, 830, 1)
# Map wavelenghts from 360--830 nm to 0-1
def remap(wvl):
return (wvl - 360) / (830 - 360)
# This is the model of spectral reflectivity described in the article.
def model(wvl, cc):
x = cc[0] * wvl ** 2 + cc[1] * wvl + cc[2]
return 1 / 2 + x / (2 * np.sqrt(1 + x ** 2))
# The goal is to minimize the color difference between a given distrbution
# and the one computed from the model above.
def error_function(cc, target, ill_sd, ill_xy):
# One thing they didn't mention in the text is that their polynomial
# is nondimensionalized (mapped from 360--800 nm to 0--1).
ev = model((wvl - 360) / (830 - 360), cc)
sd = SpectralDistribution(ev, wvl)
Lab = XYZ_to_Lab(sd_to_XYZ(sd, illuminant=ill_sd) / 100, ill_xy)
return delta_E_CIE1976(target, Lab)
# This callback to scipy.optimize.basinhopping tells the solver to stop once
# the error is small enough. The threshold was chosen arbitrarily, as a small
# fraction of the JND (about 2.3 with this metric).
def cb_basinhopping(x, f, accept):
return f < 0.1
# Finds the parameters for Jakob and Hanika's model
def jakob_hanika(target, ill_sd, ill_xy, x0=(0, 0, 0)):
# First, a conventional solver is called. For 'yellow green' this
# actually fails: gets stuck at a local minimum that's far away
# from the global one.
# FIXME: better parameters, a better x0, a better method?
# FIXME: stop iterating as soon as delta E is negligible
opt = optimize.minimize(
error_function, x0, (target, ill_sd, ill_xy),
method="L-BFGS-B", options={"disp": True, "ftol": 1e-5}
)
print(opt)
error = error_function(opt.x, target, ill_sd, ill_xy)
print("Delta E is %g" % error)
# Basin hopping is far more likely to find the actual minimum we're
# looking for, but it's extremely slow in comparison.
if error > 0.1:
print("Error too large, trying global optimization")
opt = optimize.basinhopping(
lambda cc: error_function(cc, target, ill_sd, ill_xy),
x0, disp=True, callback=cb_basinhopping
)
print(opt)
error = error_function(opt.x, target, ill_sd, ill_xy)
print("Global delta E is %g" % error)
# Map back to dimensional coefficients
cc = np.array([
opt.x[0] / 220900,
opt.x[1] / 470 - 36/11045 * opt.x[0],
opt.x[2] - 36/47 * opt.x[1] + 1296/2209 * opt.x[0]
])
matched_sd = SpectralDistribution(model(wvl, cc), wvl, name="Model")
return cc, matched_sd, error
|