예제 #1
0
    def test_count_transitions_segment(self):
        img = self.img[:, :, 0]
        annot = self.annot.astype(int)

        slic = segment_slic_img2d(img, sp_size=15, relative_compact=0.2)
        label_hist = histogram_regions_labels_norm(slic, annot)
        labels = np.argmax(label_hist, axis=1)
        trans = count_label_transitions_connected_segments({'a': slic}, {'a': labels})
        path_csv = os.path.join(PATH_OUTPUT, 'labels_transitions.csv')
        pd.DataFrame(trans).to_csv(path_csv)
        gc_regul = compute_pairwise_cost_from_transitions(trans, 10.)

        np.random.seed(0)
        features = np.tile(labels, (5, 1)).T.astype(float)
        features += np.random.random(features.shape) - 0.5

        gmm = estim_class_model_gmm(features, 4)
        proba = gmm.predict_proba(features)

        segment_graph_cut_general(slic, proba, gc_regul)
예제 #2
0
def pipe_gray3d_slic_features_model_graphcut(
        image,
        nb_classes,
        dict_features,
        spacing=(12, 1, 1),
        sp_size=15,
        sp_regul=0.2,
        gc_regul=0.1,
):
    """ complete pipe-line for segmentation using superpixels, extracting features
    and graphCut segmentation

    :param ndarray image: input RGB image
    :param int sp_size: initial size of a superpixel(meaning edge lenght)
    :param float sp_regul: regularisation in range(0;1) where "0" gives elastic
           and "1" nearly square segments
    :param int nb_classes: number of classes to be segmented(indexing from 0)
    :param tuple(int,int,int) spacing:
    :param float gc_regul: regularisation for GC
    :return list(list(int)): segmentation matrix maping each pixel into a class

    >>> np.random.seed(0)
    >>> image = np.random.random((5, 125, 150)) / 2.
    >>> image[:, :, :75] += 0.5
    >>> segm = pipe_gray3d_slic_features_model_graphcut(image, 2, {'color': ['mean']})
    >>> segm.shape
    (5, 125, 150)
    """
    logging.info('PIPELINE Superpixels-Features-GraphCut')
    slic = segment_slic_img3d_gray(image,
                                   sp_size=sp_size,
                                   relative_compact=sp_regul,
                                   space=spacing)
    # plt.imshow(segments)
    logging.info('extract segments/superpixels features.')
    # f = features.computeColourMean(image, segments)
    features, _ = compute_selected_features_gray3d(image, slic, dict_features)
    # merge features together
    logging.debug('list of features RAW: %r', features.shape)
    features[np.isnan(features)] = 0

    logging.info('norm all features.')
    features, _ = norm_features(features)
    logging.debug('list of features NORM: %r', features.shape)

    model = estim_class_model(features, nb_classes)
    proba = model.predict_proba(features)
    logging.debug('list of probabilities: %r', proba.shape)

    # resultGraph = graphCut.segment_graph_cut_int_vals(segments, prob, gcReg)
    graph_labels = segment_graph_cut_general(slic, proba, image, features,
                                             gc_regul)

    return graph_labels[slic]
