# contractDR

MainPage

Description: This method implements the direct rule for node contraction.

Constructor signature: MorphTreeAlpha.contractDR(to_keep)

Input:
• to_keep, 1d-array bool. Array indicating which nodes should be preserved.

Output

# C++ Aux Function

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

# Python Implementation

``` 1 def contractDR(self, to_keep):
2     """
3     Direct rule for contracting any max-tree nodes marked as False in 'to_keep'.
4     This is a generic node removal procedure. Note that a node in the max-tree
5     can represent many level components.
6     """
7
8     to_keep[0] = True # The root can never be removed
9     N = self.node_array.shape[1]
10     lut = np.arange(N, dtype = np.int32)
11     self.contract_dr_aux(to_keep.astype(np.int32),lut,self.node_array[0,:])
12     #self.node_index = lut[self.node_index]
13     self.compact(~to_keep,lut)
14     self.node_array[1,:] = 0
15     self.update_nchild_aux(self.node_array[0,:],self.node_array[1,:])
16     return self
```

# Example

``` 1 from morph_tree_alpha import MorphTreeAlpha
2 import numpy as np
3
4 img = np.array([[100, 100, 100, 0,   0,   0,   0,   0],\
5                 [150, 150, 150, 150, 150, 150, 150, 150],\
6                 [150, 160, 190, 150, 200, 200, 229, 150], \
7                 [150, 185, 255, 150, 200, 230, 200, 150],\
8                 [150, 180, 200, 150, 215, 229, 200, 150],\
9                 [150, 150, 150, 150, 150, 150, 150, 150],\
10                 [50,  50,  0,   0,   0,   0,   0,   0]], dtype = np.uint8)
11
12
13 Bc = np.array([[0,1,0],\
14                [1,1,1],\
15                [0,1,0]], dtype = bool)
16
17
18 morph_tree = MorphTreeAlpha(img,Bc)
19 print morph_tree.node_array
20 to_keep = np.ones(morph_tree.node_array.shape[1], dtype = bool)
21 to_keep[[2,6,8,11,12]] = False
22
23 g= morph_tree.generateGraph(keep = to_keep)
24 mmgraphviz(g, title='Morph-tree and its nodes marked to be removed')
25
26 morph_tree.contractDR(to_keep)
27 print morph_tree.node_array
28 g= morph_tree.generateGraph()
29 mmgraphviz(g, title='Morph-tree after the filtering procedure')
```
```[[  0   0   1   2   3   3   5   6   7   8   9   4   4  11  13]
[  1   1   1   2   2   1   1   1   1   1   0   1   0   1   0]
[  0  50 100 150 200 160 180 185 190 200 255 215 229 229 230]
[ 56  45  43  40   9   6   5   4   3   2   1   3   1   2   1]
[  3  48   0   8  20  17  33  25  18  34  26  36  22  37  29]
[168 132 120 120  27  18  16  12   9   7   3  11   2   7   3]
[  0   0   0   1   2   2   2   2   2   3   3   3   2   3   3]
[  6   6   5   5   4   4   4   4   4   4   3   4   2   4   3]
[196 144 143 140  45   9   8   7   6   4   2  14   6  10   5]
[  0   0   0   0   4   1   1   1   2   2   2   4   6   5   5]
[  7   7   7   7   6   2   2   2   2   2   2   5   6   5   5]]
[[  0   0   1   2   2   4   5   6   3   8]
[  1   1   2   1   1   1   1   0   1   0]
[  0  50 150 200 160 185 200 255 229 230]
[ 56  45  40   9   6   4   2   1   2   1]
[  3  48   8  20  17  25  34  26  37  29]
[168 132 120  27  18  12   7   3   7   3]
[  0   0   1   2   2   2   3   3   3   3]
[  6   6   5   4   4   4   4   3   4   3]
[196 144 140  45   9   7   4   2  10   5]
[  0   0   0   4   1   1   2   2   5   5]
[  7   7   7   6   2   2   2   2   5   5]]
```