generateCCPathGraph

MainPage

Description: This method draws the connected components path connecting two nodes of the tree. It only works for trees corresponding to 2D images.

Constructor signature: cc_graph = MorphTreeAlpha.generateCCPathGraph(start, end, s = (100,100), parent_scale = True, composite_nodes = True, LR = False)

Input:
  • start, int, first node of the path.
  • end, int, last node of the path.
  • 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.
  • composite_nodes, bool. Flag indicating if the composite nodes should appear only nlevels times, or only once.
  • 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 
 8 def generateCCPathGraph(self,start, end, s = (100,100), parent_scale = True, \
 9                         composite_nodes = True, LR = False):
10     G = gvgen.GvGen()
11     parents = self.node_array[0,:]
12     h = self.node_array[2,:]
13     nlevels = h - h[parents]
14     G.styleAppend("node", "fontcolor", "transparent")
15     G.styleAppend("node", "shape", "rectangle")
16     items = {}
17     pos = start
18     items[pos] = G.newItem("")
19     j = 1000000
20     while True:
21         path = '/tmp/' + str(pos) + '.png'
22 
23         if parent_scale and pos!= end:
24             xpmin,xpmax = self.node_array[6,parents[pos]],self.node_array[7,parents[pos]]
25             ypmin,ypmax = self.node_array[9,parents[pos]],self.node_array[10,parents[pos]]
26         else:
27             xpmin,xpmax = self.node_array[6,pos],self.node_array[7,pos]
28             ypmin,ypmax = self.node_array[9,pos],self.node_array[10,pos]
29 
30         node_image = (self.recConnectedComponent(pos)[xpmin:xpmax+1,ypmin:ypmax+1]).astype('uint8')*255
31         node_image = cv2.resize(node_image,(s[1],s[0]))
32 
33         if parent_scale and pos!= end:
34             bool_image = np.zeros(s,dtype = bool)
35             indexes = np.nonzero(node_image)
36             if indexes[0].size:
37                 ymin,ymax = indexes[0].min(),indexes[0].max()
38                 xmin,xmax = indexes[1].min(),indexes[1].max()
39                 bool_image[ymin:ymax+1,xmin] = 1
40                 bool_image[ymin:ymax+1,xmax] = 1
41                 bool_image[ymin,xmin:xmax+1] = 1
42                 bool_image[ymax,xmin:xmax+1] = 1
43                 #node_image = ia870.iagshow(node_image,bool_image)
44                 node_image = np.array([node_image,node_image,node_image])
45                 node_image[0][bool_image] = 255
46                 node_image[1][bool_image] = 0
47                 node_image[2][bool_image] = 0
48 
49         adpil.adwrite(path,node_image)
50         #items[pos] = G.newItem("")
51         G.propertyAppend(items[pos],"image", path)
52         G.styleApply("node", items[pos])
53 
54         if nlevels[pos] > 1 and composite_nodes:
55             items[j] = G.newItem("")
56             G.propertyAppend(items[j],"image", path)
57             G.styleApply("node", items[j])
58             G.newLink(items[pos], items[j])
59             j += 1
60             for i in xrange(nlevels[pos] - 2):
61                 items[j] = G.newItem("")
62                 G.propertyAppend(items[j],"image", path)
63                 G.styleApply("node", items[j])
64                 G.newLink(items[j - 1], items[j])
65                 j +=1
66 
67 
68         if pos == end:
69             break
70         pindex = parents[pos]
71         items[pindex] = G.newItem("")
72         if nlevels[pos] > 1 and composite_nodes:
73             G.newLink(items[j-1], items[pindex])
74         else:
75             G.newLink(items[pos], items[pindex])
76         pos = pindex
77 
78     fd = StringIO.StringIO()
79     G.dot(fd)
80     dottext = fd.getvalue()
81     if LR:
82         dottext = dottext.replace("TB","RL")
83         dottext = dottext.replace("{","{rankdir=LR")
84     return dottext

Example

 1 from iamxt import MaxTreeAlpha
 2 import numpy as np
 3 
 4 # Reading a sample image. Any library can be used to do that.
 5 img = adreadgray(find_attachment_file('homeroberto/carotid_demo/carotid_slice.png'))
 6 
 7 # Defining the structuring element
 8 Bc = np.zeros((3,3),dtype = bool)
 9 Bc[1,:] = True
10 Bc[:,1] = True
11 
12 #Building the image max-tree
13 mxt = MaxTreeAlpha(img,Bc)
14 
15 #Displaying the image. Any library can be used to do that.
16 adshow(img, "Original Image (%d shapes, %d maxima)" %(mxt.node_array.shape[1],(mxt.node_array[1,:] == 0).sum()))
17 
18 #Hand-picked leaf
19 rc = 41278
20 
21 # Displaying the path graph
22 cc_graph = mxt.generateCCPathGraph(rc,0, parent_scale = True)
23 mmgraphviz(cc_graph, title='CC Path Signature')

Original Image (43368 shapes, 15571 maxima)

/media/_xsb/iamxt/morph_tree_generate_cc_path_graph/GRVIZ95258_001.png

CC Path Signature