class TestGraphCut(unittest.TestCase):

    img = d_spl.load_sample_image(d_spl.IMAGE_DROSOPHILA_OVARY_2D)
    annot = d_spl.load_sample_image(d_spl.ANNOT_DROSOPHILA_OVARY_2D)

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

        slic = seg_spx.segment_slic_img2d(img, sp_size=15, rltv_compact=0.2)
        label_hist = seg_lb.histogram_regions_labels_norm(slic, annot)
        labels = np.argmax(label_hist, axis=1)
        trans = seg_gc.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 = seg_gc.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 = seg_gc.estim_class_model_gmm(features, 4)
        proba = gmm.predict_proba(features)

        seg_gc.segment_graph_cut_general(slic, proba, gc_regul)
Beispiel #2
0
class TestPipelinesClassif(unittest.TestCase):

    img = d_spl.load_sample_image(d_spl.IMAGE_DROSOPHILA_OVARY_2D)[:, :, 0]
    annot = d_spl.load_sample_image(d_spl.ANNOT_DROSOPHILA_OVARY_2D)
    img2 = d_spl.load_sample_image(d_spl.IMAGE_DROSOPHILA_OVARY_2D)[:, :, 0]

    def test_segm_supervised(self):
        img = imresize(self.img, (256, 256))
        annot = imresize(self.annot, (256, 256), interp='nearest')
        img2 = imresize(self.img2, (256, 256))

        path_dir = os.path.join(PATH_OUTPUT, 'test_segm_supervised_gc')
        if not os.path.exists(path_dir):
            os.mkdir(path_dir)

        sp_size = 10
        tp_edge = ['model', 'const']
        list_regul = [0, 1, 10]
        dict_imgs = dict()
        name = 'fig-img%i_regul-%.2f_edge-%s.png'

        classif, _, _, _ = pipelines.train_classif_color2d_slic_features(
            [img], [annot], sp_size, dict_features=FEATURES_TEXTURE)

        _ = pipelines.segment_color2d_slic_features_classif_graphcut(
            img,
            classif,
            sp_size=sp_size,
            gc_regul=0.,
            dict_features=FEATURES_TEXTURE,
            dict_debug_imgs=dict_imgs)
        show_segm_debugs_2d(dict_imgs, path_dir, name % (1, 0, '_debug'))

        for edge in tp_edge:
            dict_imgs = dict()
            for regul in list_regul:
                seg = pipelines.segment_color2d_slic_features_classif_graphcut(
                    img,
                    classif,
                    sp_size=sp_size,
                    gc_regul=regul,
                    gc_edge_type=edge,
                    dict_features=FEATURES_TEXTURE)
                show_segm_results_2d(img, seg, path_dir,
                                     name % (1, regul, edge))

                seg = pipelines.segment_color2d_slic_features_classif_graphcut(
                    img2,
                    classif,
                    sp_size=sp_size,
                    gc_regul=regul,
                    gc_edge_type=edge,
                    dict_features=FEATURES_TEXTURE,
                    dict_debug_imgs=dict_imgs)
                show_segm_results_2d(img2, seg, path_dir,
                                     name % (2, regul, edge))
                show_segm_debugs_2d(dict_imgs, path_dir,
                                    name % (2, regul, edge))
                dict_imgs = None
class TestSuperpixels(unittest.TestCase):

    img = d_spl.load_sample_image(d_spl.IMAGE_LENNA)
    seg2d = d_spl.sample_segment_vertical_2d()
    seg3d = d_spl.sample_segment_vertical_3d()

    def test_segm_connect(self):
        logging.debug(self.seg2d)
        vertices, edges = seg_spx.make_graph_segm_connect2d_conn4(self.seg2d)
        logging.debug('vertices: {} -> edges: {}'.format(vertices, edges))

        logging.debug(self.seg3d)
        vertices, edges = seg_spx.make_graph_segm_connect3d_conn6(self.seg3d)
        logging.debug('vertices: {} -> edges: {}'.format(vertices, edges))

    def test_general(self):
        slic = seg_spx.segment_slic_img2d(self.img,
                                          sp_size=15,
                                          rltv_compact=0.2)

        logging.debug(np.max(slic))

        vertices, edges = seg_spx.make_graph_segm_connect2d_conn4(slic)
        logging.debug(repr(vertices))
        logging.debug(len(edges))
        logging.debug(repr(edges))

        fig, axarr = plt.subplots(ncols=2)
        axarr[0].imshow(self.img)
        axarr[1].imshow(slic, cmap=plt.cm.jet)
        fig.savefig(os.path.join(PATH_OUTPUT, 'test_superpixels.png'))
        if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
            plt.show()
        plt.close(fig)
