Ejemplo n.º 1
0
def load_image(path_img, img_type=TYPES_LOAD_IMAGE[0]):
    """ load image and annotation according chosen type

    :param str path_img:
    :param str img_type:
    :return ndarray:
    """
    path_img = tl_data.update_path(path_img)
    if not os.path.isfile(path_img):
        raise FileNotFoundError('missing: "%s"' % path_img)
    if img_type == '2d_split':
        img, _ = tl_data.load_img_double_band_split(path_img)
        if img.ndim != 2:
            raise ImageDimensionError('image dims: %r' % img.shape)
        # img = np.rollaxis(np.tile(img, (3, 1, 1)), 0, 3)
        # if img.max() > 1:
        #     img = (img / 255.)
    elif img_type == '2d_rgb':
        img, _ = tl_data.load_image_2d(path_img)
        # if img.max() > 1:
        #     img = (img / 255.)
    elif img_type == '2d_segm':
        img, _ = tl_data.load_image_2d(path_img)
        if img.ndim == 3:
            img = img[:, :, 0]
        if ANNOT_RELABEL_SEQUENCE:
            img, _, _ = segmentation.relabel_sequential(img)
    else:
        logging.error('not supported loading img_type: %s', img_type)
        img = None
    return img
Ejemplo n.º 2
0
def load_image_segm_center(idx_row, path_out=None, dict_relabel=None):
    """ by paths load images and segmentation and weather centers exist,
    load them if the path out is given redraw visualisation of inputs

    :param (int, DF:row) idx_row: tuple of index and row
    :param str path_out: path to output directory
    :param dict dict_relabel: look-up table for relabeling
    :return(str, ndarray, ndarray, [[int, int]]): idx_name, img_rgb, segm, centers
    """
    idx, row_path = idx_row
    for k in ['path_image', 'path_segm', 'path_centers']:
        row_path[k] = tl_data.update_path(row_path[k])
        if not os.path.exists(row_path[k]):
            raise FileNotFoundError('missing %s' % row_path[k])

    idx_name = get_idx_name(idx, row_path['path_image'])
    img_struc, img_gene = tl_data.load_img_double_band_split(
        row_path['path_image'], im_range=None)
    # img_rgb = np.array(Image.open(row_path['path_img']))
    img_rgb = tl_data.merge_image_channels(img_struc, img_gene)
    if np.max(img_rgb) > 1:
        img_rgb = img_rgb / float(np.max(img_rgb))

    seg_ext = os.path.splitext(os.path.basename(row_path['path_segm']))[-1]
    if seg_ext == '.npz':
        with np.load(row_path['path_segm'], allow_pickle=True) as npzfile:
            segm = npzfile[npzfile.files[0]]
        if dict_relabel is not None:
            segm = seg_lbs.merge_probab_labeling_2d(segm, dict_relabel)
    else:
        segm = tl_data.io_imread(row_path['path_segm'])
        if dict_relabel is not None:
            segm = seg_lbs.relabel_by_dict(segm, dict_relabel)

    if row_path['path_centers'] is not None and os.path.isfile(
            row_path['path_centers']):
        ext = os.path.splitext(os.path.basename(row_path['path_centers']))[-1]
        if ext == '.csv':
            centers = tl_data.load_landmarks_csv(row_path['path_centers'])
            centers = tl_data.swap_coord_x_y(centers)
        elif ext == '.png':
            centers = tl_data.io_imread(row_path['path_centers'])
            # relabel loaded segm into relevant one
            centers = np.array(LUT_ANNOT_CENTER_RELABEL)[centers]
        else:
            logging.warning('not supported file format %s', ext)
            centers = None
    else:
        centers = None

    if is_drawing(path_out):
        export_visual_input_image_segm(path_out, idx_name, img_rgb, segm,
                                       centers)

    return idx_name, img_rgb, segm, centers
Ejemplo n.º 3
0
def load_image(path_img, img_type=TYPE_LOAD_IMAGE):
    """ load image from given path according specification

    :param str path_img:
    :param str img_type:
    :return ndarray:
    """
    path_img = os.path.abspath(os.path.expanduser(path_img))
    assert os.path.isfile(path_img), 'missing: "%s"' % path_img
    if img_type == 'segm':
        img = tl_data.io_imread(path_img)
    elif img_type == '2d_struct':
        img, _ = tl_data.load_img_double_band_split(path_img)
        assert img.ndim == 2, 'image can be only single color'
    else:
        logging.error('not supported loading img_type: %s', img_type)
        img = tl_data.io_imread(path_img)
    logging.debug('image shape: %r, value range %f - %f', img.shape, img.min(),
                  img.max())
    return img