-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathpreprocessing.py
120 lines (95 loc) · 3.42 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
108
109
110
111
112
113
114
115
116
117
118
119
120
import numpy as np
import cv2 as cv
from paddle.fluid import dygraph
from paddle.fluid import layers
from pytracking.libs.paddle_utils import PTensor, n2p, _padding, squeeze, unsqueeze
def numpy_to_paddle(a: np.ndarray):
return unsqueeze(
layers.transpose(
layers.cast(dygraph.to_variable(a), 'float32'), [2, 0, 1]), [0])
def paddle_to_numpy(a: PTensor):
return layers.transpose(squeeze(a, [0]), [1, 2, 0]).numpy()
def sample_patch(im: np.ndarray,
pos: np.ndarray,
sample_sz: np.ndarray,
output_sz: np.ndarray=None):
"""Sample an image patch.
args:
im: Image
pos: center position of crop
sample_sz: size to crop
output_sz: size to resize to
"""
# copy and convert
posl = pos.astype('long')
# Compute pre-downsampling factor
if output_sz is not None:
resize_factor = np.min(
sample_sz.astype('float32') / output_sz.astype('float32'))
df = int(max(int(resize_factor - 0.1), 1))
else:
df = int(1)
sz = sample_sz.astype('float32') / df # new size
# Do downsampling
if df > 1:
os = posl % df # offset
posl = ((posl - os) / df).astype('long') # new position
im2 = im[os[0]::df, os[1]::df] # downsample
else:
im2 = im
# compute size to crop
szl = np.maximum(
np.round(sz), np.array(
[2., 2.], dtype='float32')).astype('long')
# Extract top and bottom coordinates
tl = posl - (szl - 1) // 2
br = posl + szl // 2
# Get image patch
im_patch = _padding(
im2, (0, 0, -tl[1], br[1] - im2.shape[1] + 1, -tl[0],
br[0] - im2.shape[0] + 1),
mode='replicate')
if output_sz is None or (im_patch.shape[0] == output_sz[0] and
im_patch.shape[1] == output_sz[1]):
return im_patch
# Resample
osz = output_sz.astype('long')
im_patch = cv.resize(
im_patch, (osz[1], osz[0]), interpolation=cv.INTER_LINEAR)
return im_patch
def sample_patch_with_mean_pad(im: np.ndarray,
pos: np.ndarray,
sample_sz: np.ndarray,
output_sz: np.ndarray=None):
"""Sample an image patch.
args:
im: Image
pos: center position of crop
sample_sz: size to crop
output_sz: size to resize to
"""
# copy and convert
# posl = np.round(pos).astype('long') # TODO: maybe we should use round
posl = pos.astype('long')
im2 = im
sz = sample_sz.astype('float32')
# compute size to crop
szl = np.maximum(
np.round(sz), np.array(
[2., 2.], dtype='float32')).astype('long')
# Extract top and bottom coordinates
tl = posl - (szl - 1) // 2
br = posl + szl // 2
# Get image patch
im_patch = _padding(
im2, (0, 0, -tl[1], br[1] - im2.shape[1] + 1, -tl[0],
br[0] - im2.shape[0] + 1),
mode='replicate')
if output_sz is None or (im_patch.shape[0] == output_sz[0] and
im_patch.shape[1] == output_sz[1]):
return im_patch
# Resample
osz = output_sz.astype('long')
im_patch = cv.resize(
im_patch, (osz[1], osz[0]), interpolation=cv.INTER_LINEAR)
return im_patch