# 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
9
10 Bc = np.ones((3,3),dtype = bool) # Connectivity-8
12
13
14 #Spatial filterd
16
17 #Mean filter
18 mean_filter = convolve(img.astype(float),mask,mode = 'wrap')/49
20
21 #Median filter
22 median_filt = median_filter(img.astype(float),footprint = mask,mode = 'wrap')
```

# 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
5 adshow(img, "Image corrupted by noise")
6
7 # Write your solution here
8 Bc = np.ones((3,3),dtype = bool)
9 img_max = img.max()
10 img_neg = img_max - img
12
13 a = 50 #Area-open parameter
14 mxt = iamxt.MaxTreeAlpha(img_neg,Bc)
15 print "Nb of nodes before:", mxt.node_array.shape[1]
16 mxt.areaOpen(a)
17 print "Nb of nodes after:", mxt.node_array.shape[1]
18 area_open = mxt.getImage()
```Nb of nodes before: 26610