-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
80 lines (67 loc) · 2.85 KB
/
utils.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
# Created by Yizhi Chen. 20180412
import numpy as np
def crop(volume, l_index, output_len, fill_value=0):
'''Crop a cube from the volume.
It must be a cube. output_len is a num, not an array.
#TODO Make it flexible
'''
result = np.ones((output_len, output_len, output_len), dtype=volume.dtype)*fill_value
copy0 = np.array(l_index) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! quote!
copy1 = np.array(copy0)+output_len
paste0 = [0, 0, 0]
paste1 = [output_len, output_len, output_len]
for i in range(3):
if copy0[i]<0:
paste0[i] = -copy0[i]
copy0[i] = 0
if copy1[i]<0:
raise IndexError
if copy1[i]>volume.shape[i]:
paste1[i] = output_len - (copy1[i]-volume.shape[i])
copy1[i] = volume.shape[i]
if copy0[i]>volume.shape[i]:
raise IndexError
result[paste0[0]:paste1[0],paste0[1]:paste1[1], paste0[2]:paste1[2]] = \
volume[copy0[0]:copy1[0], copy0[1]:copy1[1], copy0[2]:copy1[2]]
return result
def revert_crop(volume, l_index, output_len, decrop_input):
'''Revert the crop action.
'''
copy0 = np.array(l_index) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! quote!
copy1 = np.array(copy0)+output_len
paste0 = [0, 0, 0]
paste1 = [output_len, output_len, output_len]
for i in range(3):
if copy0[i]<0:
paste0[i] = -copy0[i]
copy0[i] = 0
if copy1[i]<0:
raise IndexError # In this case, it would be of no sense.
if copy1[i]>volume.shape[i]:
paste1[i] = output_len - (copy1[i]-volume.shape[i])
copy1[i] = volume.shape[i]
if copy0[i]>volume.shape[i]:
raise IndexError
volume[copy0[0]:copy1[0], copy0[1]:copy1[1], copy0[2]:copy1[2]] =\
decrop_input[paste0[0]:paste1[0],paste0[1]:paste1[1], paste0[2]:paste1[2]]
def revert_crop_MAXIMUM(volume, l_index, output_len, decrop_input):
'''Revert the crop action. But Apply a Maximum process during the re-croping.
'''
copy0 = np.array(l_index) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! quote!
copy1 = np.array(copy0)+output_len
paste0 = [0, 0, 0]
paste1 = [output_len, output_len, output_len]
for i in range(3):
if copy0[i]<0:
paste0[i] = -copy0[i]
copy0[i] = 0
if copy1[i]<0:
raise IndexError # In this case, it would be of no sense.
if copy1[i]>volume.shape[i]:
paste1[i] = output_len - (copy1[i]-volume.shape[i])
copy1[i] = volume.shape[i]
if copy0[i]>volume.shape[i]:
raise IndexError
volume[copy0[0]:copy1[0], copy0[1]:copy1[1], copy0[2]:copy1[2]] = \
np.maximum(decrop_input[paste0[0]:paste1[0],paste0[1]:paste1[1], paste0[2]:paste1[2]],
volume[copy0[0]:copy1[0], copy0[1]:copy1[1], copy0[2]:copy1[2]],)