Exemple #1
0
def calc_xy_from_white_to_primary_n_step(name=cs.BT709, step=5, color='green'):
    """
    Examples
    --------
    >>> calc_xy_from_white_to_primary_n_step(name=cs.BT709,
                                             step=5, color='green')
    [[0.31269999999999998, 0.329000000000000],
     [0.30952499999999999, 0.396750000000000],
     [0.30635000000000001, 0.464499999999999],
     [0.30317499999999997, 0.532250000000000],
     [0.29999999999999999, 0.600000000000000]]
    """
    pos1_temp = D65_WHITE
    if color == 'red':
        pos2_temp = tpg.get_primaries(name)[0][0]
    elif color == 'green':
        pos2_temp = tpg.get_primaries(name)[0][1]
    else:
        pos2_temp = tpg.get_primaries(name)[0][2]
    if pos1_temp[1] < pos2_temp[1]:
        pos1 = pos1_temp
        pos2 = pos2_temp
    else:
        pos1 = pos2_temp
        pos2 = pos1_temp
    x, y = def_line(pos1, pos2)
    z = np.linspace(pos1[0], pos2[0], step)

    val = [[w, y.subs({x: w}).evalf()] for w in z]

    return np.array(val)
Exemple #2
0
def _get_primary_secondary_large_y(name):
    """
    Primary, Secondary の Y値を求める。
    色度パターン作成時に、パッチごとにYの値が変わらないように
    後で xy --> xyY 変換時に使用する。

    Parameters
    ----------
    name : string
        target color space name.

    Returns
    -------
    array_like
        large Y value for primary and secondary.

    """

    # RGBMYC の Y値リストを得る
    _, primary = tpg.get_primaries(name)
    _, secondary = tpg.get_secondaries(name)
    data = np.vstack((primary, secondary))
    rgb_to_xyz_mtx = colour.RGB_COLOURSPACES[name].RGB_to_XYZ_matrix
    data = tpg.do_matrix(img=data, mtx=rgb_to_xyz_mtx)

    return data[..., 1]
Exemple #3
0
def get_intersection_primary(out_side_name, in_side_name):
    """
    BT.2020 の Primary と D65 を結ぶ直線と
    BT.709 の Gamut が交差する点を求める
    """

    bt2020_p, _ = tpg.get_primaries(name=out_side_name)
    primary, _ = tpg.get_primaries(name=in_side_name)

    white_point = sympy.Point(tpg.D65_WHITE[0], tpg.D65_WHITE[1])

    bt2020_p_points = [
        sympy.Point(bt2020_p[x][0], bt2020_p[x][1]) for x in range(3)
    ]
    primary_points = [
        sympy.Point(primary[x][0], primary[x][1]) for x in range(4)
    ]

    bt2020_p_lines = [
        sympy.Line(bt2020_p_points[x], white_point) for x in range(3)
    ]

    # よく考えたら、どの線と交差するかは gamut の形で決まるんだった…マニュアルで。
    # ----------------------------------------------------------------------
    primary_lines = [
        sympy.Line(primary_points[2], primary_points[3]),
        sympy.Line(primary_points[1], primary_points[2]),
        sympy.Line(primary_points[1], primary_points[2])
    ]

    # 交点求める。evalf() して式の評価も済ませておく
    # -------------------------------------------
    intersections = [
        sympy.intersection(bt2020_p_lines[x], primary_lines[x])[0].evalf()
        for x in range(3)
    ]

    # 後で扱いやすいように xy の配列に変換しておく
    # -----------------------------------------
    intersections = [[intersections[x].x, intersections[x].y]
                     for x in range(3)]

    return np.array(intersections)
def plot_chromaticity_diagram(gamut, data, title=None):
    xyY = ryr.rgb_to_xyY(data, gamut)
    gamut_xy, _ = tpg.get_primaries(gamut)
    cmf_xy = tpg._get_cmfs_xy()

    rate = 1.0
    ax1 = pu.plot_1_graph(fontsize=20 * rate,
                          figsize=(8 * rate, 9 * rate),
                          graph_title=title,
                          graph_title_size=16,
                          xlabel=None,
                          ylabel=None,
                          axis_label_size=None,
                          legend_size=18 * rate,
                          xlim=(0, 0.8),
                          ylim=(0, 0.9),
                          xtick=[x * 0.1 for x in range(9)],
                          ytick=[x * 0.1 for x in range(10)],
                          xtick_size=17 * rate,
                          ytick_size=17 * rate,
                          linewidth=4 * rate,
                          minor_xtick_num=2,
                          minor_ytick_num=2)
    color = data.reshape((data.shape[0] * data.shape[1], data.shape[2]))
    ax1.plot(cmf_xy[..., 0], cmf_xy[..., 1], '-k', lw=3.5 * rate, label=None)
    ax1.plot((cmf_xy[-1, 0], cmf_xy[0, 0]), (cmf_xy[-1, 1], cmf_xy[0, 1]),
             '-k',
             lw=2.5 * rate,
             label=None)
    ax1.patch.set_facecolor("#F2F2F2")
    ax1.plot(gamut_xy[..., 0],
             gamut_xy[..., 1],
             c=K_BAR_COLOR,
             label="BT.709",
             lw=3 * rate)
    ax1.scatter(xyY[..., 0],
                xyY[..., 1],
                s=2 * rate,
                marker='o',
                c=color,
                edgecolors=None,
                linewidth=1 * rate,
                zorder=100)
    ax1.scatter(np.array([0.3127]),
                np.array([0.3290]),
                s=150 * rate,
                marker='x',
                c="#000000",
                edgecolors=None,
                linewidth=2.5 * rate,
                zorder=101,
                label="D65")
    plt.legend(loc='upper right')
    file_name = './figures/xy_chromaticity_{}.png'.format(title)
    plt.savefig(file_name, bbox_inches='tight')
