1
- #!/usr/bin/env python3
2
-
3
1
import numpy as np
4
2
from lib .config import cfg
5
3
from PIL import Image
6
4
7
5
8
- def rgb_to_hsv (rgb ):
9
- # Translated from source of colorsys.rgb_to_hsv
10
- # r,g,b should be a numpy arrays with values between 0 and 255
11
- # rgb_to_hsv returns an array of floats between 0.0 and 1.0.
12
- rgb = rgb .astype (np .float32 )
13
- hsv = np .zeros_like (rgb )
14
- # in case an RGBA array was passed, just copy the A channel
15
- hsv [..., 3 :] = rgb [..., 3 :]
16
- r , g , b = rgb [..., 0 ], rgb [..., 1 ], rgb [..., 2 ]
17
- maxc = np .max (rgb [..., :3 ], axis = - 1 )
18
- minc = np .min (rgb [..., :3 ], axis = - 1 )
19
- hsv [..., 2 ] = maxc
20
- mask = maxc != minc
21
- hsv [mask , 1 ] = (maxc - minc )[mask ] / maxc [mask ]
22
- rc = np .zeros_like (r )
23
- gc = np .zeros_like (g )
24
- bc = np .zeros_like (b )
25
- rc [mask ] = (maxc - r )[mask ] / (maxc - minc )[mask ]
26
- gc [mask ] = (maxc - g )[mask ] / (maxc - minc )[mask ]
27
- bc [mask ] = (maxc - b )[mask ] / (maxc - minc )[mask ]
28
- hsv [..., 0 ] = np .select ([r == maxc , g == maxc ],
29
- [bc - gc , 2.0 + rc - bc ],
30
- default = 4.0 + gc - rc )
31
- hsv [..., 0 ] = (hsv [..., 0 ] / 6.0 ) % 1.0
32
- return hsv
33
-
34
-
35
- def hsv_to_rgb (hsv ):
36
- # Translated from source of colorsys.hsv_to_rgb
37
- # h,s should be a numpy arrays with values between 0.0 and 1.0
38
- # v should be a numpy array with values between 0.0 and 255.0
39
- # hsv_to_rgb returns an array of uints between 0 and 255.
40
- rgb = np .empty_like (hsv )
41
- rgb [..., 3 :] = hsv [..., 3 :]
42
- h , s , v = hsv [..., 0 ], hsv [..., 1 ], hsv [..., 2 ]
43
- i = (h * 6.0 ).astype ('uint8' )
44
- f = (h * 6.0 ) - i
45
- p = v * (1.0 - s )
46
- q = v * (1.0 - s * f )
47
- t = v * (1.0 - s * (1.0 - f ))
48
- i = i % 6
49
- conditions = [s == 0.0 , i == 1 , i == 2 , i == 3 , i == 4 , i == 5 ]
50
- rgb [..., 0 ] = np .select (conditions , [v , q , p , p , t , v ], default = v )
51
- rgb [..., 1 ] = np .select (conditions , [v , v , v , q , p , p ], default = t )
52
- rgb [..., 2 ] = np .select (conditions , [v , p , t , v , v , q ], default = p )
53
- return rgb .astype ('uint8' )
54
-
55
-
56
- def shift_hue (arr , hout ):
57
- hsv = rgb_to_hsv (arr )
58
- hsv [0 , ...] += hout # change hue
59
- hsv [0 , ...] = np .max (np .min (hsv [0 , ...], 1 ), 0 )
60
- rgb = hsv_to_rgb (hsv )
61
- return rgb
62
-
63
-
64
6
def image_transform (img , crop_x , crop_y , crop_loc = None , color_tint = None ):
65
7
"""
66
8
Takes numpy.array img
67
9
"""
68
10
69
11
# Slight translation
70
12
if cfg .TRAIN .RANDOM_CROP and not crop_loc :
71
- crop_loc = [0 ]* 2
72
- crop_loc [0 ] = np .random .randint (0 , crop_y ) # corner position row
73
- crop_loc [1 ] = np .random .randint (0 , crop_x ) # corner position column
13
+ crop_loc = [np .random .randint (0 , crop_y ),
14
+ np .random .randint (0 , crop_x )]
74
15
75
16
if crop_loc :
76
17
cr , cc = crop_loc
77
- height , width , channel = img .shape
18
+ height , width , _ = img .shape
78
19
img_h = height - crop_y
79
20
img_w = width - crop_x
80
- img = img [cr :cr + img_h , cc :cc + img_w , : ]
21
+ img = img [cr :cr + img_h , cc :cc + img_w ]
81
22
# depth = depth[cr:cr+img_h, cc:cc+img_w]
82
23
83
- if cfg .TRAIN .HUE_CHANGE and not color_tint :
84
- # color tint
85
- color_tint = (np .random .rand () - 0.5 ) * cfg .TRAIN .HUE_RANGE
86
-
87
- if color_tint :
88
- # Hue change
89
- img = shift_hue (img , color_tint )
90
-
91
24
if cfg .TRAIN .FLIP and np .random .rand () > 0.5 :
92
25
img = img [:, ::- 1 , ...]
93
26
@@ -124,9 +57,7 @@ def add_random_background(im, background_img_fns):
124
57
125
58
126
59
def add_random_color_background (im , color_range ):
127
- r = np .random .randint (color_range [0 ][0 ], color_range [0 ][1 ] + 1 )
128
- g = np .random .randint (color_range [1 ][0 ], color_range [1 ][1 ] + 1 )
129
- b = np .random .randint (color_range [2 ][0 ], color_range [2 ][1 ] + 1 )
60
+ r , g , b = [np .random .randint (color_range [i ][0 ], color_range [i ][1 ] + 1 ) for i in range (3 )]
130
61
131
62
if isinstance (im , Image .Image ):
132
63
im = np .array (im )
@@ -137,15 +68,14 @@ def add_random_color_background(im, color_range):
137
68
alpha = (np .expand_dims (im [:, :, 3 ], axis = 2 ) == 0 ).astype (np .float )
138
69
im = im [:, :, :3 ]
139
70
bg_color = np .array ([[[r , g , b ]]])
140
- return alpha * bg_color + im
71
+ return alpha * bg_color + ( 1 - alpha ) * im
141
72
142
73
143
74
def test (fn ):
144
75
import matplotlib .pyplot as plt
145
76
cfg .TRAIN .RANDOM_CROP = True
146
- cfg .TRAIN .HUE_CHANGE = True
147
77
im = Image .open (fn )
148
- im = np .asarray (im )[:, :, 0 :3 ]
149
- imt = image_transform (im , 50 , 50 )
78
+ im = np .asarray (im )[:, :, :3 ]
79
+ imt = image_transform (im , 10 , 10 )
150
80
plt .imshow (imt )
151
81
plt .show ()
0 commit comments