Beispiel #4
0
    def test_show_image_features_clr2d(self):
        img = d_spl.load_sample_image(d_spl.IMAGE_LENNA)
        img = transform.resize(img, (128, 128))
        slic = seg_spx.segment_slic_img2d(img, sp_size=10,
                                          rltv_compact=0.2)

        features, names = seg_fts.compute_selected_features_color2d(
                                         img, slic, seg_fts.FEATURES_SET_ALL)

        path_dir = os.path.join(PATH_OUTPUT, 'test_image_rgb2d_features')
        if not os.path.exists(path_dir):
            os.mkdir(path_dir)

        for i in range(features.shape[1]):
            fts = features[:, i]
            im_fts = fts[slic]
            p_fig = os.path.join(path_dir, names[i] + '.png')
            plt.imsave(p_fig, im_fts)
            self.assertTrue(os.path.exists(p_fig))
Beispiel #5
0
class TestPipelinesGMM(unittest.TestCase):

    img_obj = d_spl.load_sample_image(d_spl.IMAGE_OBJECTS)
    img_star = d_spl.load_sample_image(d_spl.IMAGE_STAR_2)
    img_islet = d_spl.load_sample_image(d_spl.IMAGE_LANGER_ISLET)
    img_histo = d_spl.load_sample_image(d_spl.IMAGE_HISTOL_FLAGSHIP)
    img_disc = d_spl.load_sample_image(d_spl.IMAGE_DROSOPHILA_DISC)
    img_ovary = d_spl.load_sample_image(d_spl.IMAGE_DROSOPHILA_OVARY_2D)
    img3d = d_spl.get_image_path(d_spl.IMAGE_DROSOPHILA_OVARY_3D)

    def test_segm_gmm_gc_objects(self):
        img = imresize(self.img_obj, (256, 256))
        logging.debug('dimension: {}'.format(img.shape))

        dict_imgs = dict()
        path_dir = os.path.join(PATH_OUTPUT, 'test_segm_gmm_gc_objects')
        if not os.path.isdir(path_dir):
            os.mkdir(path_dir)
        seg = pipelines.pipe_color2d_slic_features_gmm_graphcut(
            img,
            nb_classes=4,
            sp_size=20,
            sp_regul=0.2,
            dict_features={'color': ['mean']},
            gc_regul=1.,
            gc_edge_type='model',
            dict_debug_imgs=dict_imgs)

        show_segm_debugs_2d(dict_imgs, path_dir,
                            'fig_regul-%.2f_edge-%s_debug.png' % (1., 'model'))
        show_segm_results_2d(img, seg, path_dir,
                             'fig_regul-%.2f_edge-%s.png' % (1., 'model'))

    def test_segm_gmm_gc_stars(self):
        img = self.img_star
        logging.debug('dimension: {}'.format(img.shape))
        params = dict(nb_classes=3,
                      sp_regul=0.2,
                      sp_size=25,
                      dict_features={'color': ['mean', 'std']})
        run_segm2d_gmm_gc(img, 'test_segm_gmm_gc_stars', dict_params=params)

    def test_segm_gmm_gc_langer(self):
        img = imresize(self.img_islet, (512, 512))
        params = dict(clr_space='hsv', sp_regul=0.15, sp_size=5)

        run_segm2d_gmm_gc(img,
                          'test_segm_gmm_gc_langer',
                          types_edge=['model_lT'],
                          list_regul=[0, 1],
                          dict_params=params)

    def test_segm_gmm_gc_histo(self):
        img = imresize(self.img_histo, (512, 512))
        params = dict(sp_regul=0.15, sp_size=15, pca_coef=0.98)
        run_segm2d_gmm_gc(img,
                          'test_segm_gmm_gc_histology',
                          types_edge=['model'],
                          list_regul=[0, 1, 5],
                          dict_params=params)

    def test_segm_gmm_gc_disc(self):
        img = imresize(self.img_disc, (512, 512))
        params = dict(sp_regul=0.2, sp_size=15, pca_coef=0.98)
        run_segm2d_gmm_gc(img,
                          'test_segm_gmm_gc_disc',
                          types_edge=['model_l2'],
                          list_regul=[0, 1, 5],
                          dict_params=params)

    def test_segm_gmm_gc_ovary_2d(self):
        img = imresize(self.img_ovary[:, :, 0], (512, 512))
        # img = np.rollaxis(np.tile(img[:, :, 0], (3, 1, 1)), 0, 3)
        params = dict(nb_classes=4,
                      pca_coef=0.95,
                      sp_regul=0.3,
                      sp_size=10,
                      dict_features=seg_fts.FEATURES_SET_TEXTURE_SHORT)
        run_segm2d_gmm_gc(img,
                          'test_segm_gmm_gc_ovary_2d',
                          list_regul=[0, 2, 10],
                          dict_params=params)

    def test_segm_gmm_gc_ovary_3d(self):
        # _ = self.img3d
        # TODO, add extension to 3D
        # seg = pipelines.pipe_gray3d_slic_features_gmm_graphcut(img)
        pass