Skip to content

Commit ee2660b

Browse files
committed
update tranformers
1 parent c936efa commit ee2660b

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

uncleloader/base_transforms.py

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,33 @@
1515
import random
1616
import numpy as np
1717

18+
# padding modes in opencv
1819
paddings = {'z': cv2.BORDER_CONSTANT,
1920
'r': cv2.BORDER_REFLECT_101}
2021

22+
# interpolation modes in opencv
2123
interpolations = {'bilinear': cv2.INTER_LINEAR,
2224
'bicubic': cv2.INTER_CUBIC,
2325
'nearest': cv2.INTER_NEAREST}
2426

2527
def _apply_perspective(img, M, shape, interp_mode='bilinear', padding_mode='r'):
26-
return cv2.warpPerspective(img, M, shape,
28+
""" Apply perspective transformation matrix to img with opencv API
29+
30+
Parameters
31+
----------
32+
img: 2-D image,
33+
the shape is (height, width, channel).
34+
M: transform matrix,
35+
np.array (3, 3)
36+
ref: https://upload.wikimedia.org/wikipedia/commons/2/2c/2D_affine_transformation_matrix.svg
37+
"""
38+
return cv2.warpPerspective(img, M, shape,
2739
flags=interpolations[interp_mode],
2840
borderMode=paddings[padding_mode])
2941

3042
class ImageOnly(object):
43+
""" Apply one transform only to images
44+
"""
3145
def __init__(self, transform):
3246
self.transform = transform
3347

@@ -36,6 +50,8 @@ def __call__(self, img, mask=None):
3650

3751

3852
class DualCompose(object):
53+
""" Apply a list of transforms to data
54+
"""
3955
def __init__(self, transforms, shuffle=False, ImageOnly=False):
4056
self.transforms = transforms
4157
self.ImageOnly = ImageOnly
@@ -52,6 +68,8 @@ def __call__(self, img, mask=None):
5268

5369

5470
class RandomCompose(object):
71+
""" Randomly select some tranforms in list to apply to data.
72+
"""
5573
def __init__(self, transforms, max_num=1):
5674
self.transforms = transforms
5775
self.max_num = max_num
@@ -62,4 +80,68 @@ def __call__(self, img, mask=None):
6280
img, mask = t(img, mask)
6381
return img, mask
6482

83+
"""
84+
PREPROCESSING FUNCTIONS
85+
"""
86+
87+
class ImageResize(object):
88+
def __init__(self, size, interp_mode='bilinear'):
89+
self.size = size
90+
self.interp_mode = interp_mode
91+
92+
def __call__(self, img, mask=None):
93+
img = cv2.resize(img, self.size,
94+
interpolation=interpolations[self.interp_mode])
95+
if mask is not None:
96+
mask = cv2.resize(mask, self.size,
97+
interpolation=interpolations[self.interp_mode])
98+
return img, mask
99+
100+
"""
101+
AUGUMENTATION FUNCTIONS
102+
"""
103+
class random_horizontal_flip(object):
104+
def __init__(self, prob=0.5):
105+
self.prob = prob
106+
107+
def __call__(self, img, mask=None):
108+
if random.random() < self.prob:
109+
img = cv2.flip(img, 0)
110+
if mask is not None:
111+
mask = cv2.flip(mask, 0)
112+
return img, mask
113+
114+
class random_vertical_flip(object):
115+
def __init__(self, prob=0.5):
116+
self.prob = prob
117+
118+
def __call__(self, img, mask=None):
119+
if random.random() < self.prob:
120+
img = cv2.flip(img, 1)
121+
if mask is not None:
122+
mask = cv2.flip(mask, 1)
123+
return img, mask
124+
125+
class random_flip(object):
126+
def __init__(self, prob=0.5):
127+
self.prob = prob
128+
129+
def __call__(self, img, mask=None):
130+
if random.random() < self.prob:
131+
d = random.randint(-1, 1)
132+
img = cv2.flip(img, d)
133+
if mask is not None:
134+
mask = cv2.flip(mask, d)
135+
return img, mask
136+
137+
class random_transpose(object):
138+
def __int__(self, prob=0.5):
139+
self.prob = prob
140+
141+
def __call__(self, img, mask=None):
142+
if random.random() < self.prob:
143+
img = img.transpose(1, 0, 2)
144+
if mask is not None:
145+
mask = mask.transpose(1, 0)
146+
return img, mask
65147

0 commit comments

Comments
 (0)