# Shape and size filtering

In this example, we use size and shape criteria to segment the interior of the carotid artery wall. We use area, bounding-box, eccentricty, rectangularity ratio to obtain the segmentation result.

``` 1 import numpy as np
2 import iamxt
3 import time
4
6
8
9 img_neg = 255-img
11
12 #Filtering thresholds
13 a_min,a_max = 200,300
14 dx_max,dy_max = 22,22
15 rr_max = 0.5
16 ar_min = 0.65
17 ecc_min = 0.3
18 rr_min = 0.55
19
20 Bc = np.zeros((3,3),dtype = bool)
21 Bc[1,:] = True
22 Bc[:,1] = True
23 t = time.time()
24 #Building the min-tree
25 mxt = iamxt.MaxTreeAlpha(img_neg,Bc)
26
27 #Area filtering
28 area = mxt.node_array[3,:]
29 mxt.contractDR(np.logical_and(area>a_min,area< a_max))
30 img2_neg = mxt.getImage()
31 adshow(img2_neg,"Only nodes with %d < area < %d" %(a_min,a_max))
32
33 nnodes = mxt.node_array.shape[-1]
34 print 'nodes1 :', nnodes
35
36
37 #Bounding-box filtering
38 xmin,xmax = mxt.node_array[6,:],mxt.node_array[7,:]
39 ymin,ymax = mxt.node_array[9,:],mxt.node_array[10,:]
40 dx = xmax - xmin
41 dy = ymax - ymin
42
43 mxt.contractDR(np.logical_and(dy<dy_max,dx< dx_max))
44 img3_neg = mxt.getImage()
45 adshow(img3_neg,"Only nodes with dx < %d and dy < %d" %(dx_max,dy_max))
46
47 #Aspect ratio filtering
48 xmin,xmax = mxt.node_array[6,:],mxt.node_array[7,:]
49 ymin,ymax = mxt.node_array[9,:],mxt.node_array[10,:]
50 AR = np.asarray([xmax-xmin,ymax - ymin], dtype = float)
51 AR = 1.0*AR.min(axis = 0)/AR.max(axis = 0)
52 mxt.contractDR(AR>ar_min)
53 img4_neg = mxt.getImage()
54 adshow(img4_neg,"Only nodes with aspect ratio > %f" %ar_min)
55
56 nnodes = mxt.node_array.shape[-1]
57 print 'nodes2 :', nnodes
58
59 L1,L2,ecc = mxt.computeEccentricity()
60 mxt.contractDR(ecc>ecc_min)
61 nnodes = mxt.node_array.shape[-1]
62 print 'nodes:', nnodes
63 img5_neg = mxt.getImage()
64 adshow(img5_neg,"Only nodes with eccentricity > %f" %ecc_min)
65
66 nnodes = mxt.node_array.shape[-1]
67 print 'nodes3 :', nnodes
68
69 rr = mxt.computeRR()
70 mxt.contractDR(rr>rr_min)
71 img6_neg = mxt.getImage()
72 adshow(img6_neg,"Only nodes with rectangularity ratio > %f" %rr_min)
73
74 nnodes = mxt.node_array.shape[-1]
75 print 'nodes4 :', nnodes
```
```nodes1 : 2197
nodes2 : 227
nodes: 224
nodes3 : 224
nodes4 : 148
```

# Do it yourself

Use size and shape criteria to segment the carotid wall.

```1 mxt = iamxt.MaxTreeAlpha(img,Bc)
```