Exemple #5
0
def plot_ap0_ap1():
    ap0, _ = tpg.get_primaries(cs.ACES_AP0)
    ap1, _ = tpg.get_primaries(cs.ACES_AP1)

    ax1 = pu.plot_1_graph(fontsize=20,
                          figsize=(9, 11),
                          graph_title="CIE1931 Chromaticity Diagram",
                          graph_title_size=None,
                          xlabel=None, ylabel=None,
                          axis_label_size=None,
                          legend_size=18,
                          xlim=(-0.1, 0.8),
                          ylim=(-0.1, 1.1),
                          xtick=[x * 0.1 - 0.1 for x in range(10)],
                          ytick=[x * 0.1 - 0.1 for x in range(12)],
                          xtick_size=17,
                          ytick_size=17,
                          linewidth=2,
                          minor_xtick_num=2,
                          minor_ytick_num=2)
    ax1.plot(ap0[..., 0], ap0[..., 1], label="AP0")
    ax1.plot(ap1[..., 0], ap1[..., 1], label="AP1")
    plt.legend(loc='upper right')
    plt.show()
Exemple #6
0
def get_intersection_secondary(out_side_name, in_side_name):
    """
    BT.2020 の Secondary と D65 を結ぶ直線と
    BT.709 の Gamut が交差する点を求める
    """

    secondary, _ = tpg.get_secondaries(name=out_side_name)
    primary, _ = tpg.get_primaries(name=in_side_name)

    white_point = sympy.Point(tpg.D65_WHITE[0], tpg.D65_WHITE[1])

    secondary_points = [
        sympy.Point(secondary[x][0], secondary[x][1]) for x in range(3)
    ]
    primary_points = [
        sympy.Point(primary[x][0], primary[x][1]) for x in range(4)
    ]

    secondary_lines = [
        sympy.Line(secondary_points[x], white_point) for x in range(3)
    ]
    primary_lines = [
        sympy.Line(primary_points[(x + 2) % 3], primary_points[(x + 3) % 3])
        for x in range(3)
    ]

    # 交点求める。evalf() して式の評価も済ませておく
    # -------------------------------------------
    intersections = [
        sympy.intersection(secondary_lines[x], primary_lines[x])[0].evalf()
        for x in range(3)
    ]

    # 後で扱いやすいように xy の配列に変換しておく
    # -----------------------------------------
    intersections = [[intersections[x].x, intersections[x].y]
                     for x in range(3)]

    return np.array(intersections)
Exemple #7
0
def _get_test_scatter_data(sample_num=6):

    color_space_name = TARGET_COLORSPACE
    inter_section_space_name = COMPARATIVE_COLORSPACE

    primaries, primary_rgb = tpg.get_primaries(color_space_name)
    secondaries, secondary_rgb = tpg.get_secondaries(color_space_name)

    primary_intersections\
        = get_intersection_primary(out_side_name=color_space_name,
                                   in_side_name=inter_section_space_name)
    secondary_intersections\
        = get_intersection_secondary(out_side_name=color_space_name,
                                     in_side_name=inter_section_space_name)

    ed_xy = np.vstack((primaries[:3, :], secondaries))
    st_xy = np.vstack((primary_intersections, secondary_intersections))
    patch_xy = [
        _get_interpolated_xy(st_xy[idx], ed_xy[idx], sample_num)
        for idx in range(st_xy.shape[0])
    ]

    patch_xy = np.array(patch_xy)
    specific = _get_primary_secondary_large_y(name=color_space_name)
    specific = specific[:, np.newaxis]
    specific = np.tile(specific, sample_num)
    rgb = tpg.xy_to_rgb(patch_xy,
                        color_space_name,
                        normalize='specific',
                        specific=specific)
    # rgb = tpg.xy_to_rgb(patch_xy, color_space_name,
    #                     normalize='maximum', specific=specific)
    rgb = rgb**(1 / 2.2)
    rgb = rgb.reshape((rgb.shape[0] * rgb.shape[1], rgb.shape[2]))

    # return xy, rgb.reshape((rgb.shape[0] * rgb.shape[1], 3))
    return patch_xy, rgb
