generateGraph

MainPage

Description: This method generates the graphviz code to draw the the pixel oriented max-tree.

Constructor signature: graphviz_tree = generateGraph(par,img)

Input:
  • par, 1d-array, int32. Parent array.
  • img, 2d-array, uint8. Image.

Output:

  • graphviz_tree, str. String containing the graphviz code that will be used to draw the pixel oriented max-tree.

Python Implementation

 1 def generateGraph(par,img):
 2    import gvgen
 3 
 4    f = img.ravel()
 5    G = gvgen.GvGen()
 6    G.styleAppend("levroot", "color", "red")
 7    G.styleAppend("levroot", "style", "filled")
 8    G.styleAppend("levroot", "fontcolor", "white")
 9 
10    l = []
11    items = {}
12    for i,p in enumerate(f):
13        if i != par[i]:
14            l.append(i)
15            items[i] = G.newItem('%d: %.2f' % (i,f[i]) )
16            if par[i] == i or f[i] != f[par[i]]:
17                G.styleApply("levroot", items[i])
18 
19    for i in l:
20        p = i
21        while(p != par[p]):
22 
23            if not p in items.keys():
24                items[p] = G.newItem('%d:%.2f' % (p,f[p]))
25                if par[p] == p or f[p] != f[par[p]]:
26                    G.styleApply("levroot", items[p])
27 
28            if not par[p] in items.keys():
29                q = par[p]
30                items[q] = G.newItem('%d:%.2f' % (q,f[q]))
31                if par[q] == q or f[q] != f[par[q]]:
32                    G.styleApply("levroot", items[q])
33 
34            G.newLink(items[p], items[par[p]])
35            p = par[p]
36 
37        G.newLink(items[p], items[par[p]])
38 
39    import StringIO
40    fd = StringIO.StringIO()
41    G.dot(fd)
42    dottext = fd.getvalue()
43    return dottext

Example

 1 from iamxt.max_tree_c_01 import build_max_tree
 2 import numpy as np
 3 import time
 4 from iamxt.generate_graph import generateGraph
 5 
 6 img = np.array([[9,10, 3, 3, 3, 5, 8],
 7                 [2, 0, 4, 0, 0, 0, 7],
 8                 [8, 0, 5, 0, 6, 0, 9],
 9                 [9, 0, 6, 0, 8, 4, 2]],dtype = np.uint8)[:,0:4]
10 
11 print "2D test image:\n", img
12 
13 Bc = np.array([[1,1,1],[1,1,1],[1,1,1]], dtype = bool)
14 print "\n2D structuring Element Bc:\n", Bc.astype(int)
15 
16 parent,S_rev,node_array,node_index = build_max_tree(img,Bc, option = 1)
17 print '\nNode_array:\n', node_array
18 print '\nNode_index:\n', node_index
19 graph = generateGraph(parent,img)
20 mmgraphviz(graph,"Pixel oriented max-tree")
2D test image:
[[ 9 10  3  3]
 [ 2  0  4  0]
 [ 8  0  5  0]
 [ 9  0  6  0]]

2D structuring Element Bc:
[[1 1 1]
 [1 1 1]
 [1 1 1]]

Node_array:
[[ 0  0  1  1  3  4  4  5  2  6]
 [ 1  2  1  1  2  1  1  0  0  0]
 [ 0  2  8  3  4  5  9  6  9 10]
 [16 10  2  7  5  2  2  1  1  1]
 [ 5  4  8  2  6 10  0 14 12  1]
 [24 12  5  6  6  5  0  3  3  0]
 [ 0  0  2  0  0  2  0  3  3  0]
 [ 3  3  3  3  3  3  0  3  3  0]
 [24 12  0 12  7  4  1  2  0  1]
 [ 0  0  0  0  0  2  0  2  0  1]
 [ 3  3  0  3  2  2  1  2  0  1]]

Node_index:
[[6 9 3 3]
 [1 0 4 0]
 [2 0 5 0]
 [8 0 7 0]]
/media/_xsb/iamxt/generate_graph/GRVIZ45841_001.png

Pixel oriented max-tree