-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocessing.py
107 lines (84 loc) · 2.74 KB
/
preprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import numpy as np
def whitening(image):
"""Whitening. Normalises image to zero mean and unit variance.
[INPUT]
Image -> the input image to whiten. Three dimensions i.e. [x,y,z]
[OUTPUT]
Image -> whitened volume
"""
image = image.astype(np.float32)
mean = np.mean(image)
std = np.std(image)
if std > 0:
ret = (image - mean) / std
else:
ret = image * 0.
return ret
def normalise_zero_one(image):
"""Image normalisation. Normalises image to fit [0, 1] range.
[INPUT]
Image -> the input image to normalize. Three dimensions i.e. [x,y,z]
[OUTPUT]
Image -> normalized volume
"""
image = image.astype(np.float32)
minimum = np.min(image)
maximum = np.max(image)
if maximum > minimum:
ret = (image - minimum) / (maximum - minimum)
else:
ret = image * 0.
return ret
def normalise_one_one(image):
"""Image normalization. Normalises image to fit [-1, 1] range.
[INPUT]
Image -> the input image to noramalize. Three dimensions i.e. [x,y,z]
[OUTPUT]
Image -> normalized volume
"""
ret = normalise_zero_one(image)
ret *= 2.
ret -= 1.
return ret
def whitening_3D(img):
"""Whitening. Normalises multichannel volume to zero mean and unit variance.
[INPUT]
Image -> the input volume to whiten. Expected channel last convention
i.e. [x,y,z,channels]
[OUTPUT]
Image -> whitened volume
"""
if len(img.shape) == 3:
return whitening(img)
else :
im = [whitening(img[:, :, :, c]) for c in range(img.shape[3])]
white_im = np.stack(im, axis=3)
return white_im
def normalise_zero_one_3D(img):
"""Image normalisation. Normalises multichannel volume to fit [0, 1] range.
[INPUT]
Image -> the input volume to normalize. Expected channel last convention
i.e. [x,y,z,channels]
[OUTPUT]
Image -> normalized volume
"""
if len(img.shape) == 3:
return normalise_zero_one(img)
else :
im = [normalise_zero_one(img[:, :, :, c]) for c in range(img.shape[3])]
norm_im = np.stack(im, axis=3)
return norm_im
def normalise_one_one_3D(img):
"""Image normalisation. Normalises multichannel volume to fit [-1, 1] range.
[INPUT]
Image -> the input volume to normalize. Expected channel last convention
i.e. [x,y,z,channels]
[OUTPUT]
Image -> normalized volume
"""
if len(img.shape) == 3:
return normalise_one_one(img)
else :
im = [normalise_one_one(img[:, :, :, c]) for c in range(img.shape[3])]
norm_im = np.stack(im, axis=3)
return norm_im