示例#1
0
def export_figure(idx_row, df_slices_info, path_out):
    """ load image, segmentation and csv with centres
    1) draw figure with image, segmentation and csv
    2) draw expety annotation
    3) expert figure

    :param idx_row:
    :param df_slices_info:
    :param path_out:
    """
    _, row = idx_row
    img_name = os.path.splitext(os.path.basename(row['path_image']))[0]

    try:
        if img_name not in df_slices_info.index:
            logging.debug('missing image in annotation - "%s"', img_name)
            return

        img = tl_data.io_imread(row['path_image'])
        segm = tl_data.io_imread(row['path_segm'])
        df = pd.read_csv(os.path.join(row['path_centers']), index_col=0)
        centres = df[['X', 'Y']].values

        fig = figure_draw_img_centre_segm(None, img, centres, segm)

        row_slice = df_slices_info.loc[img_name]
        fig = figure_draw_annot_csv(fig, img, row_slice)

        tl_visu.figure_image_adjustment(fig, img.shape)
        fig.savefig(os.path.join(path_out, img_name + '.png'))
        plt.close(fig)
    except Exception:
        logging.exception('failed for: %s', img_name)
示例#2
0
def export_draw_image_segm(path_fig, img, segm=None, segm_obj=None, centers=None):
    """ draw and export visualisation of image and segmentation

    :param str path_fig: path to the exported figure
    :param ndarray img:
    :param ndarray segm:
    :param ndarray segm_obj:
    :param ndarray centers:
    """
    size = np.array(img.shape[:2][::-1], dtype=float)
    fig, ax = plt.subplots(figsize=(size / size.max() * MAX_FIGURE_SIZE))
    ax.imshow(img, alpha=1., cmap=plt.cm.Greys)
    if segm is not None:
        ax.contour(segm)
    if segm_obj is not None:
        ax.imshow(segm_obj, alpha=0.1)
        assert len(np.unique(segm_obj)) < 1e2, \
            'too many labeled objects - %i' % len(np.unique(segm_obj))
        ax.contour(segm_obj, levels=np.unique(segm_obj).tolist(),
                   cmap=plt.cm.jet_r, linewidths=(10, ))
    if centers is not None:
        ax.plot(np.array(centers)[:, 1], np.array(centers)[:, 0], 'o', color='r')

    fig = tl_visu.figure_image_adjustment(fig, img.shape)

    fig.savefig(path_fig)
    plt.close(fig)
示例#3
0
def expert_visual(row, method_name, path_out, max_fig_size=10):
    """ export several visualisation segmentation and annotation

    :param dict row:
    :param str method_name:
    :param str path_out:
    :param int max_fig_size:
    """
    im_name = os.path.splitext(os.path.basename(row['path_image']))[0]
    img, _ = tl_data.load_image_2d(row['path_image'])
    # annot = tl_data.load_image(row['path_annot'])
    egg_segm, _ = tl_data.load_image_2d(row['path_egg-segm'])
    in_segm, _ = tl_data.load_image_2d(row['path_in-segm'])
    centers = tl_data.load_landmarks_csv(row['path_centers'])
    centers = np.array(tl_data.swap_coord_x_y(centers))

    fig_size = max_fig_size * np.array(img.shape[:2]) / float(np.max(
        img.shape))
    fig_name = '%s_%s.jpg' % (im_name, method_name)

    fig, ax = plt.subplots(figsize=fig_size[::-1])
    ax.imshow(img[:, :, 0], cmap=plt.cm.gray)
    ax.imshow(egg_segm, alpha=0.15)
    ax.contour(egg_segm, levels=np.unique(egg_segm), linewidths=(3, ))
    ax.plot(centers[:, 1], centers[:, 0], 'ob')
    tl_visu.figure_image_adjustment(fig, img.shape)
    path_fig = os.path.join(path_out, NAME_DIR_VISUAL_1, fig_name)
    fig.savefig(path_fig, bbox_inches='tight', pad_inches=0)
    plt.close(fig)

    fig, ax = plt.subplots(figsize=fig_size[::-1])
    # ax.imshow(np.max(in_segm) - in_segm, cmap=plt.cm.gray)
    ax.imshow(LUT_COLOR[in_segm], vmin=0., vmax=1., alpha=0.5)
    ax.contour(in_segm, levels=np.unique(in_segm), colors='k')
    ax.imshow(egg_segm, alpha=0.3)
    ax.contour(egg_segm, levels=np.unique(egg_segm), linewidths=(5, ))
    ax.plot(centers[:, 1], centers[:, 0], 'or')
    tl_visu.figure_image_adjustment(fig, img.shape)
    path_fig = os.path.join(path_out, NAME_DIR_VISUAL_2, fig_name)
    fig.savefig(path_fig, bbox_inches='tight', pad_inches=0)
    plt.close(fig)

    fig, ax = plt.subplots(figsize=fig_size[::-1])
    ax.imshow(img[:, :, 0], cmap=plt.cm.gray, alpha=1.)
    ax.contour(in_segm, levels=np.unique(in_segm), colors='w')
    ax.imshow(egg_segm, alpha=0.3)
    ax.contour(egg_segm, levels=np.unique(egg_segm), linewidths=(5, ))
    ax.plot(centers[:, 1], centers[:, 0], 'og')
    tl_visu.figure_image_adjustment(fig, img.shape)
    path_fig = os.path.join(path_out, NAME_DIR_VISUAL_3, fig_name)
    fig.savefig(path_fig, bbox_inches='tight', pad_inches=0)
    plt.close(fig)