Size filtering

MainPage || iamxt

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 
 7 img = adreadgray(find_attachment_file('iamxt/conv_attribute_filtering/ct.png'))
 8 
 9 Bc = np.ones((3,3),dtype = bool) # Connectivity-8
10 adshow(img, "Original Image")
11 
12 
13 #Spatial filterd
14 mask = np.ones((7,7))
15 
16 #Mean filter
17 mean_filter = convolve(img.astype(float),mask,mode = 'wrap')/49
18 adshow(mean_filter.astype(np.uint8),'Mean filter 7x7 mask')
19 
20 #Median filter
21 median_filt = median_filter(img.astype(float),footprint = mask,mode = 'wrap')
22 adshow(median_filt.astype(np.uint8),'Median filter 7x7 mask')
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()
29 adshow(area_open, 'Area-open - 25')

Original Image

Mean filter 7x7 mask

Median filter 7x7 mask

Area-open - 25

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 
 7 img =adreadgray('/awmedia/www/media/Attachments/homeroberto/homeroberto_softwareX_bbox/lp.png')
 8 adshow(img,'original image')
 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 
24 adshow(mxt.getImage(),title='Bounding-box Filter')

original image

Bounding-box Filter

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 
4 img = adreadgray('/awmedia/www/media/Attachments/iamxt/simple_filtering_ex/apple.png')
5 adshow(img, "Image corrupted by noise")
6 
7 # Write your solution here

Image corrupted by noise