Watershed Tie-zone Classification Illustration

 1 import numpy as np
 2 import os
 3 import nibabel as nib
 4 from morph import mmimg2se, mmero
 5 import ia636
 6 
 7 
 8 subject_path ='/awmedia3/q/orig/LPBA40/native_space/S11'
 9 
10 orig = 'S11.native.mri.img'
11 orig = os.path.join(subject_path,orig)
12 
13 mask = 'S11.native.brain.mask.img'
14 mask = os.path.join(subject_path,mask)
15 
16 
17 orig_data = nib.load(orig).get_data()
18 mask_data = nib.load(mask).get_data() > 0
19 
20 if orig_data.ndim == 4:
21     orig_data = orig_data[:,:,:,0]
22 
23 if mask_data.ndim == 4:
24     mask_data = mask_data[:,:,:,0]
25 
26 H,W,Z = orig_data.shape
27 
28 ncols = 5
29 
30 ift_ws = os.path.join('/awmedia3/q/proc/MICCAI2016_SS_Study/lpba40/ws3d','S11_ws3d.nii.gz')
31 ift_ws = nib.load(ift_ws).get_data() > 0
32 
33 tz_min = os.path.join('/awmedia3/q/proc/MICCAI2016_SS_Study/lpba40/ws3d_2','S11_ws3d.nii.gz')
34 tz_min = nib.load(tz_min).get_data() > 0
35 
36 tz_max = os.path.join('/awmedia3/q/proc/MICCAI2016_SS_Study/lpba40/ws3d_3','S11_ws3d.nii.gz')
37 tz_max = nib.load(tz_max).get_data() > 0
38 
39 tz_optimum = os.path.join('/awmedia3/q/proc/MICCAI2016_SS_Study/lpba40/ws3d_4','S11_ws3d.nii.gz')
40 tz_optimum = nib.load(tz_optimum).get_data()
41 label = tz_optimum[H/2,W/2,Z/2]
42 tz = (tz_optimum == 0)
43 tz_optimum =  (tz_optimum == label)
44 tz_optimum[tz] = mask_data[tz]
45 
46 
47 
48 
49 tz_rf = os.path.join('/awmedia3/q/proc/MICCAI2016_SS_Study/lpba40/ws3d_5','S11_ws3d.nii.gz')
50 tz_rf = nib.load(tz_rf).get_data() > 0
51 
52 
53 orig_data = ia636.ianormalize(orig_data).astype(np.uint8)
54 #adshow(ia636.iamosaic(orig_data,ncols))

Mosaics

 1 import ia870
 2 from homedavidsantos.iamosaic_color import iamosaic_color
 3 from homeroberto.crop3D import crop3D
 4 
 5 x1,x2,y1,y2,z1,z2 = crop3D(tz_max)
 6 x1-=2
 7 x2+=2
 8 y1-=2
 9 y2+=2
10 z1-=2
11 z2+=2
12 
13 orig_data = orig_data[x1:x2,y1:y2,z1:z2]
14 mask_data = mask_data[x1:x2,y1:y2,z1:z2]
15 ift_ws = ift_ws[x1:x2,y1:y2,z1:z2]
16 tz_min = tz_min[x1:x2,y1:y2,z1:z2]
17 tz_max = tz_max[x1:x2,y1:y2,z1:z2]
18 tz_optimum = tz_optimum[x1:x2,y1:y2,z1:z2]
19 tz_rf  = tz_rf[x1:x2,y1:y2,z1:z2]
20 
21 s = 76
22 
23 C6 = np.zeros((3,3,3),dtype = bool)
24 C6[1,:,1] = True
25 C6[1,1,:] = True
26 C6[:,1,1] = True
27 Bc = mmimg2se(C6)
28 
29 
30 
31 
32 mask_data = np.logical_xor(mask_data,mmero(mask_data,Bc))
33 ift_ws = np.logical_xor(ift_ws,mmero(ift_ws,Bc))
34 tz_min = np.logical_xor(tz_min,mmero(tz_min,Bc))
35 tz_max = np.logical_xor(tz_max,mmero(tz_max,Bc))
36 tz_optimum = np.logical_xor(tz_optimum,mmero(tz_optimum,Bc))
37 tz_rf  = np.logical_xor(tz_rf,mmero(tz_rf,Bc))
38 
39 
40 orig_data_color = np.array([orig_data,orig_data,orig_data]).transpose(1,2,3,0)
41 print orig_data_color.shape
42 orig_data_color2 = orig_data_color.copy()
43 orig_data_color3 = orig_data_color.copy()
44 
45 
46 
47 #GT and TZ-optimum
48 orig_data_color[mask_data,:] = np.array([0,255,0])
49 orig_data_color[tz_optimum,:] = np.array([255,0,0])
50 aux = orig_data_color.transpose(0,2,1,3)[:,::-1,:,:]
51 adshow(iamosaic_color(aux,ncols), "TZ-optimum (red), manual annotation (blue).")
52 
53 
54 #RF and IFT WS and TZ-optimum
55 orig_data_color2[tz_rf,:] = np.array([0,255,0])
56 orig_data_color2[ift_ws,:] = np.array([0,0,255])
57 orig_data_color2[tz_optimum,:] = np.array([255,0,0])
58 aux2 = orig_data_color2.transpose(0,2,1,3)[:,::-1,:,:]
59 adshow(iamosaic_color(aux2,ncols), "TZ-optimum (red), TZ-classification (green) and IFT-watershed (blue).")
60 
61 #TZ-optimum, TZ-min, TZ-max
62 orig_data_color3[tz_min,:] = np.array([0,255,0])
63 orig_data_color3[tz_max,:] = np.array([0,0,255])
64 orig_data_color3[tz_optimum,:] = np.array([255,0,0])
65 aux3 = orig_data_color3.transpose(0,2,1,3)[:,::-1,:,:]
66 adshow(iamosaic_color(aux3,ncols), "TZ-optimum (red), minimum (green) and maximum (blue).")
(152, 120, 210, 3)

TZ-optimum (red), manual annotation (blue).

TZ-optimum (red), TZ-classification (green) and IFT-watershed (blue).

TZ-optimum (red), minimum (green) and maximum (blue).

One Slice

1 adshow(orig_data_color[s,:,::-1,:].transpose(2,1,0), "TZ-optimum (red), manual annotation (blue).")
2 adshow(orig_data_color2[s,:,::-1,:].transpose(2,1,0), "TZ-optimum (red), TZ-classification (green) and IFT-watershed (blue).")
3 adshow(orig_data_color3[s,:,::-1,:].transpose(2,1,0), "TZ-optimum (red), minimum (green) and maximum (blue).")

TZ-optimum (red), manual annotation (blue).

TZ-optimum (red), TZ-classification (green) and IFT-watershed (blue).

TZ-optimum (red), minimum (green) and maximum (blue).