|
a |
|
b/ImageProcessing.py |
|
|
1 |
import numpy as np |
|
|
2 |
import SimpleITK as sitk |
|
|
3 |
|
|
|
4 |
def read_image(opt): |
|
|
5 |
I_pre = sitk.ReadImage(opt.pre) |
|
|
6 |
I_post = sitk.ReadImage(opt.post) |
|
|
7 |
img_pre = np.array(sitk.GetArrayFromImage(I_pre)) |
|
|
8 |
img_post = np.array(sitk.GetArrayFromImage(I_post)) |
|
|
9 |
img_sub = img_post - img_pre |
|
|
10 |
return img_pre, img_post, img_sub, np.array(I_pre.GetSpacing()) |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
def Norm_Zscore(img): |
|
|
15 |
img= (img-np.mean(img))/np.std(img) |
|
|
16 |
return img |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
def imgnorm(N_I,index1=0.001,index2=0.001): |
|
|
20 |
N_I = N_I.astype(np.float32) |
|
|
21 |
I_sort = np.sort(N_I.flatten()) |
|
|
22 |
I_min = I_sort[int(index1*len(I_sort))] |
|
|
23 |
I_max = I_sort[-int(index2*len(I_sort))] |
|
|
24 |
|
|
|
25 |
N_I =1.0*(N_I-I_min)/(I_max-I_min) |
|
|
26 |
N_I[N_I>1.0]=1.0 |
|
|
27 |
N_I[N_I<0.0]=0.0 |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
return N_I |
|
|
31 |
|
|
|
32 |
def save_image(image,opt,savename): |
|
|
33 |
I = sitk.ReadImage(opt.pre) |
|
|
34 |
Heat_image = sitk.GetImageFromArray(image, isVector=False) |
|
|
35 |
Heat_image.SetSpacing(I.GetSpacing()) |
|
|
36 |
Heat_image.SetOrigin(I.GetOrigin()) |
|
|
37 |
Heat_image.SetDirection(I.GetDirection()) |
|
|
38 |
sitk.WriteImage(Heat_image,opt.outfolder+'/'+savename) |
|
|
39 |
|
|
|
40 |
|
|
|
41 |
from skimage.morphology import closing |
|
|
42 |
from scipy import ndimage as nd |
|
|
43 |
def labeling_seg(seg): |
|
|
44 |
bw = closing(seg) |
|
|
45 |
struct = nd.generate_binary_structure(3, 3) |
|
|
46 |
bw = nd.morphology.binary_dilation(bw,structure=struct,iterations=7) |
|
|
47 |
bw=bw.astype('uint8') |
|
|
48 |
label_image = nd.measurements.label(bw) |
|
|
49 |
label_image = label_image[0] |
|
|
50 |
|
|
|
51 |
label_image = seg*label_image |
|
|
52 |
|
|
|
53 |
return label_image |