Skip to content

Commit 7be5de6

Browse files
committed
update illumination
1 parent bf8960d commit 7be5de6

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

tensorlayer/prepro.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -877,39 +877,56 @@ def brightness_multi(x, gamma=1, gain=1, is_random=False):
877877
return np.asarray(results)
878878

879879
# illumination
880-
def illumination(x, gamma=1, contrast=1, saturation=1, is_random=False):
880+
def illumination(x, gamma=1., contrast=1., saturation=1., is_random=False):
881881
"""Perform illumination augmentation for a single image, randomly or non-randomly.
882882
883883
Parameters
884884
-----------
885885
x : numpy array
886886
an image with dimension of [row, col, channel] (default).
887887
gamma : change brightness
888+
- if is_random=False, one float number, small than one means brighter, greater than one means darker.
889+
- if is_random=True, tuple of two float numbers, (min, max).
888890
contrast : change contrast
891+
- if is_random=False, one float number, small than one means blur.
892+
- if is_random=True, tuple of two float numbers, (min, max).
889893
saturation : change saturation
890-
is_random : whether the parameters are randomly set
894+
- if is_random=False, one float number, small than one means unsaturation.
895+
- if is_random=True, tuple of two float numbers, (min, max).
896+
is_random : whether the parameters are randomly set.
897+
898+
Examples
899+
---------
900+
- Random
901+
>>> x = illumination(x, gamma=(0.5, 5.0), contrast=(0.3, 1.0), saturation=(0.7, 1.0), is_random=True)
902+
- Non-random
903+
>>> x = illumination(x, 0.5, 0.6, 0.8, is_random=False)
891904
"""
892905
from PIL import Image, ImageEnhance
893906

894907
if is_random:
908+
try:
909+
assert len(gamma) = len(contrast) == len(saturation), "if is_random = True, the arguments are (min, max)"
910+
except:
911+
raise Exception("if is_random = True, the arguments are (min, max)")
895912
## random change brightness # small --> brighter
896913
illum_settings = np.random.randint(0,3) # 0-brighter, 1-darker, 2 keep normal
897914

898915
if illum_settings == 0: # brighter
899-
gamma = np.random.uniform(.5, 1.0)
916+
gamma = np.random.uniform(gamma[0], 1.0) # (.5, 1.0)
900917
elif illum_settings == 1: # darker
901-
gamma = np.random.uniform(1.0, 5.0)
918+
gamma = np.random.uniform(1.0, gamma[1])# (1.0, 5.0)
902919
else:
903920
gamma = 1
904921
im_ = brightness(x, gamma=gamma, gain=1, is_random=False)
905922

906923
# print("using contrast and saturation")
907924
image = Image.fromarray(im_) # array -> PIL
908925
contrast_adjust = ImageEnhance.Contrast(image)
909-
image = contrast_adjust.enhance(np.random.uniform(0.3,0.9))
926+
image = contrast_adjust.enhance(np.random.uniform(contrast[0], contrast[1]))#0.3,0.9))
910927

911928
saturation_adjust = ImageEnhance.Color(image)
912-
image = saturation_adjust.enhance(np.random.uniform(0.7,1.0))
929+
image = saturation_adjust.enhance(np.random.uniform(saturation[0], saturation[1]))# (0.7,1.0))
913930
im_ = np.array(image) # PIL -> array
914931
else:
915932
im_ = brightness(x, gamma=gamma, gain=1, is_random=False)

0 commit comments

Comments
 (0)