MorphTreeAlpha

MainPage

Class definition

  1 from se2off import se2off
  2 from max_tree_c_01 import build_max_tree
  3 from max_tree_vmax import vmax
  4 from morph_tree_get_image import getImage
  5 from morph_tree_clone import clone
  6 from morph_tree_rec_connected_component import recConnectedComponent
  7 from morph_tree_compact import compact
  8 from morph_tree_area_open import areaOpen
  9 from morph_tree_bbox import bbox
 10 from morph_tree_compute_rr import computeRR
 11 from morph_tree_generate_cc_path_graph import generateCCPathGraph
 12 from morph_tree_generate_cc_graph import generateCCGraph
 13 from morph_tree_generate_graph import generateGraph
 14 from morph_tree_get_chilldren import getChildren, get_children_aux_c, \
 15                                            get_children_aux_py
 16 from morph_tree_get_ancestors import  getAncestors, get_ancestors_aux_py,\
 17                                             get_ancestors_aux_c
 18 from morph_tree_get_descendants import getDescendants, get_descendants_aux_py,\
 19                                              get_descendants_aux_c
 20 from morph_tree_get_sub_branches import getSubBranches, get_sub_branches_aux_py,\
 21                                               get_sub_branches_aux_c
 22 from morph_tree_prune import prune, prune_aux_c, prune_aux_py
 23 
 24 from morph_tree_contract_dr import contractDR, contract_dr_aux_py, contract_dr_aux_c, update_nchild_aux_py, \
 25                                    update_nchild_aux_c
 26 
 27 from morph_tree_get_image import get_image_aux_py, get_image_aux_c
 28 
 29 from morph_tree_compact import lut_node_index_py, lut_node_index_c
 30 from morph_tree_get_bif_ancestor import get_bif_ancestor_aux_c, get_bif_ancestor_aux_py, getBifAncestor
 31 from morph_tree_compute_histogram import computeHistogram
 32 from morph_tree_compute_node_gray_average import computeNodeGrayAvg,compute_node_gray_avg_aux_c
 33 from morph_tree_compute_node_gray_std  import computeNodeGrayVar, compute_node_gray_var_aux_c
 34 from morph_tree_compute_eccentricity import computeEccentricity, compute_eccentricity_aux_c
 35 from morph_tree_rec_connected_component import rec_connected_component_2d_c, rec_connected_component_3d_c
 36 from morph_tree_compute_node_centroid import computeNodeCentroid
 37 class MorphTreeAlpha:
 38     """
 39     This class builds the morphological tree corresponding to a 8-bit
 40     grayscale image. The morphological trees available for
 41     construction are 2D and 3D max-trees, and 2D tree of shapes.
 42     **Input:**
 43     img -> uint8 image, may be either 2D or 3D. When working with 1D
 44     signals use a 2D array with the shape 1xW.
 45     Bc -> Boolean array corresponding to the connectivity to be used
 46     during the tree construction. The convention is that coordinates
 47     (0,0) or (0,0,0) are in the center of the array.
 48     option-> string, it may either be 'max_tree' or 'tree_of_shapes'
 49     """
 50 
 51     computeHistogram = computeHistogram
 52     getImage = getImage
 53     clone = clone
 54     recConnectedComponent = recConnectedComponent
 55     computeRR = computeRR
 56     areaOpen = areaOpen
 57     bbox = bbox
 58     compact = compact
 59     generateCCPathGraph = generateCCPathGraph
 60     generateCCGraph = generateCCGraph
 61     generateGraph = generateGraph
 62     getChildren = getChildren
 63     getAncestors = getAncestors
 64     getDescendants = getDescendants
 65     getSubBranches = getSubBranches
 66     prune = prune
 67     contractDR = contractDR
 68     getBifAncestor = getBifAncestor
 69     computeNodeGrayAvg = computeNodeGrayAvg
 70     computeNodeGrayVar = computeNodeGrayVar
 71     computeEccentricity = computeEccentricity
 72     computeNodeCentroid = computeNodeCentroid
 73 
 74     def __init__(self,img = None, Bc = None,option = 'max_tree', implementation = 'c'):
 75         if option == 'max_tree':
 76             _,_,self.node_array,self.node_index, = build_max_tree(img,Bc, option = 1)
 77         elif option == 'tree_of_shapes':
 78             print "Error: Option not implemented yet"
 79         else:
 80             print "Error: invalid option"
 81             return
 82         self.Bc = Bc
 83         self.off = se2off(Bc)
 84         self.shape = img.shape
 85         self._children_list = []
 86         self._cum_children_hist = []
 87         self._children_updated = False
 88         self._sb = []
 89         self._cum_sb_hist = []
 90         self._sb_updated = False
 91         self.implementation = implementation
 92         self.ftype = img.dtype
 93 
 94 
 95         if self.implementation == 'c':
 96             self.get_children_aux = get_children_aux_c
 97             self.get_ancestors_aux = get_ancestors_aux_c
 98             self.get_descendants_aux = get_descendants_aux_c
 99             self.get_sub_branches_aux = get_sub_branches_aux_c
