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

Original Image

Mean filter 7x7 mask

Median filter 7x7 mask

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
 8 Bc = np.ones((3,3),dtype = bool)
 9 img_max = img.max()
10 img_neg = img_max - img
11 adshow(img_neg,"Negative")
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()
19 adshow(area_open,"Negative Filtered")
20 area_open = img_max - area_open
21 adshow(area_open,"Filtered image")
Nb of nodes before: 26610
Nb of nodes after: 296

Image corrupted by noise

Negative

Negative Filtered

Filtered image