# CT Filtering

In this example, we compare classical spatial filters, like mean and median filters, to the area-open filter implemented on the max-tree.

``` 1 import numpy as np
2 import iamxt
3 from scipy.ndimage.filters import convolve, median_filter
4 import time
5 import ia636
6
8
9 Bc = np.ones((3,3),dtype = bool) # Connectivity-8
11
12
13 #Spatial filterd
15
16 #Mean filter
17 mean_filter = convolve(img.astype(float),mask,mode = 'wrap')/49
19
20 #Median filter
21 median_filt = median_filter(img.astype(float),footprint = mask,mode = 'wrap')
23
24 #Area-open on the max-tree
25 a = 50 #Area-open parameter
26 mxt = iamxt.MaxTreeAlpha(img,Bc)
27 mxt.areaOpen(a)
28 area_open = mxt.getImage()
```

# Bounding-box Filter

In this example, we filter the max-tree based on the bounding-box dimensions and rectangularity ratio.

``` 1 Wmin = 7  # 2 minimum bounding box
2 Hmin = 13 #11
3 Wmax = 17 # maximum bounding box
4 Hmax = 25
5 id = 10
6
9
10 img_neg = img.max() - img
11 Bc = np.zeros((3,3), dtype = bool)
12 Bc[1,:] = True
13 Bc[:,1] = True
14
15 mxt = iamxt.MaxTreeAlpha(img_neg,Bc)
16
17
18 dx = mxt.node_array[7,:] - mxt.node_array[6,:]
19 dy = mxt.node_array[10,:] - mxt.node_array[9,:]
20 RR = mxt.computeRR()
21 nodes = (dx>Hmin) & (dx<Hmax) & (dy > Wmin) & (dy < Wmax) & (RR > 0.45)
22 mxt.contractDR(nodes)
23
```

# Do it yourself

Try to filter the image corrupted by noise below using a max-tree filter. Tip: the noise is characterized by black spots in the image, so it may be more suitable to use the min-tree.

```1 import numpy as np
2 import iamxt
3