Newer
Older
CalcXYZ / calc_cc_xyz.py
import csv

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d


def read_spectrum_csv(filename, wl):
    # csv読み込み
    with open(filename, newline="") as f:
        csvreader = csv.reader(f)
        data_org = np.array([[float(val) for val in row] for row in csvreader])
    wl_org = data_org[:, 0]

    # return wl_org, data[:, 1:]
    data = np.zeros([len(wl), data_org.shape[-1] - 1])
    for i in range(data.shape[-1]):
        resampler = interp1d(wl_org, data_org[:, 1 + i], kind="linear")
        data[:, i] = resampler(wl)
    return data


wl = np.linspace(400, 700, 61)
cmf = read_spectrum_csv("cmf.csv", wl)
d65 = read_spectrum_csv("d65.csv", wl)
cc24 = read_spectrum_csv("cc24.csv", wl)

print(d65.shape, cmf[:, 1].shape)
K = 100.0 / (d65[:, 0] @ cmf[:, 1])
XYZw = (d65.T @ cmf) * K
print("White point of D65 = ", XYZw[0])
rep = np.repeat(d65.T, cc24.shape[-1], 0)
print(rep.shape, cc24.T.shape)
XYZcc24 = ((rep * cc24.T) @ cmf) * K
print(XYZcc24)

plt.subplot(1, 3, 1)
plt.plot(wl, cmf)
plt.title("Color Matching Func")
plt.subplot(1, 3, 2)
plt.plot(wl, d65)
plt.title("D65 illuminant")
plt.subplot(1, 3, 3)
plt.plot(wl, cc24)
plt.title("Target spectrum")

plt.show()

np.savetxt("XYZ_output.csv", XYZcc24, delimiter=",", fmt="%.4f")