예제 #3
0
def pipe_color2d_slic_features_model_graphcut(image,
                                              nb_classes,
                                              dict_features,
                                              sp_size=30,
                                              sp_regul=0.2,
                                              pca_coef=None,
                                              use_scaler=True,
                                              estim_model='GMM',
                                              gc_regul=1.,
                                              gc_edge_type='model',
                                              debug_visual=None):
    """ complete pipe-line for segmentation using superpixels, extracting features
    and graphCut segmentation

    :param ndarray image: input RGB image
    :param int nb_classes: number of classes to be segmented(indexing from 0)
    :param int sp_size: initial size of a superpixel(meaning edge length)
    :param float sp_regul: regularisation in range(0,1) where 0 gives elastic
                   and 1 nearly square slic
    :param dict dict_features: {clr: list(str)}
    :param float pca_coef: range (0, 1) or None
    :param str estim_model: estimating model
    :param float gc_regul: GC regularisation
    :param str gc_edge_type: graphCut edge type
    :param bool use_scaler: using scaler block in pipeline
    :param debug_visual: dict
    :return list(list(int)): segmentation matrix maping each pixel into a class

    >>> np.random.seed(0)
    >>> image = np.random.random((125, 150, 3)) / 2.
    >>> image[:, :75] += 0.5
    >>> segm, seg_soft = pipe_color2d_slic_features_model_graphcut(
    ...                                         image, 2, {'color': ['mean']})
    >>> segm.shape
    (125, 150)
    >>> seg_soft.shape
    (125, 150, 2)
    """
    logging.info('PIPELINE Superpixels-Features-GMM-GraphCut')
    slic, features = compute_color2d_superpixels_features(image,
                                                          dict_features,
                                                          sp_size=sp_size,
                                                          sp_regul=sp_regul)

    if debug_visual is not None:
        if image.ndim == 2:  # duplicate channels to be like RGB
            image = np.rollaxis(np.tile(image, (3, 1, 1)), 0, 3)
        debug_visual['image'] = image
        debug_visual['slic'] = slic
        debug_visual['slic_mean'] = sk_color.label2rgb(slic, image, kind='avg')

    model = estim_class_model(features, nb_classes, estim_model, pca_coef,
                              use_scaler)
    proba = model.predict_proba(features)
    logging.debug('list of probabilities: %r', proba.shape)

    # gmm = mixture.GaussianMixture(n_components=nb_classes,
    #                               covariance_type='full', max_iter=1)
    # gmm.fit(features, np.argmax(proba, axis=1))
    # proba = gmm.predict_proba(features)

    segm_soft = proba[slic]

    graph_labels = segment_graph_cut_general(slic,
                                             proba,
                                             image,
                                             features,
                                             gc_regul,
                                             gc_edge_type,
                                             debug_visual=debug_visual)
    segm = graph_labels[slic]
    return segm, segm_soft
예제 #4
0
def segment_color2d_slic_features_model_graphcut(image,
                                                 model_pipeline,
                                                 dict_features,
                                                 sp_size=30,
                                                 sp_regul=0.2,
                                                 gc_regul=1.,
                                                 gc_edge_type='model',
                                                 debug_visual=None):
    """ complete pipe-line for segmentation using superpixels, extracting features
    and graphCut segmentation

    :param ndarray image: input RGB image
    :param obj model_pipeline:
    :param int sp_size: initial size of a superpixel(meaning edge lenght)
    :param float sp_regul: regularisation in range(0;1) where "0" gives elastic
                   and "1" nearly square slic
    :param dict(list(str)) dict_features: list of features to be extracted
    :param float gc_regul: GC regularisation
    :param str gc_edge_type: select the GC edge type
    :param debug_visual: dict
    :return list(list(int)): segmentation matrix mapping each pixel into a class

    Examples
    --------
    >>> # UnSupervised:
    >>> import imsegm.descriptors as seg_fts
    >>> np.random.seed(0)
    >>> seg_fts.USE_CYTHON = False
    >>> image = np.random.random((125, 150, 3)) / 2.
    >>> image[:, :75] += 0.5
    >>> model, _ = estim_model_classes_group([image], 2, {'color': ['mean']})
    >>> segm, seg_soft = segment_color2d_slic_features_model_graphcut(
    ...                                     image, model, {'color': ['mean']})
    >>> segm.shape
    (125, 150)
    >>> seg_soft.shape
    (125, 150, 2)

    >>> # Supervised:
    >>> import imsegm.descriptors as seg_fts
    >>> np.random.seed(0)
    >>> seg_fts.USE_CYTHON = False
    >>> image = np.random.random((125, 150, 3)) / 2.
    >>> image[:, 75:] += 0.5
    >>> annot = np.zeros(image.shape[:2], dtype=int)
    >>> annot[:, 75:] = 1
    >>> clf, _, _, _ = train_classif_color2d_slic_features([image], [annot],
    ...                                                    {'color': ['mean']})
    >>> segm, seg_soft = segment_color2d_slic_features_model_graphcut(
    ...                                         image, clf, {'color': ['mean']})
    >>> segm.shape
    (125, 150)
    >>> seg_soft.shape
    (125, 150, 2)
    """
    logging.info('PIPELINE Superpixels-Features-Model-GraphCut')
    slic, features = compute_color2d_superpixels_features(image,
                                                          dict_features,
                                                          sp_size=sp_size,
                                                          sp_regul=sp_regul)

    if debug_visual is not None:
        if image.ndim == 2:  # duplicate channels to be like RGB
            image = np.rollaxis(np.tile(image, (3, 1, 1)), 0, 3)
        debug_visual['image'] = image
        debug_visual['slic'] = slic
        debug_visual['slic_mean'] = sk_color.label2rgb(slic, image, kind='avg')

    proba = model_pipeline.predict_proba(features)
    logging.debug('list of probabilities: %r', proba.shape)

    # gmm = mixture.GaussianMixture(n_components=proba.shape[1],
    #                               covariance_type='full', max_iter=1)
    # gmm.fit(features, np.argmax(proba, axis=1))
    # proba = gmm.predict_proba(features)

    segm_soft = proba[slic]

    graph_labels = segment_graph_cut_general(slic,
                                             proba,
                                             image,
                                             features,
                                             gc_regul,
                                             gc_edge_type,
                                             debug_visual=debug_visual)
    # relabel according classif classes
    if hasattr(model_pipeline, 'classes_'):
        graph_labels = model_pipeline.classes_[graph_labels]
    segm = graph_labels[slic]
    return segm, segm_soft
