generateCCGraph

MainPage

Description: This method draws the morphological tree displaying the connected components in the places of the tree nodes. It only works for 2D images.

Constructor signature: cc_graph = MorphTreeAlpha.generateCCGraph(s = (100,100), parent_scale = True, LR = False)

Input:
  • s, tuple of ints. This tuple defines the size in pixels of the nodes.
  • parent_scale, bool. Flag indicating if the node should be drawn using the parent node scale as a reference.
  • LR, bool. Flag indicating if the tree will be drawn from left to right, or top-down.

Output:

  • cc_graph, 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 import cv2
 5 import adpil
 6 
 7 def generateCCGraph(self,s = (100,100), parent_scale = True, LR = False):
 8     n_nodes = self.node_array.shape[1]
 9     G = gvgen.GvGen()
10     parents = self.node_array[0,:]
11     G.styleAppend("node", "fontcolor", "transparent")
12     G.styleAppend("node", "shape", "rectangle")
13     items = {}
14     items[0] = G.newItem(str(0))
15     G.styleApply("node", items[0])
16     for i in xrange(1,n_nodes):
17         items[i] = G.newItem("")
18         path = '/tmp/' + str(i) + '.png'
19 
20         if parent_scale:
21             xpmin,xpmax = self.node_array[6,parents[i]],self.node_array[7,parents[i]]
22             ypmin,ypmax = self.node_array[9,parents[i]],self.node_array[10,parents[i]]
23         else:
24             xpmin,xpmax = self.node_array[6,i],self.node_array[7,i]
25             ypmin,ypmax = self.node_array[9,i],self.node_array[10,i]
26 
27         node_image = (self.recConnectedComponent(i)[xpmin:xpmax+1,ypmin:ypmax+1]).astype('uint8')*255
28         node_image = cv2.resize(node_image,(s[1],s[0]))
29 
30         if parent_scale:
31             bool_image = np.zeros(s,dtype = bool)
32             indexes = np.nonzero(node_image)
33             if indexes[0].size:
34                 ymin,ymax = indexes[0].min(),indexes[0].max()
35                 xmin,xmax = indexes[1].min(),indexes[1].max()
36                 bool_image[ymin:ymax+1,xmin] = 1
37                 bool_image[ymin:ymax+1,xmax] = 1
38                 bool_image[ymin,xmin:xmax+1] = 1
39                 bool_image[ymax,xmin:xmax+1] = 1
40                 #node_image = ia870.iagshow(node_image,bool_image)
41                 node_image = np.array([node_image,node_image,node_image])
42                 node_image[0][bool_image] = 255
43                 node_image[1][bool_image] = 0
44                 node_image[2][bool_image] = 0
45 
46 
47         adpil.adwrite(path,node_image)
48         G.propertyAppend(items[i],"image", path)
49         G.styleApply("node", items[i])
50         pindex = parents[i]
51         if pindex != i:
52             G.newLink(items[i], items[pindex])
53 
54     fd = StringIO.StringIO()
55     G.dot(fd)
56     dottext = fd.getvalue()
57     if LR:
58         dottext = dottext.replace("TB","LR")
59         dottext = dottext.replace("{","{rankdir=RL")
60     return dottext

Example

 1 from iamxt.morph_tree_alpha import MorphTreeAlpha
 2 import numpy as np
 3 
 4 img = adreadgray(find_attachment_file('iamxt/morph_tree_generate_cc_graph/synthetic01.png'))
 5 adshow(img, "Sample image")
 6 Bc = np.ones((3,3), dtype = bool)
 7 
 8 morph_tree = MorphTreeAlpha(img,Bc)
 9 cc_graph = morph_tree.generateCCGraph(parent_scale = True)
10 mmgraphviz(cc_graph, title='Morph-tree, parent_scale = True')
11 
12 cc_graph2 = morph_tree.generateCCGraph(parent_scale = False)
13 mmgraphviz(cc_graph2, title='Morph-tree, parent_scale = False')

Sample image

/media/_xsb/iamxt/morph_tree_generate_cc_graph/GRVIZ75761_001.png

Morph-tree, parent_scale = True

/media/_xsb/iamxt/morph_tree_generate_cc_graph/GRVIZ75761_002.png

Morph-tree, parent_scale = False