-
Notifications
You must be signed in to change notification settings - Fork 25
/
cvtransforms.py
executable file
·160 lines (130 loc) · 5.09 KB
/
cvtransforms.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
""" OpenCV-based transforms
Operate on np.ndarrays only, no PIL or torch dependency
"""
from __future__ import division
import math
import random
import numpy as np
import numbers
import cv2
class Normalize(object):
"""Given mean: (R, G, B) and std: (R, G, B),
will normalize each channel of the np.ndarray, i.e.
channel = (channel - mean) / std
"""
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
return (tensor - self.mean) / self.std
class Scale(object):
"""Rescales the input np.ndarray to the given 'size'.
'size' will be the size of the smaller edge.
For example, if height > width, then image will be
rescaled to (size * height / width, size)
size: size of the smaller edge
interpolation: Default: cv.INTER_CUBIC
"""
def __init__(self, size, interpolation=cv2.INTER_CUBIC):
self.size = size
self.interpolation = interpolation
def __call__(self, img):
w, h = img.shape[1], img.shape[0]
if (w <= h and w == self.size) or (h <= w and h == self.size):
return img
if w < h:
ow = self.size
oh = int(float(self.size) * h / w)
else:
oh = self.size
ow = int(float(self.size) * w / h)
return cv2.resize(img, dsize=(ow, oh),
interpolation=self.interpolation)
class CenterCrop(object):
"""Crops the given np.ndarray at the center to have a region of
the given size. size can be a tuple (target_height, target_width)
or an integer, in which case the target will be of a square shape
(size, size)
"""
def __init__(self, size):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
def __call__(self, img):
w, h = img.shape[1], img.shape[0]
th, tw = self.size
x1 = int(round((w - tw) / 2.))
y1 = int(round((h - th) / 2.))
return img[y1:y1+th, x1:x1+tw, :]
class Pad(object):
"""Pads the given np.ndarray on all sides with the given "pad" value."""
def __init__(self, padding, borderType=cv2.BORDER_CONSTANT, borderValue=0):
assert isinstance(padding, numbers.Number)
self.padding = padding
self.borderType = borderType
self.borderValue = borderValue
def __call__(self, img):
if self.padding == 0:
return img
p = self.padding
res = cv2.copyMakeBorder(img, p, p, p, p,
borderType=self.borderType,
value=self.borderValue)
return res[:, :, np.newaxis] if np.ndim(res) == 2 else res
class RandomCrop(object):
"""Crops the given np.ndarray at a random location to have a region of
the given size. size can be a tuple (target_height, target_width)
or an integer, in which case the target will be of a square shape
(size, size)
"""
def __init__(self, size):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
def __call__(self, img):
w, h = img.shape[1], img.shape[0]
th, tw = self.size
if w == tw and h == th:
return img
x1 = random.randint(0, w - tw)
y1 = random.randint(0, h - th)
return img[y1:y1+th, x1:x1+tw, :]
class RandomHorizontalFlip(object):
"""Randomly horizontally flips the given np.ndarray with a probability of 0.5
"""
def __call__(self, img):
if random.random() < 0.5:
return cv2.flip(img, 1).reshape(img.shape)
return img
class RandomSizedCrop(object):
"""Random crop the given np.ndarray to a random size of (0.08 to 1.0) of the original size
and and a random aspect ratio of 3/4 to 4/3 of the original aspect ratio
This is popularly used to train the Inception networks
size: size of the smaller edge
interpolation: Default: cv2.INTER_CUBIC
"""
def __init__(self, size, interpolation=cv2.INTER_CUBIC):
self.size = size
self.interpolation = interpolation
def __call__(self, img):
for attempt in range(10):
area = img.shape[0] * img.shape[1]
target_area = random.uniform(0.08, 1.0) * area
aspect_ratio = random.uniform(3. / 4., 4. / 3.)
w = int(round(math.sqrt(target_area * aspect_ratio)))
h = int(round(math.sqrt(target_area / aspect_ratio)))
if random.random() < 0.5:
w, h = h, w
if w <= img.shape[1] and h <= img.shape[0]:
x1 = random.randint(0, img.shape[1] - w)
y1 = random.randint(0, img.shape[0] - h)
img = img[y1:y1+h, x1:x1+w, :]
assert img.shape[0] == h and img.shape[1] == w
return cv2.resize(img, (self.size, self.size),
interpolation=self.interpolation)
# Fallback
scale = Scale(self.size, interpolation=self.interpolation)
crop = CenterCrop(self.size)
return crop(scale(img))