# hmax

MainPage

Description: This method implements the hmax filter. It removes all components with height smaller than a threshold h.

Constructor signature: MaxTreeAlpha.hmax(h)

Input:
• h, int. Height threshold value. Every node with height < h will be contracted.
• Height, 1d-array, int32. Optional argument containing the height attributes of the max-tree nodes. If it is not provided, the height is computed internally by the method.

Output:

# Python Implementation

``` 1 def hmax(self, h, Height = None):
2     """
3     This method implemnets the hmax filter
4     """
5     h = h + 1
6     if Height == None:
7         child_height = self.computeHeight()
8     else:
9         child_height = Height
10
11     level = self.node_array[2,:]
12     parent = self.node_array[0,:]
13     total_height = level - level[parent] + child_height
14     self.prune(total_height < h)
15     child_height = child_height[total_height >= h]
16     self.node_array[2,child_height < h] -= (h - child_height[child_height < h])
17     return self
```

# Example

``` 1 from max_tree_alpha import MaxTreeAlpha
2 import numpy as np
3 from ia870 import iahmax
4
5 img = np.array([[100, 100, 100, 0,   0,   0,   0,   0],\
6                 [150, 150, 150, 150, 150, 150, 150, 150],\
7                 [150, 160, 190, 150, 200, 200, 229, 150], \
8                 [150, 185, 255, 150, 200, 230, 200, 150],\
9                 [150, 180, 200, 150, 215, 229, 200, 150],\
10                 [150, 150, 150, 150, 150, 150, 150, 150],\
11                 [50,  50,  0,   0,   0,   0,   0,   0]], dtype = np.uint8)
12
13
14 Bc = np.array([[0,1,0],\
15                [1,1,1],\
16                [0,1,0]], dtype = bool)
17
18
19 mxt = MaxTreeAlpha(img,Bc)
20 height = mxt.computeHeight()
21 g= mxt.generateGraph(nodes_attr = height)
22 mmgraphviz(g, title='Max-tree after filtering the nodes with height < 4')
23
24 mxt.hmax(4)
25 g= mxt.generateGraph()
26 mmgraphviz(g, title='Max-tree after filtering the nodes with height < 4')
27
28 a = mxt.getImage()
29 b = iahmax(img,4)
30 print a
31 print b
32
33 print (a==b).all()
34
36 mxt = MaxTreeAlpha(img2,Bc)
37 height = mxt.computeHeight()
38 mxt.hmax(15)
39 c = mxt.getImage()
40 d =  iahmax(img2,15)
41 print (c!=d).sum()
42 e =  mmhmax ( img2 ,16)
43 print np.all(e == d)
44 print np.all(e == c)
45 #print (np.abs(c.astype(int) - d.astype(int))).max()
46 print (np.abs(e.astype(int) - d.astype(int))).max()
47 print (np.abs(e.astype(int) - c.astype(int))).max()
```
```[[100 100 100   0   0   0   0   0]
[150 150 150 150 150 150 150 150]
[150 160 190 150 200 200 225 150]
[150 185 251 150 200 226 200 150]
[150 180 200 150 215 226 200 150]
[150 150 150 150 150 150 150 150]
[ 50  50   0   0   0   0   0   0]]
[[100 100 100   0   0   0   0   0]
[150 150 150 150 150 150 150 150]
[150 160 190 150 200 200 225 150]
[150 185 251 150 200 226 200 150]
[150 180 200 150 215 226 200 150]
[150 150 150 150 150 150 150 150]
[ 50  50   0   0   0   0   0   0]]
True
1889
False
False
9
1
```