# extinctionFilter2

MainPage

Description: This method implements the extinction filter on the second max-tree.

Signature: MaxTreeAlpha2.extinctionFilter2(ext,n)

Input:
• ext, 1d-array float. Extinction values array.
• n, int. Number of maxima to be preserved.

Output: - to_keep,**1-darray bool**. Boolean array of the nodes to be preserved in the original max-tree.

# C++ Aux Function

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

# Python Implementation

``` 1 import numpy as np
2
3 def extinctionFilter2(self,ext,n):
4     """
5     This method implements the extinction filter on the second max-tree.
6     It returns  a boolean array of the nodes to be preserved in the ori-
7     ginal max-tree.
8     """
9
10     indexes = np.where(ext!=0)[0]
11     ext = ext[indexes]
12     leaves = (np.argsort(ext)[::-1][:n])
13     leaves = indexes[leaves].astype(np.int32)
14     to_keep = np.zeros((self.node_array2.shape[1],),dtype = np.int32)
15     self.extinction_filter2_aux(leaves,self.node_array2[0,:].astype(np.int32),to_keep)
16     to_keep = to_keep[self.node_index2]
17     to_keep = to_keep > 0
```

# 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
11 #print "2D test image:\n", img
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
17 mxt = MaxTreeAlpha2(img,Bc)
18 dx = mxt.node_array[7,:] - mxt.node_array[6,:] + 1
19 dy = mxt.node_array[10,:] - mxt.node_array[9,:] + 1
20 # Aspect ratio
21 temp = np.concatenate((dx.reshape(1,-1),dy.reshape(1,-1)), axis = 0)
22 den = temp.max(axis = 0)
23 num = temp.min(axis = 0)
24 attr = (1.0*num/den)
25 g= mxt.generateGraph(nodes_attr = attr)
26 mmgraphviz(g, title='Max-tree node:level[ar]')
27
28
29
30 mxt.build2ndMaxTree(attr)
31 graph = generateGraph(mxt.node_array2[0,:],mxt.node_array2[2,:])
32 mmgraphviz(graph,"Pixel oriented 2nd max-tree")
33 ext = mxt.computeExtinctionValues2(mxt.computeHeightFloat())
34 print mxt.computeHeightFloat()
35 print "ext:"
36 print ext
37 print mxt.extinctionFilter2(ext,1)
38 print mxt.node_index2
```
```2D structuring Element Bc:
[[1 1 1]
[1 1 1]
[1 1 1]]
[3 4 7 8]
[  3.34883721e-01   6.46258503e-02   2.17525499e-01   1.00000000e-20
1.00000000e-20   9.34115980e-02   3.07343433e-02   1.00000000e-20
1.00000000e-20]
ext:
[ 0.          0.          0.          1.          1.          0.          0.
1.06462585  1.2175255 ]
[ True False  True False False  True  True  True False]
[0 1 8 3 7 2 5 6 4]
```