Exemplo n.º 1
0
def Lab_to_sRGB(L, a, b):
    XYZ = colour.Lab_to_XYZ(
        np.array([L, a, b]),
        illuminant=colour.ILLUMINANTS.get(
            'CIE 1931 2 Degree Standard Observer').get('D65'))
    r, g, b = colour.sRGB_COLOURSPACE.XYZ_to_RGB_matrix.dot(XYZ)
    return r, g, b
Exemplo n.º 2
0
def set_convertor(name, ill='D65'):
    """ Binds the conversion functions LCH2RGB() and RGB2LCH() to the choosen colour package
    """
    global LCH2RGB, RGB2LCH, convertor, illuminant
    if name not in ['custom', 'colorspacious', 'colourscience']:
        print("Unknown conversion module")
        return
    convertor = name
    illuminant = ill
    if name == 'custom':
        LCH2RGB = lambda L, C, H: XYZ2RGB(Lab2XYZ(LCH2Lab((L, C, H))))
        RGB2LCH = lambda R, G, B: Lab2LCH(XYZ2Lab(RGB2XYZ((R, G, B))))
    if name == 'colorspacious':
        from colorspacious import cspace_convert
        func_LCH2RGB = lambda L, C, H: cspace_convert([L, C, H], {
            "name": "CIELCh",
            "XYZ100_w": ill
        }, "sRGB1")
        func_RGB2LCH = lambda R, G, B: cspace_convert([R, G, B], "sRGB1", {
            "name": "CIELCh",
            "XYZ100_w": ill
        })
    if name == 'colourscience':
        import colour as cs
        cs_ill = cs.ILLUMINANTS['CIE 1931 2 Degree Standard Observer'][ill]
        func_LCH2RGB = lambda L, C, H: cs.XYZ_to_sRGB(
            cs.Lab_to_XYZ(cs.LCHab_to_Lab([L, C, H]), illuminant=cs_ill))
        func_RGB2LCH = lambda R, G, B: cs.Lab_to_LCHab(
            cs.XYZ_to_Lab(cs.sRGB_to_XYZ([R, G, B]), illuminant=cs_ill))
    if name == 'colorspacious' or name == 'colourscience':

        def LCH2RGB(L, C, H):
            if hasattr(L, '__iter__'):
                RGB = np.array(list(map(func_LCH2RGB, L, C, H)))
                R = RGB[:, 0]
                G = RGB[:, 1]
                B = RGB[:, 2]
            else:
                R, G, B = func_LCH2RGB(L, C, H)
            return R, G, B

        def RGB2LCH(R, G, B):
            if hasattr(R, '__iter__'):
                LCH = np.array(list(map(func_RGB2LCH, R, G, B)))
                L = LCH[:, 0]
                C = LCH[:, 1]
                H = LCH[:, 2]
            else:
                L, C, H = func_RGB2LCH(R, G, B)
            return L, C, H

    print("convertor = '%s' (illuminant = '%s')" % (name, illuminant))
Exemplo n.º 3
0
Arquivo: adds.py Projeto: jhasanov/ACV
def LAB2RGB(image):
    """
    Convert image from CIE Lab to RGB colour space
    Lab   Scale
    L|    0:100 |
    a| -100:100 |
    b| -100:100 |

    :param image:
    :return:
    """
    image_sRGB = colour.XYZ_to_sRGB(colour.Lab_to_XYZ(image))
    image_RGB = np.round(image_sRGB * 255).astype(np.uint8)
    return image_RGB
Exemplo n.º 4
0
def lab_to_rgb_d65(lab, name='ITU-R BT.2020'):

    illuminant_XYZ = tpg.D65_WHITE
    illuminant_RGB = tpg.D65_WHITE
    chromatic_adaptation_transform = 'CAT02'
    xyz_to_rgb_matrix = tpg.get_xyz_to_rgb_matrix(name)

    # Lab to XYZ
    large_xyz = colour.Lab_to_XYZ(lab, illuminant_XYZ)

    # XYZ to RGB
    rgb = colour.XYZ_to_RGB(large_xyz, illuminant_XYZ, illuminant_RGB,
                            xyz_to_rgb_matrix,
                            chromatic_adaptation_transform)

    return rgb
Exemplo n.º 5
0
print(colour.LCHuv_to_Luv(LCHuv))

print('\n')

message_box(('Converting to "CIE Lab" colourspace from given "CIE XYZ" '
             'tristimulus values:\n'
             '\n\t{0}'.format(XYZ)))
print(colour.XYZ_to_Lab(XYZ))

print('\n')

Lab = (100.00000000, 28.97832184, 30.96902832)
message_box(('Converting to "CIE XYZ" tristimulus values from given "CIE Lab" '
             'colourspace values:\n'
             '\n\t{0}'.format(Lab)))
print(colour.Lab_to_XYZ(Lab))

print('\n')

message_box(('Converting to "CIE LCHab" colourspace from given "CIE Lab" '
             'colourspace values:\n'
             '\n\t{0}'.format(Lab)))
print(colour.Lab_to_LCHab(Lab))

print('\n')

LCHab = (100.00000000, 42.41254357, 46.90195532)
message_box(('Converting to "CIE Lab" colourspace from given "CIE LCHab" '
             'colourspace values:\n'
             '\n\t{0}'.format(LCHab)))
print(colour.LCHab_to_Lab(LCHab))
    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)
Exemplo n.º 8
0
 def time_4k(self):
     colour.Lab_to_XYZ(lab_4k)
Exemplo n.º 9
0
 def time_hd(self):
     colour.Lab_to_XYZ(lab_hd)
Exemplo n.º 10
0
 def time_sd(self):
     colour.Lab_to_XYZ(lab_sd)
Exemplo n.º 11
0
csv_result = './ResultIOS.csv'
with open(csv_result, 'r') as fp:
    n = int(fp.readline())
    for i in range(0, n):
        row = fp.readline().split(',')
        #print(row)
        color_name = row[0].replace('_', ' ')
        c_xyz = [float(x) for x in row[-3:]]
        d_xyz = [float(x) for x in row[-6:-3]]
        j_rgb = [float(x) for x in row[-9:-6]]
        #print(j_rgb)
        j_xyz = colour.sRGB_to_XYZ(j_rgb)
        ppg_lab = ppg_data[color_name]['LAB']
        ppg_xyz = colour.Lab_to_XYZ(
            ppg_lab,
            illuminant=colour.
            ILLUMINANTS['CIE 1931 2 Degree Standard Observer']['D65'])
        ppg_srgb = colour.XYZ_to_sRGB(ppg_xyz)
        for tt in range(0, 3):
            if (ppg_srgb[tt] > 1.0 or ppg_srgb[tt] < 0.0):
                print(color_name)
                break
        #print(ppg_srgb)
        xy = colour.XYZ_to_xy(
            ppg_xyz,
            illuminant=colour.
            ILLUMINANTS['CIE 1931 2 Degree Standard Observer']['D65'])

        x, y = xy
        plt.plot(x, y, 'o-', color='black', markersize=4)