# computeNodeCentroid

MainPage

Description: This method computes the cenroids of the max-tree nodes.

Signature: cent = MorphTreeAlpha.computeNodeCentroid()

Input:

Output:
• cent, 2d-array,float. Array containing the centroids of the max-tree nodes

# Python Implementation

``` 1 import numpy as np
2
3 def computeNodeCentroid(self):
4     cent = np.zeros((self.node_array.shape[1],self.node_index.ndim))
5     area = self.node_array[3,:]
6     sumx = self.node_array[5,:]
7     sumy = self.node_array[8,:]
8
9
10     cent[:,0] = 1.0*sumx/area
11     cent[:,1] = 1.0*sumy/area
12
13     if self.node_index.ndim == 3:
14         sumz = self.node_array[11,:]
15         cent[:,2] = 1.0*sumz/area
16     return cent
```

# Example

``` 1 from morph_tree_alpha import MorphTreeAlpha
2 import numpy as np
3
4 np.set_printoptions(precision=4)
5
6 img = np.array([[100, 100, 100, 0,   0,   0,   0,   0],\
7                 [150, 150, 150, 150, 150, 150, 150, 150],\
8                 [150, 160, 190, 150, 200, 200, 229, 150], \
9                 [150, 185, 255, 150, 200, 230, 200, 150],\
10                 [150, 180, 200, 150, 215, 229, 200, 150],\
11                 [150, 150, 150, 150, 150, 150, 150, 150],\
12                 [50,  50,  0,   0,   0,   0,   0,   0]], dtype = np.uint8)
13
14
15 Bc = np.array([[0,1,0],\
16                [1,1,1],\
17                [0,1,0]], dtype = bool)
18
19
20 morph_tree = MorphTreeAlpha(img,Bc)
21
22 cent = morph_tree.computeNodeCentroid()
23
24 print "Centroids:"
25 print cent
```
```Centroids:
[[ 3.      3.5   ]
[ 2.9333  3.2   ]
[ 2.7907  3.3256]
[ 3.      3.5   ]
[ 3.      5.    ]
[ 3.      1.5   ]
[ 3.2     1.6   ]
[ 3.      1.75  ]
[ 3.      2.    ]
[ 3.5     2.    ]
[ 3.      2.    ]
[ 3.6667  4.6667]
[ 2.      6.    ]
[ 3.5     5.    ]
[ 3.      5.    ]]
```