forked from clguo/RSAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
38 lines (32 loc) · 1.4 KB
/
util.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
import cv2
import numpy as np
def crop_to_shape(data, shape):
"""
Crops the array to the given image shape by removing the border (expects a tensor of shape [batches, nx, ny, channels].
:param data: the array to crop
:param shape: the target shape
"""
#
offset0 = (data.shape[1] - shape[1])//2
offset1 = (data.shape[2] - shape[2])//2
if offset0==0:
if data.shape[1] % 2 == 1 or shape[1] % 2 == 1:
return data[:, offset0:data.shape[1], offset1:(-offset1)]
elif data.shape[2] % 2 == 1 or shape[2] % 2 == 1:
return data[:, offset0:data.shape[1], offset1:(-offset1 - 1)]
else:
return data[:, offset0:data.shape[1], offset1:(-offset1)]
elif offset1==0:
if data.shape[1] % 2 == 1 or shape[1] % 2 == 1:
return data[:, offset0:(-offset0 - 1), offset1:data.shape[2]]
elif data.shape[2] % 2 == 1 or shape[2] % 2 == 1:
return data[:, offset0:-offset0, offset1:data.shape[2]]
else:
return data[:, offset0:-offset0, offset1:data.shape[2]]
else:
if data.shape[1] % 2 == 1 or shape[1] % 2 == 1:
return data[:, offset0:(-offset0 - 1), offset1:(-offset1)]
elif data.shape[2] % 2 == 1 or shape[2] % 2 == 1:
return data[:, offset0:-offset0, offset1:(-offset1-1)]
else:
return data[:, offset0:-offset0, offset1:(-offset1)]