generateGraph

MainPage

Description: This method generates the graphviz code to draw the morphological tree.

Constructor signature: graphviz_tree = MorphTreeAlpha.generateGraph(keep = [],nodes_attr = [], LR = False)

Input:
  • keep, 1d-array, bool. This boolean array indicates the nodes of the morphological tree that will be preserved and the nodes that will be removed.
  • nodes_attr, 1d-array, int32. This array contains the attribute values of each node of the max-tree. If no attribute is given, the tree will display the area attribute.
  • LR, bool. Flag indicating if the tree will be drawn from left to right, or top-down.

Output:

  • graphviz_tree, str. String containing the graphviz file that will be used to draw the tree.

Python Implementation

 1 import numpy as np
 2 import gvgen
 3 import StringIO
 4 
 5 
 6 def generateGraph(self, keep = [],nodes_attr = [], LR = False):
 7     n_nodes = self.node_array.shape[1]
 8     h = self.node_array[2,:]
 9     if keep == []:
10         keep = np.ones(n_nodes, dtype = bool)
11     if nodes_attr == []:
12         nodes_attr = self.node_array[3,:]   # Default attribute is area
13 
14     G = gvgen.GvGen()
15     G.styleAppend("remove", "style", "dashed")
16 
17     G.styleAppend("keep", "color","red")
18     G.styleAppend("keep", "style","filled")
19     items = {}
20 
21 
22     for i in xrange(n_nodes):
23         if keep[i]:
24             items[i] = G.newItem('%d: %d [%.2f]' %(i,h[i],nodes_attr[i]))
25             #items[i] = G.newItem('%d: %d' %(i,h[i]))
26             G.styleApply("keep", items[i])
27         else:
28             items[i] = G.newItem('%d: %d [%.2f]' %(i,h[i],nodes_attr[i]))
29             #items[i] = G.newItem('%d: %d' %(i,h[i]))
30             G.styleApply("remove", items[i])
31         pindex = self.node_array[0,i]
32         if pindex != i:
33             G.newLink(items[i], items[pindex])
34     fd = StringIO.StringIO()
35     G.dot(fd)
36     dottext = fd.getvalue()
37 
38     if LR:
39         dottext = dottext.replace("TB","RL")
40         dottext = dottext.replace("{","{rankdir=LR")
41 
42     return dottext

Simple 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 
22 g= morph_tree.generateGraph()
23 mmgraphviz(g, title='Morph-tree node:level[area]')
/media/_xsb/iamxt/morph_tree_generate_graph/GRVIZ91895_001.png

Morph-tree node:level[area]