build2ndMaxTree

MainPage

C++ Implementation

OK
OK [C/C++ extension is up-to-date]

Python Implementation

 1 import numpy as np
 2 
 3 def build2ndMaxTree(self,attr):
 4     par = np.empty(attr.size, dtype = np.int32)
 5     par[:] = -1
 6     zpar = np.empty(attr.size, dtype = np.int32)
 7     S = np.argsort(attr).astype(np.int32)#[::-1]
 8     #S = S.copy()
 9     if not self._children_updated:
10         self.getChildren(0)
11     self.union_find2_aux(self.node_array[0,:], self._cum_children_hist,self._children_list,attr, zpar,par, S)
12     self.canonicalize2_aux(attr,par,S)
13     self.node_index2 = np.empty(attr.shape, dtype = np.int32)
14     self.node_index2[:] = -1
15 
16     self.node_array2 = self.computeNodeArray_2nd_tree_aux(par,attr,S,self.node_index2)
17     self.node_array2 = self.node_array2.T.copy()
18     return self

Example

 1 from iamxt.max_tree_alpha2 import MaxTreeAlpha2
 2 import numpy as np
 3 from iamxt.generate_graph import generateGraph
 4 
 5 img = np.array([[9,10, 3, 3, 3, 5, 8],
 6                 [2, 0, 4, 0, 0, 0, 7],
 7                 [8, 0, 5, 0, 6, 0, 9],
 8                 [9, 0, 6, 0, 8, 4, 2]],dtype = np.uint8)
 9 
10 img = adreadgray(find_attachment_file("shapes.png"))
11 img = adreadgray(find_attachment_file('iamxt/morph_tree_generate_cc_graph/synthetic01.png'))
12 Bc = np.array([[1,1,1],[1,1,1],[1,1,1]], dtype = bool)
13 #print "\n2D structuring Element Bc:\n", Bc.astype(int)
14 adshow(img)
15 
16 mxt = MaxTreeAlpha2(img,Bc)
17 dx = mxt.node_array[7,:] - mxt.node_array[6,:] + 1
18 dy = mxt.node_array[10,:] - mxt.node_array[9,:] + 1
19 # Aspect ratio
20 temp = np.concatenate((dx.reshape(1,-1),dy.reshape(1,-1)), axis = 0)
21 den = temp.max(axis = 0)
22 num = temp.min(axis = 0)
23 attr = (1.0*num/den)
24 
25 
26 #g= mxt.generateGraph(nodes_attr = attr)
27 #mmgraphviz(g, title='Max-tree node:level[var]')
28 graph = generateGraph(mxt.node_array[0,:], attr)
29 mmgraphviz(graph,"Max-tree")
30 cc_graph2 = mxt.generateCCGraph(parent_scale = True)
31 mmgraphviz(cc_graph2, title='Max-tree')
32 imgs = []
33 for i in xrange(mxt.node_array.shape[1]):
34     imgs.append(mxt.recConnectedComponent(i))
35 
36 mxt.build2ndMaxTree(attr)
37 print mxt.node_array2[0,:]
38 print "node_index2:"
39 print mxt.node_index2
40 LUT = np.array([0,1,5,3,8,6,7,4,2],dtype = np.int32)
41 graph = generateGraph(mxt.node_array2[0,:], mxt.node_array2[2,:])
42 mmgraphviz(graph,"2nd max-tree")
43 #mxt.node_array[1:,:] = mxt.node_array[1:,LUT] #mxt.node_index2
44 #mxt.node_array[0,:] = mxt.node_array2[0,:].astype(int)
45 #mxt.node_array[2,:] = (mxt.node_array2[2,:]*200).astype(np.int32)
46 
47 #mxt.node_index = mxt.node_index2[mxt.node_index].astype(np.int32)
48 
49 for ii in LUT:
50     adshow(imgs[int(ii)],str(ii))
51 
52 cc_graph2 = mxt.generateCCGraph()
53 mmgraphviz(cc_graph2, title='Morph-tree, parent_scale = False')
54 #print "node array new"
55 #print mxt.node_array
[ 0.  0.  0.  0.  2.  2.  5.  1.  6.]
node_index2:
[0 1 8 3 7 2 5 6 4]

/media/_xsb/iamxt/max_tree2_c_01/GRVIZ99407_001.png

Max-tree

/media/_xsb/iamxt/max_tree2_c_01/GRVIZ99407_002.png

Max-tree

/media/_xsb/iamxt/max_tree2_c_01/GRVIZ99407_003.png

2nd max-tree

0

1

5

3

8

6

7

4

2

/media/_xsb/iamxt/max_tree2_c_01/GRVIZ99407_004.png

Morph-tree, parent_scale = False