Skip to content

Commit f2bc26b

Browse files
authored
Merge pull request #227 from yzwxx/master
add illumination augmentation
2 parents a0bf7f6 + 8f6481b commit f2bc26b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tensorlayer/prepro.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,50 @@ def body(central_x, i):
17221722
return result
17231723

17241724

1725+
def illumination(x, gamma = 1, contrast=1, saturation=1, is_random=False):
1726+
"""perform illumination augmentation for a single image, randomly or non-randomly.
17251727
1728+
Parameters
1729+
-----------
1730+
x : numpy array
1731+
an image with dimension of [row, col, channel] (default).
1732+
gamma : change brightness
1733+
contrast : change contrast
1734+
saturation : change saturation
1735+
is_random : whether the parameters are randomly set
1736+
"""
1737+
from PIL import Image, ImageEnhance
1738+
1739+
if is_random:
1740+
## random change brightness # small --> brighter
1741+
illum_settings = np.random.randint(0,3) # 0-brighter, 1-darker, 2 keep normal
1742+
1743+
if illum_settings == 0: # brighter
1744+
gamma = np.random.uniform(.5, 1.0)
1745+
elif illum_settings == 1: # darker
1746+
gamma = np.random.uniform(1.0, 5.0)
1747+
else:
1748+
gamma = 1
1749+
im_ = brightness(x, gamma=gamma, gain=1, is_random=False)
1750+
1751+
# print("using contrast and saturation")
1752+
image = Image.fromarray(im_) # array -> PIL
1753+
contrast_adjust = ImageEnhance.Contrast(image)
1754+
image = contrast_adjust.enhance(np.random.uniform(0.3,0.9))
1755+
1756+
saturation_adjust = ImageEnhance.Color(image)
1757+
image = saturation_adjust.enhance(np.random.uniform(0.7,1.0))
1758+
im_ = np.array(image) # PIL -> array
1759+
else:
1760+
im_ = brightness(im_, gamma=gamma, gain=1, is_random=False)
1761+
image = Image.fromarray(im_) # array -> PIL
1762+
contrast_adjust = ImageEnhance.Contrast(image)
1763+
image = contrast_adjust.enhance(contrast)
1764+
1765+
saturation_adjust = ImageEnhance.Color(image)
1766+
image = saturation_adjust.enhance(saturation)
1767+
im_ = np.array(image) # PIL -> array
1768+
return np.asarray(im_)
17261769

17271770

17281771

0 commit comments

Comments
 (0)