def InstSegm(extent, boundary, t_ext=0.4, t_bound=0.2): """ INPUTS: extent : extent prediction boundary : boundary prediction t_ext : threshold for extent t_bound : threshold for boundary OUTPUT: instances """ # Threshold extent mask ext_binary = np.uint8(extent >= t_ext) # Artificially create strong boundaries for # pixels with non-field labels input_hws = np.copy(boundary) input_hws[ext_binary == 0] = 1 # Create the directed graph size = input_hws.shape[:2] graph = hg.get_8_adjacency_graph(size) edge_weights = hg.weight_graph(graph, input_hws, hg.WeightFunction.mean) tree, altitudes = hg.watershed_hierarchy_by_dynamics(graph, edge_weights) # Get individual fields # by cutting the graph using altitude instances = hg.labelisation_horizontal_cut_from_threshold( tree, altitudes, threshold=t_bound).astype(np.float) instances[ext_binary == 0] = np.nan return instances
def test_labelisation_horizontal_cut(self): tree = hg.Tree(np.asarray((5, 5, 6, 6, 6, 7, 7, 7))) altitudes = np.asarray((0, 0, 0, 0, 0, 0.5, 0, 0.7), dtype=np.double) ref_t0 = np.asarray((1, 2, 3, 3, 3), dtype=np.int32) ref_t1 = np.asarray((1, 1, 2, 2, 2), dtype=np.int32) ref_t2 = np.asarray((1, 1, 1, 1, 1), dtype=np.int32) output_t0 = hg.labelisation_horizontal_cut_from_threshold(tree, altitudes, 0) output_t1 = hg.labelisation_horizontal_cut_from_threshold(tree, altitudes, 0.5) output_t2 = hg.labelisation_horizontal_cut_from_threshold(tree, altitudes, 0.7) self.assertTrue(hg.is_in_bijection(ref_t0, output_t0)) self.assertTrue(hg.is_in_bijection(ref_t1, output_t1)) self.assertTrue(hg.is_in_bijection(ref_t2, output_t2))
def test_hierarchy_to_optimal_MumfordShah_energy_cut_hierarchy(self): # Test strategy: # 1) start from a random hierarchy # 2) construct the corresponding optimal Mumford-Shah energy cut hierarchy # 3) verify that the horizontal cuts of the new hierarchy corresponds to the # optimal energy cuts of the first hierarchy obtained from the explicit MF energy # and the function labelisation_optimal_cut_from_energy shape = (10, 10) g = hg.get_4_adjacency_graph(shape) np.random.seed(2) vertex_weights = np.random.rand(*shape) edge_weights = hg.weight_graph(g, vertex_weights, hg.WeightFunction.L1) tree1, altitudes1 = hg.watershed_hierarchy_by_area(g, edge_weights) tree, altitudes = hg.hierarchy_to_optimal_MumfordShah_energy_cut_hierarchy( tree1, vertex_weights, approximation_piecewise_linear_function=999999) for a in altitudes: if a != 0: res = False cut1 = hg.labelisation_horizontal_cut_from_threshold( tree, altitudes, a) # du to numerical issues, and especially as we test critical scale level lambda, # we test several very close scale levels for margin in [-1e-8, 0, 1e-8]: mfs_energy = hg.attribute_piecewise_constant_Mumford_Shah_energy( tree1, vertex_weights, a + margin) cut2 = hg.labelisation_optimal_cut_from_energy( tree1, mfs_energy) res = res or hg.is_in_bijection(cut1, cut2) self.assertTrue(res)
def get_cut_thr(tree, altitudes, thr=0.5): labels = hg.labelisation_horizontal_cut_from_threshold( tree, 1 - altitudes, 1 - thr) return labels