# 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
7
8 img_neg = 255-img
10
11 #Filtering thresholds
12 a_min,a_max = 100,300
13 dx_max,dy_max = 22,22
14 rr_max = 0.4
15 ar_min = 0.75
16 ecc_min = 0.7
17 rr_min = 0.55
18
19 Bc = np.zeros((3,3),dtype = bool)
20 Bc[1,:] = True
21 Bc[:,1] = True
22 t = time.time()
23 #Building the min-tree
24 mxt = iamxt.MaxTreeAlpha(img_neg,Bc)
25
26 #Area filtering
27 area = mxt.node_array[3,:]
28 mxt.contractDR(np.logical_and(area>a_min,area< a_max))
29 img2_neg = mxt.getImage()
30 adshow(img2_neg,"Only nodes with %d < area < %d" %(a_min,a_max))
31
32 #Bounding-box filtering
33 xmin,xmax = mxt.node_array[6,:],mxt.node_array[7,:]
34 ymin,ymax = mxt.node_array[9,:],mxt.node_array[10,:]
35 dx = xmax - xmin
36 dy = ymax - ymin
37
38 mxt.contractDR(np.logical_and(dy<dy_max,dx< dx_max))
39 img3_neg = mxt.getImage()
40 adshow(img3_neg,"Only nodes with dx < %d and dy < %d" %(dx_max,dy_max))
41
42 #Aspect ratio filtering
43 xmin,xmax = mxt.node_array[6,:],mxt.node_array[7,:]
44 ymin,ymax = mxt.node_array[9,:],mxt.node_array[10,:]
45 AR = np.asarray([xmax-xmin,ymax - ymin], dtype = float)
46 AR = 1.0*AR.min(axis = 0)/AR.max(axis = 0)
47 mxt.contractDR(AR>ar_min)
48 img4_neg = mxt.getImage()
49 adshow(img4_neg,"Only nodes with aspect ratio > %f" %ar_min)
50
51 L1,L2,ecc = mxt.computeEccentricity()
52 mxt.contractDR(ecc>ecc_min)
53 img5_neg = mxt.getImage()
54 adshow(img5_neg,"Only nodes with eccentricity > %f" %ecc_min)
55
56 rr = mxt.computeRR()
57 mxt.contractDR(rr>rr_min)
58 img6_neg = mxt.getImage()
59 adshow(img6_neg,"Only nodes with rectangularity ratio > %f" %rr_min)

# Do it yourself

Use size and shape criteria to segment the carotid wall.

1 import numpy as np
2 import iamxt
3 import time
4
7
8 #Filtering thresholds
9 a_min,a_max = 80,400
10 dx_max,dy_max = 40,40
11 rr_max = 0.4
12 ar_min = 0.75
13 ecc_min = 0.6
14 rr_min = 0.3
15
16 Bc = np.zeros((3,3),dtype = bool)
17 Bc[1,:] = True
18 Bc[:,1] = True
19 t = time.time()
20 #Building the max-tree
21 mxt = iamxt.MaxTreeAlpha(img,Bc)
22
23 #Area filtering
24 area = mxt.node_array[3,:]
25 mxt.contractDR(np.logical_and(area>a_min,area< a_max))
26 img2_neg = mxt.getImage()
27 adshow(img2_neg,"Only nodes with %d < area < %d" %(a_min,a_max))
28
29 #Bounding-box filtering
30 xmin,xmax = mxt.node_array[6,:],mxt.node_array[7,:]
31 ymin,ymax = mxt.node_array[9,:],mxt.node_array[10,:]
32 dx = xmax - xmin
33 dy = ymax - ymin
34
35 mxt.contractDR(np.logical_and(dy<dy_max,dx< dx_max))
36 img3_neg = mxt.getImage()
37 adshow(img3_neg,"Only nodes with dx < %d and dy < %d" %(dx_max,dy_max))
38
39 #Aspect ratio filtering
40 xmin,xmax = mxt.node_array[6,:],mxt.node_array[7,:]
41 ymin,ymax = mxt.node_array[9,:],mxt.node_array[10,:]
42 AR = np.asarray([xmax-xmin,ymax - ymin], dtype = float)
43 AR = 1.0*AR.min(axis = 0)/AR.max(axis = 0)
44 mxt.contractDR(AR>ar_min)
45 img4_neg = mxt.getImage()
46 adshow(img4_neg,"Only nodes with aspect ratio > %f" %ar_min)
47
48 L1,L2,ecc = mxt.computeEccentricity()
49 mxt.contractDR(ecc>ecc_min)
50 img5_neg = mxt.getImage()
51 adshow(img5_neg,"Only nodes with eccentricity > %f" %ecc_min)
52
53 rr = mxt.computeRR()
54 mxt.contractDR(rr>rr_min)
55 img6_neg = mxt.getImage()
56 adshow(img6_neg,"Only nodes with rectangularity ratio > %f" %rr_min)