示例#1
0
def show_means_compared(name_frame, name_scene, id_exbl, N=1000):
    """
    Plots the visualisation of impact of the second stage of the algorithm on 
    spectra, Fig.9
    parameters:
        name_frame: name of the source image (inductive scenario)
        name_scene: name of the output image
        id_exbl: id of the reference spectrum
        N: no. vectors in the 2nd stage as int or percentage string e.g. '33p'
    """

    data_frame, anno_frame = load_ds(name_frame, normalise=False)
    data_scene, anno_scene = load_ds(name_scene, normalise=False)
    blood_exbl = load_exbl(id_exbl)

    blood_frame = np.mean(data_frame[anno_frame == 1], axis=0)
    blood_scene = np.mean(data_scene[anno_scene == 1], axis=0)

    assert len(blood_exbl) == len(blood_frame)
    assert len(blood_exbl) == len(blood_scene)

    markers = ['s', 'v', '<', '>', '1', '.', 'p']

    n_blood = np.count_nonzero(anno_scene == 1)
    N_v = decode_N(N, n_blood)

    wav = get_wavelengths()

    X_data = data_scene.reshape(-1, data_scene.shape[2]).copy()
    mf = TwoStageMatchedFilter()

    mf.fit(blood_exbl, X_data, N=N_v, N_supression=0)

    plt.rcParams.update({'font.size': 14})
    plt.plot(wav,
             blood_scene,
             label="Image mean",
             marker=markers[0],
             markevery=0.3)
    plt.plot(wav,
             blood_exbl,
             label="Library",
             marker=markers[1],
             markevery=0.3)
    plt.plot(wav,
             mf.mf_2.mu_t,
             label='Second stage',
             marker=markers[2],
             markevery=0.3)

    plt.ylabel("Reflectance")
    plt.xlabel("Wavelenghts")

    plt.legend()
    plt.tight_layout()
    plt.show()
    plt.close()
示例#2
0
def show_days(absorbance=True):
    """
    plots blood spectra sorted by days (Fig.2)

    Parameters:
    ---------------------
    absorbance: transform reflectance into (pseudo)absorbance i.e. log(1/R)
    """
    wav = get_wavelengths()

    days = ["Day 1(~1h)", "Day 1(~5h)", "Day 2", "Day 7"]
    frames = ["F(1)", "F(1s)", "F(2)", "F(7)"]
    plt.rcParams.update({'font.size': 14})
    fig, ax = plt.subplots()
    markers = ['s', 'v', '<', '>', '1', '.', 'p']

    for i_d, d in enumerate(days):
        data, anno = load_ds(frames[i_d], normalise=False)
        s = np.mean(data[anno == 1], axis=0)
        if absorbance:
            s[s == 0] += 0.0001
            s = np.log10(1.0 / s)
        plt.plot(wav,
                 s,
                 label="{}".format(d),
                 marker=markers[i_d],
                 markevery=10)
    plt.legend()
    y0, y1 = ax.get_ylim()
    plt.xlim(400, 1000)
    if absorbance:
        plt.ylim(0.2, y1)

    plt.plot([542, 542], [0, y1],
             lw=0.5,
             linestyle='--',
             alpha=0.7,
             color='black')
    plt.annotate('542', xy=(542, y1), xytext=(525, y1), fontsize=10)
    plt.plot([577, 577], [0, y1],
             lw=0.5,
             linestyle='--',
             alpha=0.7,
             color='black')
    plt.annotate('577', xy=(577, y1), xytext=(560, y1), fontsize=10)

    if absorbance:
        plt.ylabel("Log(1/R)")
    else:
        plt.ylabel("Reflectance")
    plt.xlabel("Wavelenghts")

    plt.tight_layout()
    plt.show()
    plt.close()