100             self.prune_aux = prune_aux_c
101             self.contract_dr_aux = contract_dr_aux_c
102             self.update_nchild_aux = update_nchild_aux_c
103             self.get_image_aux   = get_image_aux_c
104             self.lut_node_index_aux = lut_node_index_c
105             self.get_bif_ancestor_aux = get_bif_ancestor_aux_c
106             self.compute_node_gray_avg_aux = compute_node_gray_avg_aux_c
107             self.compute_node_gray_var_aux = compute_node_gray_var_aux_c
108             self.compute_eccentricity_aux = compute_eccentricity_aux_c
109             self.rec_connected_component_2d_aux = rec_connected_component_2d_c
110             self.rec_connected_component_3d_aux = rec_connected_component_3d_c
111 
112         else:
113             self.get_children_aux =  get_children_aux_py
114             self.get_ancestors_aux = get_ancestors_aux_py
115             self.get_descendants_aux = get_descendants_aux_py
116             self.get_sub_branches_aux = get_sub_branches_aux_py
117             self.prune_aux = prune_aux_py
118             self.contract_dr_aux = contract_dr_aux_py
119             self.update_nchild_aux = update_nchild_aux_py
120             self.get_image_aux   = get_image_aux_py
121             self.lut_node_index_aux = lut_node_index_py
122             self.get_bif_ancestor_aux = get_bif_ancestor_aux_py

Example

 1 from morph_tree_alpha import MorphTreeAlpha
 2 import numpy as np
 3 
 4 img = np.array([[100, 100, 100, 0,   0,   0,   0,   0],\
 5                 [150, 150, 150, 150, 150, 150, 150, 150],\
 6                 [150, 160, 190, 150, 200, 200, 229, 150], \
 7                 [150, 185, 255, 150, 200, 230, 200, 150],\
 8                 [150, 180, 200, 150, 215, 229, 200, 150],\
 9                 [150, 150, 150, 150, 150, 150, 150, 150],\
10                 [50,  50,  0,   0,   0,   0,   0,   0]], dtype = np.uint8)
11 
12 
13 Bc = np.array([[0,1,0],\
14                [1,1,1],\
15                [0,1,0]], dtype = bool)
16 
17 
18 morph_tree = MorphTreeAlpha(img,Bc)
19 
20 
21 to_prune = morph_tree.node_array[3,:] < 6
22 
23 
24 g= morph_tree.generateGraph(keep = ~to_prune)
25 mmgraphviz(g, title='Morph-tree and its nodes marked to be pruned')
26 
27 morph_tree.prune(to_prune)
28 g= morph_tree.generateGraph()
29 mmgraphviz(g, title='Morph-tree after the filtering procedure')
/media/_xsb/iamxt/morph_tree_alpha/GRVIZ92027_001.png

Morph-tree and its nodes marked to be pruned

/media/_xsb/iamxt/morph_tree_alpha/GRVIZ92027_002.png

Morph-tree after the filtering procedure