def invert_LUT3D(LUT, size=None, extrapolate=True, query_size=4): LUT = LUT.copy() source_size = LUT.size SIZE = source_size target_size = (colour.utilities.as_int(2**(np.sqrt(source_size) + 1) + 1) if size is None else size) if target_size > 129: colour.utilities.usage_warning( 'LUT3D inverse computation time could be excessive!') if extrapolate: LUT.table = np.pad(LUT.table, [(1, 1), (1, 1), (1, 1), (0, 0)], 'reflect', reflect_type='odd') LUT.domain[0] -= 1 / (SIZE - 1) LUT.domain[1] += 1 / (SIZE - 1) LUT_intermediate = colour.LUT3D(size=target_size, domain=LUT.domain) indexes = LUT_intermediate.table LUT_intermediate.table = LUT.apply(LUT_intermediate.table) tree = cKDTree(LUT_intermediate.table.reshape(-1, 3)) LUT_inverse = colour.LUT3D(size=target_size, domain=LUT.domain) query = tree.query(indexes, query_size, n_jobs=-1)[-1] if query_size == 1: LUT_inverse.table = indexes.reshape(-1, 3)[query].reshape( [target_size, target_size, target_size, 3]) else: LUT_inverse.table = np.mean( indexes.reshape(-1, 3)[query], axis=-2).reshape([target_size, target_size, target_size, 3]) LUT_target = colour.LUT3D(size=target_size, domain=LUT.domain) LUT_target.table = LUT_inverse.apply(LUT_target.table) return LUT_target
def invert(self): """ Returns the inverted format of the current *LUT*. Returns ------- LUT3D Inverted *LUT* class instance. Warning ------- The *invert* function is in experimental state and some inversions may be inaccurate and time-taking. Examples -------- >>> LUT = LUT3D() >>> print(LUT) LUT3D - Unity 33 ---------------- <BLANKLINE> Dimensions : 3 Domain : [[ 0. 0. 0.] [ 1. 1. 1.]] Size : (33, 33, 33, 3) >>> print(LUT.invert()) LUT3D - Inverted Format ----------------------- <BLANKLINE> Dimensions : 3 Domain : [[ 0. 0. 0.] [ 1. 1. 1.]] Size : (33, 33, 33, 3) """ size = self.size LUT_inverse = colour.LUT3D(size=size, name='Inverted Format') indexes = LUT_inverse.table tree = cKDTree(self.table.reshape(-1, 3)) query = tree.query(indexes)[-1] LUT_inverse.table = indexes.reshape(-1, 3)[query].reshape( [size, size, size, 3]) return LUT_inverse
left_hand_LU = sparse.linalg.splu(left_hand, diag_pivot_thresh=0) step = (lattice_size)**2 e = sparse.identity(M, dtype=np.float32, format='lil') left_hand_inv = [] for i in tqdm(range(0, M, step)): e_begin = i e_end = e_begin + step left_hand_inv.append( left_hand_LU.solve(e[:, e_begin:e_end].todense()).astype(np.float32)) left_hand_inv = np.hstack(left_hand_inv) print('compute B') B = (right_hand @ left_hand_inv).T if args.color_space == 'lab': B = colour.Lab_to_XYZ(B) B = colour.XYZ_to_sRGB(B) B = np.clip(B, 0.0, 1.0) print('save lut') B = B.reshape((lattice_size, lattice_size, lattice_size, 3)) lut = colour.LUT3D(B, size=lattice_size) colour.write_LUT(lut, args.lut_path)
n_estimators=n_estimators, monotone_constraints=c, objective='reg:squarederror', n_jobs=8) model.fit(raw, jpeg[:, i].copy()) models.append(model) print('predict lut values') xi = np.linspace(0.0, 1.0, args.lut_size) xi = np.array([pi for pi in product(xi, xi, xi)]) if args.color_space == 'lab': xi = colour.XYZ_to_Lab(colour.sRGB_to_XYZ(xi)) pred = [model.predict(xi) for model in models] pred = np.vstack(pred).transpose() if args.color_space == 'lab': pred = colour.XYZ_to_sRGB(colour.Lab_to_XYZ(pred)) pred = np.clip(pred, 0.0, 1.0) pred = pred.reshape((args.lut_size, args.lut_size, args.lut_size, 3)) print('save lut') lut = colour.LUT3D(pred, size=args.lut_size) colour.write_LUT(lut, args.lut_path) print('save models') with open(f'{args.lut_path}.model.pkl', 'wb') as f: pickle.dump(models, f)
----------------------- <BLANKLINE> Dimensions : 3 Domain : [[ 0. 0. 0.] [ 1. 1. 1.]] Size : (33, 33, 33, 3) """ size = self.size LUT_inverse = colour.LUT3D(size=size, name='Inverted Format') indexes = LUT_inverse.table tree = cKDTree(self.table.reshape(-1, 3)) query = tree.query(indexes)[-1] LUT_inverse.table = indexes.reshape(-1, 3)[query].reshape( [size, size, size, 3]) return LUT_inverse RGB = [0.18, 0.18, 0.18] LUT = colour.LUT3D() LUT.table = colour.cctf_encoding(LUT.table) RGB_a = LUT.apply(RGB) LUT_inverse = invert(LUT) RGB_i = LUT_inverse.apply(RGB_a) print(RGB) print(RGB_a) print(RGB_i)