def test_apriori(self): """ Test apriori segmentation. Make segmentation twice. First is used with gamma=0.2, second is used with gamma=0.9 """ img, seg, seeds = self.make_data(64, 20) apriori = np.zeros([64, 64, 64]) apriori[:20, :20, :20] = 1 segparams1 = {'apriori_gamma': .1} gc = pycut.ImageGraphCut(img, segparams=segparams1) gc.set_seeds(seeds) gc.apriori = apriori gc.run() # import sed3 # ed = sed3.sed3(img, contour=(gc.segmentation==0)) # ed.show() self.assertLess( np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8))), 600) segparams2 = {'apriori_gamma': .9} gc = pycut.ImageGraphCut(img, segparams=segparams2) gc.set_seeds(seeds) gc.apriori = apriori gc.run() self.assertLess( np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - apriori.astype(np.int8))), 600)
def test_ms_seg_compared_with_different_resolution(self): """ Test multiscale segmentation """ img, seg, seeds = self.make_data(64, 20) segparams = { # 'method':'graphcut', 'method': 'multiscale_graphcut', 'use_boundary_penalties': False, 'boundary_dilatation_distance': 2, 'boundary_penalties_weight': 1, 'block_size': 8, 'tile_zoom_constant': 1 } gc = pycut.ImageGraphCut(img, segparams=segparams) gc.set_seeds(seeds) gc.run() # import sed3 # ed = sed3.sed3(gc.segmentation==0, contour=seg) # ed.show() self.assertLess( np.sum( np.abs( (gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8)) ), 600) # different resolution # sz = [128,128,128] sz = [70,70,70] sz = [90,90,90] sz = [100,100,100] sz = [200,200,200] sz1 = 70 sz = [sz1, sz1, sz1] img2 = pycut.zoom_to_shape(img, sz, np.uint8) seg2 = pycut.zoom_to_shape(seg, sz, np.uint8) seeds2 = pycut.zoom_to_shape(seeds, sz, np.int8) segparams['tile_zoom_constant'] = 0.8 gc = pycut.ImageGraphCut(img2, segparams=segparams) gc.set_seeds(seeds2) gc.run() import sed3 ed = sed3.sed3(gc.segmentation==0, contour=seg2) ed.show()
def test_multiscale_gc_seg(self): """ Test multiscale segmentation """ img, seg, seeds = self.make_data(64, 20) segparams = { # 'method':'graphcut', 'method': 'multiscale_graphcut', 'use_boundary_penalties': False, 'boundary_dilatation_distance': 2, 'boundary_penalties_weight': 1, 'block_size': 8, 'tile_zoom_constant': 1 } gc = pycut.ImageGraphCut(img, segparams=segparams) gc.set_seeds(seeds) gc.run() # import sed3 # ed = sed3.sed3(gc.segmentation==0, contour=seg) # ed.show() self.assertLess( np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8))), 600)
def test_segmentation_with_boundary_penalties(self): data_shp = [16, 16, 16] #data = self.generate_data(data_shp, boundary_only=True) data = self.generate_data(data_shp, object_type='wall') seeds = np.zeros(data_shp) # setting background seeds seeds[:, 0, 0] = 1 seeds[6, 8:-5, 2] = 2 #x[4:-4, 6:-2, 1:-6] = -1 segparams = {'pairwiseAlpha': 10, 'use_boundary_penalties': True} igc = pycut.ImageGraphCut(data, segparams=segparams) igc.interactivity() # instead of interacitivity just set seeeds #igc.set_seeds(seeds) #igc.make_gc() # instead of showing just test results # from PyQt4.QtGui import QApplication # app = QApplication(sys.argv) # pyed = seed_editor_qt.QTSeedEditor(igc.segmentation, # modeFun=self.interactivity_loop, # voxelVolume=self.voxel_volume, # seeds=self.seeds, minVal=min_val, maxVal=max_val) # app.exec_() #igc.show_segmentation() import pdb pdb.set_trace() segmentation = igc.segmentation # Testin some pixels for result self.assertTrue(segmentation[0, 0, -1] == 0) self.assertTrue(segmentation[7, 9, 3] == 1) self.assertTrue(np.sum(segmentation) > 10)
def test_external_fv(self): """ test external feature vector """ img, seg, seeds = self.make_data(64, 20) segparams = { # 'method':'graphcut', 'method': 'graphcut', 'use_boundary_penalties': False, 'boundary_dilatation_distance': 2, 'boundary_penalties_weight': 1, 'modelparams': { 'fv_type': "fv_extern", 'fv_extern': fv_function, } } gc = pycut.ImageGraphCut(img, segparams=segparams) gc.set_seeds(seeds) gc.run() # import sed3 # ed = sed3.sed3(gc.segmentation==0, contour=seg) # ed.show() self.assertLess( np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8))), 600)
def test_dicomread_and_graphcut(self): """ Test dicomread module and graphcut module """ try: from pysegbase import pycut except: print("Deprecated of pyseg_base as submodule") import pycut dcmdir = os.path.join(path_to_script, './../sample_data/matlab/examples/sample_data/DICOM/digest_article/') #noqa data3d, metadata = dcmr.dcm_read_from_dir(dcmdir) # print ("Data size: " + str(data3d.nbytes) + ', shape: ' + str(data3d.shape) ) #noqa igc = pycut.ImageGraphCut(data3d, zoom=0.5) seeds = igc.seeds seeds[0, :, 0] = 1 seeds[60:66, 60:66, 5:6] = 2 igc.noninteractivity(seeds) igc.make_gc() segmentation = igc.segmentation self.assertTrue(segmentation[14, 4, 1] == 0) self.assertTrue(segmentation[127, 120, 10] == 1) self.assertTrue(np.sum(segmentation == 1) > 100) self.assertTrue(np.sum(segmentation == 0) > 100)
def test_multiscale_indexes(self): # there must be some data img = np.zeros([32, 32, 32], dtype=np.int16) gc = pycut.ImageGraphCut(img) # mask = np.zeros([1, 4, 4], dtype=np.int16) # mask[0,1:3,2:] = 1 mask = np.zeros([1, 3, 3], dtype=np.int16) mask[0, 1:, 1] = 1 orig_shape = [2, 6, 6] zoom = 2 inds = gc._ImageGraphCut__multiscale_indexes(mask, orig_shape, zoom) expected_result = [[[0, 0, 1, 1, 2, 2], [0, 0, 1, 1, 2, 2], [3, 3, 7, 8, 4, 4], [3, 3, 9, 10, 4, 4], [5, 5, 11, 12, 6, 6], [5, 5, 13, 14, 6, 6]], [[0, 0, 1, 1, 2, 2], [0, 0, 1, 1, 2, 2], [3, 3, 15, 16, 4, 4], [3, 3, 17, 18, 4, 4], [5, 5, 19, 20, 6, 6], [5, 5, 21, 22, 6, 6]]] self.assertCountEqual(inds.reshape(-1), np.array(expected_result).reshape(-1))
def test_simple_graph_cut(self): img, seg, seeds = self.make_data(64, 20) segparams = { # 'method':'graphcut', 'method': 'graphcut', 'use_boundary_penalties': False, 'boundary_dilatation_distance': 2, 'boundary_penalties_weight': 1, 'modelparams': { 'type': 'gmmsame', "n_components": 2, # 'fv_type': "fv_extern", # 'fv_extern': fv_function, # 'adaptation': 'original_data', } } gc = pycut.ImageGraphCut(img, segparams=segparams) gc.set_seeds(seeds) gc.run() # import sed3 # ed = sed3.sed3((gc.segmentation==0).astype(np.double), contour=seg) # ed.show() err = np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8))) self.assertLess(err, 600)
def test_segmentation(self): data_shp = [16, 16, 16] data = self.generate_data(data_shp) seeds = np.zeros(data_shp) # setting background seeds seeds[:, 0, 0] = 1 seeds[6, 8:-5, 2] = 2 #x[4:-4, 6:-2, 1:-6] = -1 igc = pycut.ImageGraphCut(data) igc.interactivity() # instead of interacitivity just set seeeds #igc.set_seeds(seeds) #igc.make_gc() # instead of showing just test results # from PyQt4.QtGui import QApplication # app = QApplication(sys.argv) # pyed = seed_editor_qt.QTSeedEditor(igc.segmentation, # modeFun=self.interactivity_loop, # voxelVolume=self.voxel_volume, # seeds=self.seeds, minVal=min_val, maxVal=max_val) # app.exec_() #igc.show_segmentation() segmentation = igc.segmentation # Testin some pixels for result self.assertTrue(segmentation[0, 0, -1] == 0) self.assertTrue(segmentation[7, 9, 3] == 1) self.assertTrue(np.sum(segmentation) > 10)
def test_segmentation(self): img, seg, seeds = self.make_data() gc = pycut.ImageGraphCut(img) gc.set_seeds(seeds) gc.run() # import sed3 # ed = sed3.sed3(gc.segmentation==0, contour=seg) # ed.show() self.assertLess( np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8))), 30)
def test_segmentation(self): img, seg, seeds = self.make_data() gc = pycut.ImageGraphCut(img) gc.set_seeds(seeds) gc.run() self.assertLess( np.sum( np.abs( (gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8)) ) , 30)
def test_boundary_penalty_array(self): """ Test if on edge are smaller values """ data = self.generate_data([16, 16, 16]) * 100 igc = pycut.ImageGraphCut(data) #igc.interactivity() penalty_array = igc.boundary_penalties_array(axis=0) edge_area_pattern = np.mean(penalty_array[3:5, 8:10, 2]) flat_area_pattern = np.mean(penalty_array[1:3, 3:6, -4:-2]) self.assertGreater(flat_area_pattern, edge_area_pattern)
def test_ordered_values_by_indexes(self): """ test of pycut.__ordered_values_by_indexes """ # there must be some data img = np.zeros([32, 32, 32], dtype=np.int16) data = np.array([[0, 1, 1], [0, 2, 2], [0, 2, 2]]) inds = np.array([[0, 1, 2], [3, 4, 4], [5, 4, 4]]) gc = pycut.ImageGraphCut(img) vals = gc._ImageGraphCut__ordered_values_by_indexes(data, inds) expected = np.array([0, 1, 1, 0, 2, 0]) self.assertCountEqual(vals, expected)
def test_ordered_values_by_indexes_with_different_values(self): """ test of pycut.__ordered_values_by_indexes in input data are non-consistent data Function should take maximal value """ # there must be some data img = np.zeros([32, 32, 32], dtype=np.int16) data = np.array([[0, 1, 1], [0, 2, 2], [0, 3, 2]]) inds = np.array([[0, 1, 2], [3, 4, 4], [5, 4, 4]]) gc = pycut.ImageGraphCut(img) vals = gc._ImageGraphCut__ordered_values_by_indexes(data, inds) expected = np.array([0, 1, 1, 0, 3, 0]) self.assertCountEqual(vals, expected)
def autoSeg(self): if self.dcm_3Ddata is None: self.statusBar().showMessage('No DICOM data!') return igc = pycut.ImageGraphCut(self.dcm_3Ddata, voxelsize=self.voxel_size_mm) pyed = QTSeedEditor(self.dcm_3Ddata, seeds=self.segmentation_seeds, modeFun=igc.interactivity_loop, voxelVolume=self.voxel_volume) pyed.exec_() self.segmentation_data = pyed.getContours() self.segmentation_seeds = pyed.getSeeds() self.checkSegData()
def test_gc_box_overfiting(self): data3d, seeds, voxelsize = box_data(noise_sigma=0.5) segparams = { # 'method':'graphcut', 'method': 'graphcut', 'use_boundary_penalties': False, 'boundary_dilatation_distance': 2, 'boundary_penalties_weight': 1, 'modelparams': { 'type': 'gmmsame', "n_components": 3, # 'fv_type': "fv_extern", # 'fv_extern': fv_function, # 'adaptation': 'original_data', } } gc = pycut.ImageGraphCut(data3d, segparams=segparams, debug_images=False) gc.set_seeds(seeds) gc.run()
def test_external_fv_with_save(self): """ test external feature vector with save model in the middle of processing """ img, seg, seeds = self.make_data(64, 20) segparams = { # 'method':'graphcut', 'method': 'graphcut', 'use_boundary_penalties': False, 'boundary_dilatation_distance': 2, 'boundary_penalties_weight': 1, 'modelparams': { 'type': 'gmmsame', 'fv_type': "fv_extern", 'fv_extern': fv_function, 'adaptation': 'original_data', } } gc = pycut.ImageGraphCut(img, segparams=segparams) gc.set_seeds(seeds) gc.run() # import sed3 # ed = sed3.sed3((gc.segmentation==0).astype(np.double), contour=seg) # ed.show() err = np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8))) self.assertLess(err, 600) mdl_stored_file = "test_model.p" gc.save(mdl_stored_file) # forget gc = None img, seg, seeds = self.make_data(56, 18) # there is only one change in mdl params segparams['modelparams'] = { 'mdl_stored_file': mdl_stored_file, 'fv_extern': fv_function } gc = pycut.ImageGraphCut(img, segparams=segparams) gc.set_seeds(seeds) gc.run() err = np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8))) self.assertLess(err, 600) # import sed3 # sed3.show_slices(img, contour=gc.segmentation==0, slice_step=6) # if we change the data there should be more error (assertMore) img = (img * 0.2).astype(np.uint8) # segparams['modelparams']['adaptation'] = 'original_data' # print(np.max(img)) # print(np.min(img)) gc = pycut.ImageGraphCut(img, segparams=segparams) gc.set_seeds(seeds) gc.run() m0 = gc.mdl.mdl[1] m1 = gc.mdl.mdl[2] logger.debug("model parameters") # import sed3 # ed = sed3.sed3((gc.segmentation==0).astype(np.double), contour=seg) # ed.show() err = np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8))) self.assertGreater(err, 600) # self.assertGreater( # np.sum( # np.abs( # (gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8)) # ), # 600) os.remove(mdl_stored_file)