示例#3
0
def show_classes(name='F(1)', absorbance=False):
    """
    plots spectra of classes in the image (Fig.1)
    
    Parameters:
    ---------------------
    name: image name
    absorbance: transform reflectance into (pseudo)absorbance i.e. log(1/R)
    """
    data, anno = load_ds(name, normalise=False)
    wav = get_wavelengths()
    plt.rcParams.update({'font.size': 14})

    plt.rcParams.update({'font.size': 12})
    labels = [
        'blood', 'ketchup', 'artificial blood', 'beetroot juice',
        'poster paint', 'tomato concentrate', 'acrylic paint'
    ]
    markers = ['s', 'v', '<', '>', '1', '.', 'p']
    cmap = plt.get_cmap("Set1")
    colors = [cmap(i / 7) for i in range(8)]

    colors[-4] = cmap(1.0)
    for c_label in range(1, 8):
        if c_label in anno:
            al = 1.0 if c_label == 1 else 0.7
            pattern = data[anno == c_label]
            s = np.median(pattern, axis=0)
            if absorbance:
                s[s == 0] += 0.0001
                s = np.log10(1.0 / s)
            plt.plot(wav,
                     s,
                     label="{} ({})".format(labels[c_label - 1], c_label),
                     color=colors[c_label - 1],
                     alpha=al,
                     marker=markers[c_label - 1],
                     markevery=10)
    plt.legend()

    if absorbance:
        plt.ylabel("Log(1/R)")
    else:
        plt.ylabel("Reflectance")
    plt.xlabel("Wavelenghts")

    plt.tight_layout(pad=0)
    plt.show()

    plt.close()
示例#4
0
def save_gt(data, anno_in, code):
    """
    saves rgb with class annotation
    fig.6-8

    Parameters:
    ---------------------
    data: data cube as nparray
    annotation: 2d annotation array
    """

    ax = plt.subplot()
    colors = []

    #listerd colormap
    for i in [0, 6, 1, 2, 3, 4, 5, 10, 7, 8]:
        colors.append(plt.get_cmap('tab20')(i / 20))
    cmap = ListedColormap(colors, name='colors', N=len(colors))

    anno = anno_in.copy()
    anno[anno == 15] = 0

    rgb = hsi2rgb(data, wavelengths=get_wavelengths(), gamma=0.7)
    plt.imshow(rgb, aspect='auto')
    X, y, pos = data2xy(data, anno)
    for u in np.unique(y)[::-1]:
        if u != 0:
            where = y == u
            pw = pos[where]
            r = [t[0] for t in pw]
            c = [t[1] for t in pw]
            sc = plt.scatter(x=c,
                             y=r,
                             marker=',',
                             s=3,
                             lw=1,
                             alpha=0.8,
                             color=cmap(u))
    ax.set_axis_off()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    plt.tight_layout(pad=0)
    plt.savefig('res/res_{}.png'.format(code),
                bbox_inches='tight',
                pad_inches=0)
    plt.close()
示例#5
0
def show_mixtures(absorbance=True):
    """
    plots differences of blood spectra on different backgrounds (Fig.2)
    
    Parameters:
    ---------------------
    absorbance: transform reflectance into (pseudo)absorbance i.e. log(1/R)
    """
    wav = get_wavelengths()
    data, gt = load_ds('E(1)', normalise=False)

    #lower range and material
    backgrounds = [[44, 'metal'], [74, 'plastic'], [147, 'wood'],
                   [192, 'blue'], [271, 'red(t-shirt)'], [332, 'mixed'],
                   [430, 'mixed(green)'], [516, 'red(sweater)']]

    plt.rcParams.update({'font.size': 14})
    where = gt != 1
    gt[where] = 0
    markers = ['s', 'v', '<', '>', '1', '.', 'p', 'x']
    for i_b, b in enumerate(backgrounds):
        gta = gt.copy()
        gta[b[0]:, :] = 0
        if i_b != 0:
            gta[:backgrounds[i_b - 1][0]:, :] = 0
        X = data[gta == 1, :]
        print(b, X.shape)
        s = np.median(X, axis=0)
        if absorbance:
            s[s == 0] += 0.0001
            s = np.log10(1.0 / s)
        plt.plot(wav,
                 s,
                 label="{}".format(b[1]),
                 marker=markers[i_b],
                 markevery=10)
    plt.legend()

    if absorbance:
        plt.ylabel("Log(1/R)")
    else:
        plt.ylabel("Reflectance")
    plt.xlabel("Wavelenghts")
    plt.show()
    plt.close()