Exemple #8
0
def plot_chromaticity_diagram(
        rate=480/755.0*2, xmin=0.0, xmax=0.8, ymin=0.0, ymax=0.9, **kwargs):
    """
    >>> xy = calc_xy_from_white_to_primary_n_step(name=name, step=step,
    >>>                                           color=primary_color)
    >>> linear_rgb = get_normalize_rgb_value_from_small_xy(xy, cs.ACES_AP1)
    >>> rgb = tf.oetf(linear_rgb, oetf_name)
    >>> plot_chromaticity_diagram(rate=480/755.0*2,
    >>>                           xmin=-0.1, xmax=0.8, ymin=-0.1, ymax=1.05,
    >>>                           test_scatter=(xy, rgb),
    >>>                           white_point=D65_WHITE)
    """
    # キーワード引数の初期値設定
    # ------------------------------------
    test_scatter = kwargs.get('test_scatter', None)
    white_point = kwargs.get('white_point', None)

    # プロット用データ準備
    # ---------------------------------
    xy_image = tpg.get_chromaticity_image(
        xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)
    cmf_xy = tpg._get_cmfs_xy()

    bt709_gamut, _ = tpg.get_primaries(name=cs.BT709)
    bt2020_gamut, _ = tpg.get_primaries(name=cs.BT2020)
    dci_p3_gamut, _ = tpg.get_primaries(name=cs.P3_D65)
    ap0_gamut, _ = tpg.get_primaries(name=cs.ACES_AP0)
    ap1_gamut, _ = tpg.get_primaries(name=cs.ACES_AP1)
    xlim = (min(0, xmin), max(0.8, xmax))
    ylim = (min(0, ymin), max(0.9, ymax))

    ax1 = pu.plot_1_graph(fontsize=20 * rate,
                          figsize=((xmax - xmin) * 10 * rate,
                                   (ymax - ymin) * 10 * rate),
                          graph_title="CIE1931 Chromaticity Diagram",
                          graph_title_size=None,
                          xlabel=None, ylabel=None,
                          axis_label_size=None,
                          legend_size=18 * rate,
                          xlim=xlim, ylim=ylim,
                          xtick=[x * 0.1 + xmin for x in
                                 range(int((xlim[1] - xlim[0])/0.1) + 1)],
                          ytick=[x * 0.1 + ymin for x in
                                 range(int((ylim[1] - ylim[0])/0.1) + 1)],
                          xtick_size=17 * rate,
                          ytick_size=17 * rate,
                          linewidth=4 * rate,
                          minor_xtick_num=2,
                          minor_ytick_num=2)
    ax1.plot(cmf_xy[..., 0], cmf_xy[..., 1], '-k', lw=3.5*rate, label=None)
    ax1.plot((cmf_xy[-1, 0], cmf_xy[0, 0]), (cmf_xy[-1, 1], cmf_xy[0, 1]),
             '-k', lw=3.5*rate, label=None)
    ax1.plot(bt709_gamut[:, 0], bt709_gamut[:, 1],
             c=tpg.UNIVERSAL_COLOR_LIST[0], label="BT.709", lw=2.75*rate)
    ax1.plot(bt2020_gamut[:, 0], bt2020_gamut[:, 1],
             c=tpg.UNIVERSAL_COLOR_LIST[1], label="BT.2020", lw=2.75*rate)
    ax1.plot(dci_p3_gamut[:, 0], dci_p3_gamut[:, 1],
             c=tpg.UNIVERSAL_COLOR_LIST[2], label="DCI-P3", lw=2.75*rate)
    ax1.plot(ap1_gamut[:, 0], ap1_gamut[:, 1],
             c=tpg.UNIVERSAL_COLOR_LIST[3], label="ACES AP1", lw=2.75*rate)
    ax1.plot(ap0_gamut[:, 0], ap0_gamut[:, 1],
             c=tpg.UNIVERSAL_COLOR_LIST[4], label="ACES AP0", lw=2.75*rate)
    if test_scatter is not None:
        xy, rgb = test_scatter
        ax1.scatter(xy[..., 0], xy[..., 1], s=300*rate, marker='s', c=rgb,
                    edgecolors='#404040', linewidth=2*rate)
    if white_point is not None:
        ax1.plot(white_point[0], white_point[1], 'kx', label="D65",
                 markersize=10*rate, markeredgewidth=2.0*rate)
    for idx in range(test_scatter[0].shape[0]):
        text = "No.{}".format(idx)
        xy = (test_scatter[0][idx][0]+0.01, test_scatter[0][idx][1])
        xy_text = (xy[0] + 0.35, xy[1] + 0.1)
        ax1.annotate(text, xy=xy, xycoords='data',
                     xytext=xy_text, textcoords='data',
                     ha='left', va='bottom',
                     arrowprops=dict(facecolor='#333333', shrink=0.0))

    ax1.imshow(xy_image, extent=(xmin, xmax, ymin, ymax))
    plt.legend(loc='upper right')
    plt.savefig('temp_fig.png', bbox_inches='tight')
    plt.show()