-
Notifications
You must be signed in to change notification settings - Fork 0
/
cropper.py
113 lines (85 loc) · 3.32 KB
/
cropper.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
108
109
110
111
112
113
import numpy as np
def crop(image):
"""
Crops a numpy array of surrounding 0 values
i.e. the reverse of np.pad
only works for 1D to 5D array
"""
if len(image.shape) == 1:
xs= np.where(image != 0)
image = image[min(xs):max(xs)+1]
elif len(image.shape) == 2:
xs, ys = np.where(image != 0)
image = image[min(xs):max(xs)+1, min(ys):max(ys)+1]
elif len(image.shape) == 3:
xs, ys, zs = np.where(image != 0)
image = image[min(xs):max(xs)+1, min(ys):max(ys)+1, min(zs):max(zs)+1]
elif len(image.shape) == 4:
xs, ys, zs, zz = np.where(image != 0)
image = image[min(xs):max(xs)+1, min(ys):max(ys)+1, min(zs):max(zs)+1, min(zz):max(zz)+1]
elif len(image.shape) == 5:
xs, ys, zs, zz, aa = np.where(image != 0)
image = image[min(xs):max(xs)+1, min(ys):max(ys)+1, min(zs):max(zs)+1, min(zz):max(zz)+1, min(aa):max(aa)+1]
else:
print('[Warning!] Array has dimensions > 5, use ncrop')
return image
def ncrop(image):
"""
Crops a numpy array of surrounding 0 values
i.e. the reverse of np.pad
works on N-dimensional array
"""
xs = np.where(image != 0)
for i in range(len(xs)):
image = np.swapaxes(image, 0, i)
xmin = min(xs[i])
xmax = max(xs[i])+1
image = image[xmin:xmax,...]
image = np.swapaxes(image, 0, i)
return image
def nancrop(image_):
"""
Crops a numpy array of surrounding nan values
i.e. the reverse of np.pad
only works for 1D to 5D array
"""
coords = np.argwhere(~np.isnan(image_))
if len(image_.shape) == 2:
xmin = np.min(coords[:,0])
xmax = np.max(coords[:,0]) + 1
ymin = np.min(coords[:,1])
ymax = np.max(coords[:,1]) + 1
image_ = image_[xmin:xmax, ymin:ymax]
elif len(image_.shape) == 3:
xmin = np.min(coords[:,0])
xmax = np.max(coords[:,0]) + 1
ymin = np.min(coords[:,1])
ymax = np.max(coords[:,1]) + 1
zmin = np.min(coords[:,2])
zmax = np.max(coords[:,2]) + 1
image_ = image_[xmin:xmax, ymin:ymax, zmin:zmax]
elif len(image_.shape) == 4:
xmin = np.min(coords[:,0])
xmax = np.max(coords[:,0]) + 1
ymin = np.min(coords[:,1])
ymax = np.max(coords[:,1]) + 1
zmin = np.min(coords[:,2])
zmax = np.max(coords[:,2]) + 1
amin = np.min(coords[:,3])
amax = np.max(coords[:,3]) + 1
image_ = image_[xmin:xmax, ymin:ymax, zmin:zmax, amin:amax]
elif len(image_.shape) == 5:
xmin = np.min(coords[:,0])
xmax = np.max(coords[:,0]) + 1
ymin = np.min(coords[:,1])
ymax = np.max(coords[:,1]) + 1
zmin = np.min(coords[:,2])
zmax = np.max(coords[:,2]) + 1
amin = np.min(coords[:,3])
amax = np.max(coords[:,3]) + 1
bmin = np.min(coords[:,4])
bmax = np.max(coords[:,4]) + 1
image_ = image_[xmin:xmax, ymin:ymax, zmin:zmax, amin:amax, bmin:bmax]
else:
print('[Warning!] Array has dimensions > 5, cannot compute')
return image_