def test_multiple_crop_and_uncrop_nearest_outside(self): """ test combination of multiple crop """ shape = [10, 11, 5] img_in = np.random.random(shape) crinfo1 = [[2, 8], [3, 9], [2, 5]] # crinfo2 = [[2, 5], [1, 5], [1, 2]] img_cropped = imma.crop(img_in, crinfo1) # img_cropped = imma.crop(img_cropped, crinfo2) # crinfo_combined = imma.combinecrinfo(crinfo1, crinfo2) img_uncropped = imma.uncrop(img_cropped, crinfo1, shape, outside_mode="nearest") # import sed3 # ed = sed3.sed3(img_uncropped) # ed.show() self.assertTrue(img_uncropped[4, 4, 3] == img_in[4, 4, 3]) self.assertTrue(img_uncropped[crinfo1[0][0], 5, 3] == img_uncropped[0, 5, 3], msg="pixels under crop") self.assertTrue(img_uncropped[5, crinfo1[1][0], 3] == img_uncropped[5, 0, 3], msg="pixels under crop") self.assertTrue(img_uncropped[7, 3, crinfo1[2][0]] == img_uncropped[7, 3, 0], msg="pixels under crop") self.assertTrue(img_uncropped[crinfo1[0][1] - 1, 5, 3] == img_uncropped[-1, 5, 3], msg="pixels over crop") self.assertTrue(img_uncropped[5, crinfo1[1][1] - 1, 3] == img_uncropped[5, -1, 3], msg="pixels over crop") self.assertTrue(img_uncropped[7, 3, crinfo1[2][1] - 1] == img_uncropped[7, 3, -1], msg="pixels over crop") # self.assertTrue(img_uncropped[crinfo1[0][1], 5 , 3] == img_uncropped[0, 5, 3], msg="pixels over crop") # self.assertTrue(img_uncropped[crinfo1[1][1], 5 , 3] == img_uncropped[1, 5, 3], msg="pixels over crop") # self.assertTrue(img_uncropped[crinfo1[2][1], 5 , 3] == img_uncropped[2, 5, 3], msg="pixels over crop") self.assertEquals(img_in.shape, img_uncropped.shape)
def test_uncrop_with_none_crinfo(self): shape = [10, 10, 5] orig_shape = [15, 13, 7] img_in = np.random.random(shape) img_uncropped = imma.uncrop(img_in, crinfo=None, orig_shape=orig_shape) self.assertTrue(img_uncropped[-1, -1, -1] == 0) self.assertTrue(img_uncropped[4, 4, 3] == img_in[4, 4, 3])
def test_uncrop_with_start_point_crinfo(self): shape = [10, 10, 5] orig_shape = [15, 13, 7] img_in = np.random.random(shape) crinfo = [5, 2, 1] img_uncropped = imma.uncrop(img_in, crinfo=crinfo, orig_shape=orig_shape) self.assertTrue(img_uncropped[-1, -1, -1] == 0) self.assertTrue(img_uncropped[4 + 5, 4 + 2, 3 + 1] == img_in[4 , 4, 3])
def test_crop_and_uncrop(self): shape = [10, 10, 5] img_in = np.random.random(shape) crinfo = [[2, 8], [3, 9], [2, 5]] img_cropped = imma.crop(img_in, crinfo) img_uncropped = imma.uncrop(img_cropped, crinfo, shape) self.assertTrue(img_uncropped[4, 4, 3] == img_in[4, 4, 3])
def test_multiple_crop_and_uncrop(self): """ test combination of multiple crop """ shape = [10, 10, 5] img_in = np.random.random(shape) crinfo1 = [[2, 8], [3, 9], [2, 5]] crinfo2 = [[2, 5], [1, 4], [1, 2]] img_cropped = imma.crop(img_in, crinfo1) img_cropped = imma.crop(img_cropped, crinfo2) crinfo_combined = imma.combinecrinfo(crinfo1, crinfo2) img_uncropped = imma.uncrop(img_cropped, crinfo_combined, shape) self.assertTrue(img_uncropped[4, 4, 3] == img_in[4, 4, 3]) self.assertEquals(img_in.shape, img_uncropped.shape)
def test_msgc_lo2hi_round_data(self): """ Test multiscale segmentation lo2hi round data """ np.random.seed(3) img, seg, seeds = generate_round_data(45, 3, 10, 3) blocksize = 6 segparams = { # 'method':'graphcut', "method": "multiscale_graphcut_lo2hi", # 'method': 'multiscale_graphcut_hi2lo', "use_boundary_penalties": False, "boundary_dilatation_distance": 1, "boundary_penalties_weight": 1, "block_size": blocksize, "tile_zoom_constant": 1, } gc = pycut.ImageGraphCut( img, segparams=segparams, keep_graph_properties=True, # debug_images=True, debug_images=False, ) gc.set_seeds(seeds) gc.run() # import sed3 # ed = sed3.show_slices(img, gc.segmentation*2) # gc.debug_show_model() # gc.debug_show_reconstructed_similarity() tdata1, tdata2 = gc.debug_get_reconstructed_similarity() edseeds = seeds # node_unariesalt, node_neighboor_edges_and_weights, node_neighboor_seeds, node_msindex = gc.interactive_inspect_node() node_msindex = gc.debug_get_node_msindex(edseeds) node_unariesalt, node_neighboor_edges_and_weights, node_neighboor_seeds = gc.debug_inspect_node( node_msindex) nlinks_max = gc.debug_reconstruct_nlinks_max() from imcut import image_manipulation as imma seg_uncr = imma.uncrop(gc.segmentation, None, nlinks_max.shape) # import sed3 # ed = sed3.sed3(nlinks_max, contour=seg_uncr*2) # ed.show() self.assertLess( np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8))), 100, ) self.assertEqual( np.max(nlinks_max), blocksize, "nlink maximal value in reconstructed nlink map. " "It should be size of block if there are at least some" " lowres blocks.", ) self.assertEqual(np.min(nlinks_max), 1, "nlink minimal value in reconstructed nlink map") self.assertGreaterEqual( np.min(tdata1), 0, "tlink minimal value in reconstructed tlink map") self.assertGreaterEqual( np.min(tdata2), 0, "tlink minimal value in reconstructed tlink map") self.assertGreaterEqual( node_neighboor_edges_and_weights.shape[0], 3, "check number of nlink connections", ) self.assertLessEqual( node_neighboor_edges_and_weights.shape[0], 6, "check number of nlink connections", ) self.assertGreaterEqual(np.min(node_unariesalt), 0, "selected node nlink minimum")
def test_msgc_lo2hi_round_data(self): """ Test multiscale segmentation lo2hi round data """ np.random.seed(3) img, seg, seeds = generate_round_data(45, 3, 10, 3) blocksize = 6 segparams = { # 'method':'graphcut', "method": "multiscale_graphcut_lo2hi", # 'method': 'multiscale_graphcut_hi2lo', "use_boundary_penalties": False, "boundary_dilatation_distance": 1, "boundary_penalties_weight": 1, "block_size": blocksize, "tile_zoom_constant": 1, } gc = pycut.ImageGraphCut( img, segparams=segparams, keep_graph_properties=True, # debug_images=True, debug_images=False, ) gc.set_seeds(seeds) gc.run() # import sed3 # ed = sed3.show_slices(img, gc.segmentation*2) # gc.debug_show_model() # gc.debug_show_reconstructed_similarity() tdata1, tdata2 = gc.debug_get_reconstructed_similarity() edseeds = seeds # node_unariesalt, node_neighboor_edges_and_weights, node_neighboor_seeds, node_msindex = gc.interactive_inspect_node() node_msindex = gc.debug_get_node_msindex(edseeds) node_unariesalt, node_neighboor_edges_and_weights, node_neighboor_seeds = gc.debug_inspect_node( node_msindex ) nlinks_max = gc.debug_reconstruct_nlinks_max() from imcut import image_manipulation as imma seg_uncr = imma.uncrop(gc.segmentation, None, nlinks_max.shape) # import sed3 # ed = sed3.sed3(nlinks_max, contour=seg_uncr*2) # ed.show() self.assertLess( np.sum( np.abs((gc.segmentation == 0).astype(np.int8) - seg.astype(np.int8)) ), 100, ) self.assertEqual( np.max(nlinks_max), blocksize, "nlink maximal value in reconstructed nlink map. " "It should be size of block if there are at least some" " lowres blocks.", ) self.assertEqual( np.min(nlinks_max), 1, "nlink minimal value in reconstructed nlink map" ) self.assertGreaterEqual( np.min(tdata1), 0, "tlink minimal value in reconstructed tlink map" ) self.assertGreaterEqual( np.min(tdata2), 0, "tlink minimal value in reconstructed tlink map" ) self.assertGreaterEqual( node_neighboor_edges_and_weights.shape[0], 3, "check number of nlink connections", ) self.assertLessEqual( node_neighboor_edges_and_weights.shape[0], 6, "check number of nlink connections", ) self.assertGreaterEqual( np.min(node_unariesalt), 0, "selected node nlink minimum" )