def segment_image(imgs_idx_path, params, classif, path_out, path_visu=None,
                  show_debug_imgs=SHOW_DEBUG_IMAGES):
    """ perform image segmentation on input image with given paramters
    and trained classifier, and save results

    :param (int, str) imgs_idx_path:
    :param {str: ...} params: segmentation parameters
    :param obj classif: trained classifier
    :param str path_out: path for output
    :param str path_visu: the existing patch means export also visualisation
    :return (str, ndarray, ndarray):
    """
    idx, path_img = parse_imgs_idx_path(imgs_idx_path)
    logging.debug('segmenting image: "%s"', path_img)
    idx_name = get_idx_name(idx, path_img)
    img = load_image(path_img, params['img_type'])
    slic = seg_spx.segment_slic_img2d(img, sp_size=params['slic_size'],
                                            rltv_compact=params['slic_regul'])
    img = seg_pipe.convert_img_color_space(img, params.get('clr_space', 'rgb'))
    features, _ = seg_fts.compute_selected_features_img2d(img, slic,
                                                          params['features'])
    labels = classif.predict(features)
    segm = labels[slic]
    path_img = os.path.join(path_out, idx_name + '.png')
    logging.debug('export segmentation: %s', path_img)
    img_seg = Image.fromarray(segm.astype(np.uint8))
    img_seg.convert('L').save(path_img)
    # io.imsave(path_img, segm)

    # plt.imsave(os.path.join(path_out, idx_name + '_rgb.png'), seg_pipe)
    if path_visu is not None and os.path.isdir(path_visu):
        export_draw_image_segm_contour(img, segm, path_visu, idx_name)

    try:  # in case some classiefier do not support predict_proba
        proba = classif.predict_proba(features)
        segm_soft = proba[slic]
        path_npz = os.path.join(path_out, idx_name + '.npz')
        np.savez_compressed(path_npz, segm_soft)
    except Exception:
        logging.warning('classif: %s not support predict_proba(.)',
                        repr(classif))
        proba = None
        segm_soft = None

    # if probabilities was not estimated of GC regul. is zero
    if proba is not None and params['gc_regul'] > 0:
        gc_regul = params['gc_regul']
        if params['gc_use_trans']:
            label_penalty = seg_gc.compute_pairwise_cost_from_transitions(
                                                params['label_transitions'])
            gc_regul = (gc_regul * label_penalty)
        labels_gc = seg_gc.segment_graph_cut_general(slic, proba, img, features,
                                     gc_regul, edge_type=params['gc_edge_type'])
        # labels_gc = seg_gc.segment_graph_cut_simple(slic, proba, gc_regul)
        segm_gc = labels_gc[slic]
        # relabel according classif classes
        segm_gc = classif.classes_[segm_gc]

        path_img = os.path.join(path_out, idx_name + '_gc.png')
        logging.debug('export segmentation: %s', path_img)
        img_seg_gc = Image.fromarray(segm_gc.astype(np.uint8))
        img_seg_gc.convert('L').save(path_img)
        # io.imsave(path_img, segm_gc)

        if path_visu is not None and os.path.isdir(path_visu):
            export_draw_image_segm_contour(img, segm_gc, path_visu,
                                           idx_name, '_gc')

            if show_debug_imgs:
                labels_map = np.argmax(proba, axis=1)
                plt.imsave(os.path.join(path_visu, idx_name + '_map.png'),
                           labels_map[slic])
                if not segm_soft is None:
                    for lb in range(segm_soft.shape[2]):
                        uc_name = idx_name + '_gc_unary-lb%i.png' % lb
                        plt.imsave(os.path.join(path_visu, uc_name),
                                   segm_soft[:, :, lb], vmin=0., vmax=1.,
                                   cmap=plt.cm.Greens)
    else:
        segm_gc = np.zeros(segm.shape)
    # gc.collect(), time.sleep(1)
    return idx_name, segm, segm_gc
