Skip to content

Commit 8681745

Browse files
author
GOESTERN-0968798
committed
Padding of affine transformation
1 parent e2ff655 commit 8681745

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

scripts/augmentation/affine_transform.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,27 @@ def affine_transform_euler(data, euler_angles, label_flag = False):
155155
offset = [t_vec[0][0], t_vec[1][0], t_vec[2][0]]
156156

157157
if label_flag:
158-
result = scipy.ndimage.affine_transform(data, rot_matrix, order=0, offset=offset, prefilter=False)
158+
result = scipy.ndimage.affine_transform(data, rot_matrix, order=0, offset=offset, prefilter=False).astype(np.int32)
159159
else:
160-
result = scipy.ndimage.affine_transform(data, rot_matrix, offset=offset)
160+
cval = get_cval(data)
161+
result = scipy.ndimage.affine_transform(data, rot_matrix, offset=offset, cval=cval)
161162
return result
162163

164+
def get_cval(arr):
165+
"""
166+
Get constant value 'cval' for padding of affine transformation.
167+
The value is the mean value of the corner section of the volume with the smallest standard deviation.
168+
169+
:param np.ndarray arr: Input array with format [x y z]
170+
:returns: Value for padding
171+
:rtype:float
172+
"""
173+
corner_arrays = [arr[0:arr.shape[0]//10, 0:arr.shape[1]//10, :], arr[0:arr.shape[0]//10, -arr.shape[1]//10:], arr[-arr.shape[0]//10:, 0:arr.shape[1]//10], arr[-arr.shape[0]//10:, -arr.shape[1]//10:]]
174+
stdv = [np.std(a) for a in corner_arrays]
175+
min_std_index = stdv.index(min(stdv))
176+
pad_value = np.mean(corner_arrays[min_std_index])
177+
return pad_value
178+
163179
def pad_scaled_output(arr, target_shape, pad_type = 'zero'):
164180
"""
165181
Pad input array, either with constant value 'zero'
@@ -173,10 +189,7 @@ def pad_scaled_output(arr, target_shape, pad_type = 'zero'):
173189
"""
174190

175191
if "mean" == pad_type:
176-
corner_arrays = [arr[0:arr.shape[0]//10, 0:arr.shape[1]//10, :], arr[0:arr.shape[0]//10, -arr.shape[1]//10:], arr[-arr.shape[0]//10:, 0:arr.shape[1]//10], arr[-arr.shape[0]//10:, -arr.shape[1]//10:]]
177-
stdv = [np.std(a) for a in corner_arrays]
178-
min_std_index = stdv.index(min(stdv))
179-
pad_value = np.mean(corner_arrays[min_std_index])
192+
pad_value = get_cval(arr)
180193
elif "zero" == pad_type:
181194
pad_value = 0
182195
else:

0 commit comments

Comments
 (0)