-
Notifications
You must be signed in to change notification settings - Fork 0
/
joint_transforms.py
42 lines (35 loc) · 1.59 KB
/
joint_transforms.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
# ============================================================
# THIS FILE CONTAINS THE METHODS FOR TRANSFORMING THE IMAGES.
# Authors: Mark Edward M. Gonzales & Lorene C. Uy
# ============================================================
import random
from PIL import Image
# =====================================================
# Class for composing several transformations together
# =====================================================
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img, edge, mask):
assert img.size == mask.size and edge.size == mask.size
for t in self.transforms:
img, edge, mask = t(img, edge, mask)
return img, edge, mask
# ==================================
# Apply random horizontal flipping.
# ==================================
class RandomHorizontallyFlip(object):
def __call__(self, img, edge, mask):
if random.random() < 0.5:
return img.transpose(Image.FLIP_LEFT_RIGHT), edge.transpose(Image.FLIP_LEFT_RIGHT), mask.transpose(Image.FLIP_LEFT_RIGHT)
return img, edge, mask
# ================
# Apply resizing.
# ================
class Resize(object):
def __init__(self, size):
# Reverse since size follows (height, width) while PIL requires (width, height)
self.size = tuple(reversed(size))
def __call__(self, img, edge, mask):
assert img.size == mask.size and edge.size == mask.size
return img.resize(self.size, Image.BILINEAR), edge.resize(self.size, Image.NEAREST), mask.resize(self.size, Image.NEAREST)