예제 #6
0
def pipe_color2d_slic_features_gmm_graphcut(image,
                                            nb_classes=3,
                                            clr_space='rgb',
                                            sp_size=30,
                                            sp_regul=0.2,
                                            gc_regul=1.,
                                            dict_features=FTS_SET_SIMPLE,
                                            proba_type='GMM',
                                            gc_edge_type='model_lT',
                                            pca_coef=None,
                                            dict_debug_imgs=None):
    """ complete pipe-line for segmentation using superpixels, extracting features
    and graphCut segmentation

    :param float gc_regul: GC regularisation
    :param image: input RGB image
    :param int sp_size: initial size of a superpixel(meaning edge lenght)
    :param float sp_regul: regularisation in range(0;1) where "0" gives elastic
                   and "1" nearly square slic
    :param int nb_classes: number of classes to be segmented(indexing from 0)
    :param dict_features: {clr: [str], ...}
    :param str clr_space: use color space
    :param float gc_regul: GC regularisation
    :param str gc_edge_type: graphCut edge type
    :param float pca_coef: range (0, 1) or None
    :param dict_debug_imgs: {str: ...}
    :return [[int]]: segmentation matrix maping each pixel into a class

    >>> np.random.seed(0)
    >>> image = np.random.random((125, 150, 3)) / 2.
    >>> image[:, :75] += 0.5
    >>> segm = pipe_color2d_slic_features_gmm_graphcut(image, nb_classes=2)
    >>> segm.shape
    (125, 150)
    """
    logging.info('PIPELINE Superpixels-Features-GMM-GraphCut')
    slic, features = compute_color2d_superpixels_features(
        image, clr_space, sp_size, sp_regul, dict_features)

    if dict_debug_imgs is not None:
        if image.ndim == 2:  # duplicate channels to be like RGB
            image = np.rollaxis(np.tile(image, (3, 1, 1)), 0, 3)
        dict_debug_imgs['image'] = image
        dict_debug_imgs['slic'] = slic
        dict_debug_imgs['slic_mean'] = sk_color.label2rgb(slic,
                                                          image,
                                                          kind='avg')

    if pca_coef is not None:
        pca = decomposition.PCA(pca_coef)
        features = pca.fit_transform(features)

    model = seg_gc.estim_class_model(features, nb_classes, proba_type)
    proba = model.predict_proba(features)
    logging.debug('list of probabilities: %s', repr(proba.shape))

    gmm = mixture.GaussianMixture(n_components=nb_classes,
                                  covariance_type='full',
                                  max_iter=1)
    gmm.fit(features, np.argmax(proba, axis=1))
    proba = gmm.predict_proba(features)

    graph_labels = seg_gc.segment_graph_cut_general(
        slic,
        proba,
        image,
        features,
        gc_regul,
        gc_edge_type,
        dict_debug_imgs=dict_debug_imgs)
    segm = graph_labels[slic]
    return segm
