forked from harpArk614/3d-pose-warping
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaugment_color.py
64 lines (46 loc) · 1.64 KB
/
augment_color.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
import cv2
import numpy as np
def augment_brightness(im, in_colorspace, rng):
if in_colorspace != 'rgb':
cv2.cvtColor(im, cv2.COLOR_HSV2RGB, dst=im)
im += rng.uniform(-0.125, 0.125)
return 'rgb'
def augment_contrast(im, in_colorspace, rng):
if in_colorspace != 'rgb':
cv2.cvtColor(im, cv2.COLOR_HSV2RGB, dst=im)
im -= 0.5
im *= 1 + rng.uniform(-0.5, 0.5)
im += 0.5
return 'rgb'
def augment_hue(im, in_colorspace, rng):
if in_colorspace != 'hsv':
np.clip(im, 0, 1, out=im)
cv2.cvtColor(im, cv2.COLOR_RGB2HSV, dst=im)
hue = im[:, :, 0]
hue += rng.uniform(-72, 72)
hue[hue < 0] += 360
hue[hue > 360] -= 360
return 'hsv'
def augment_saturation(im, in_colorspace, rng):
if in_colorspace != 'hsv':
np.clip(im, 0, 1, out=im)
cv2.cvtColor(im, cv2.COLOR_RGB2HSV, dst=im)
saturation = im[:, :, 1]
saturation *= 1 + rng.uniform(-0.5, 0.5)
saturation[saturation > 1] = 1
return 'hsv'
def augment_color(im, rng):
im += 1
im /= 2
result = np.empty_like(im, dtype=np.float32)
cv2.divide(im, (1, 1, 1, 1), dst=result, dtype=cv2.CV_32F)
augmentation_functions = [augment_brightness, augment_contrast, augment_hue, augment_saturation]
rng.shuffle(augmentation_functions)
colorspace = 'rgb'
for fn in augmentation_functions:
colorspace = fn(result, colorspace, rng)
if colorspace != 'rgb':
cv2.cvtColor(result, cv2.COLOR_HSV2RGB, dst=result)
np.clip(result, 0, 1, out=result)
result = result.astype(np.float32)
return result * 2 - 1