예제 #7
0
def segment_color2d_slic_features_classif_graphcut(
        image,
        classif,
        clr_space='rgb',
        sp_size=30,
        sp_regul=0.2,
        gc_regul=1.,
        dict_features=FTS_SET_SIMPLE,
        gc_edge_type='model',
        dict_debug_imgs=None):
    """ take trained classifier and apply it on new images

    :param ndarray image: input image
    :param classif: trained classifier
    :param str clr_space: chose the color space
    :param int sp_size: initial size of a superpixel(meaning edge lenght)
    :param float sp_regul: regularisation in range(0;1) where "0" gives elastic
           and "1" nearly square segments
    :param {str: [str]} dict_features: list of features to be extracted
    :param gc_regul: regularisation for GC
    :param str gc_edge_type: select the GC edge type
    :param dict_debug_imgs:
    :return:

    >>> np.random.seed(0)
    >>> seg_fts.USE_CYTHON = False
    >>> image = np.random.random((125, 150, 3)) / 2.
    >>> image[:, 75:] += 0.5
    >>> annot = np.zeros(image.shape[:2], dtype=int)
    >>> annot[:, 75:] = 1
    >>> clf, _, _, _ = train_classif_color2d_slic_features([image], [annot])
    >>> segm = segment_color2d_slic_features_classif_graphcut(image, clf)
    >>> segm.shape
    (125, 150)
    """
    logging.info('SEGMENTATION Superpixels-Features-Classifier-GraphCut')
    slic, features = compute_color2d_superpixels_features(image,
                                                          clr_space,
                                                          sp_size,
                                                          sp_regul,
                                                          dict_features,
                                                          fts_norm=False)

    proba = classif.predict_proba(features)

    if dict_debug_imgs is not None:
        if image.ndim == 2:  # duplicate channels to be like RGB
            image = np.rollaxis(np.tile(image, (3, 1, 1)), 0, 3)
        dict_debug_imgs['image'] = image
        dict_debug_imgs['slic'] = slic
        dict_debug_imgs['slic_mean'] = sk_color.label2rgb(slic,
                                                          image,
                                                          kind='avg')

    graph_labels = seg_gc.segment_graph_cut_general(
        slic,
        proba,
        image,
        features,
        gc_regul,
        gc_edge_type,
        dict_debug_imgs=dict_debug_imgs)
    segm = graph_labels[slic]
    # relabel according classif classes
    segm = classif.classes_[segm]
    return segm
예제 #8
0
def segment_color2d_slic_features_model_graphcut(image,
                                                 scaler,
                                                 pca,
                                                 model,
                                                 clr_space='rgb',
                                                 sp_size=30,
                                                 sp_regul=0.2,
                                                 gc_regul=1.,
                                                 dict_features=FTS_SET_SIMPLE,
                                                 gc_edge_type='model',
                                                 dict_debug_imgs=None):
    """ complete pipe-line for segmentation using superpixels, extracting features
    and graphCut segmentation

    :param ndarry img: input RGB image
    :param str clr_space: chose the color space
    :param int sp_size: initial size of a superpixel(meaning edge lenght)
    :param float sp_regul: regularisation in range(0;1) where "0" gives elastic
                   and "1" nearly square slic
    :param {str: [str]} dict_features: list of features to be extracted
    :param float gc_regul: GC regularisation
    :param str gc_edge_type: select the GC edge type
    :param float pca_coef: range (0, 1) or None
    :param dict_debug_imgs: {str: ...}
    :return [[int]]: segmentation matrix mapping each pixel into a class

    >>> np.random.seed(0)
    >>> image = np.random.random((125, 150, 3)) / 2.
    >>> image[:, :75] += 0.5
    >>> sc, pca, model = estim_model_classes_group([image], nb_classes=2)
    >>> segm = segment_color2d_slic_features_model_graphcut(image, sc, pca, model)
    >>> segm.shape
    (125, 150)
    """
    logging.info('PIPELINE Superpixels-Features-Model-GraphCut')
    slic, features = compute_color2d_superpixels_features(image,
                                                          clr_space,
                                                          sp_size,
                                                          sp_regul,
                                                          dict_features,
                                                          fts_norm=False)

    if dict_debug_imgs is not None:
        if image.ndim == 2:  # duplicate channels to be like RGB
            image = np.rollaxis(np.tile(image, (3, 1, 1)), 0, 3)
        dict_debug_imgs['image'] = image
        dict_debug_imgs['slic'] = slic
        dict_debug_imgs['slic_mean'] = sk_color.label2rgb(slic,
                                                          image,
                                                          kind='avg')

    features = scaler.transform(features)
    if pca is not None:
        features = pca.fit_transform(features)

    proba = model.predict_proba(features)
    logging.debug('list of probabilities: %s', repr(proba.shape))

    gmm = mixture.GaussianMixture(n_components=proba.shape[1],
                                  covariance_type='full',
                                  max_iter=1)
    gmm.fit(features, np.argmax(proba, axis=1))
    proba = gmm.predict_proba(features)

    graph_labels = seg_gc.segment_graph_cut_general(
        slic,
        proba,
        image,
        features,
        gc_regul,
        gc_edge_type,
        dict_debug_imgs=dict_debug_imgs)
    segm = graph_labels[slic]
    return segm