Commit 0423fe15 authored by Philipp Werner's avatar Philipp Werner Committed by Yuxin Wu

Fixes bug with floating point images in imgaug.Hue (#547)

* Fixes bug in imgaug.Hue

Hue range of cv2.cvtColor() is 0-360 for floating point images (and 0-180 for 8-bit images), see
https://docs.opencv.org/3.2.0/de/d25/imgproc_color_conversions.html#color_convert_rgb_hsv.

* fix lint

* add link about opencv hue range
parent bf683cd2
......@@ -18,7 +18,7 @@ class Hue(ImageAugmentor):
def __init__(self, range=(0, 180), rgb=None):
"""
Args:
range(list or tuple): hue range
range(list or tuple): range from which the applied hue offset is selected (maximum [-90,90] or [0,180])
rgb (bool): whether input is RGB or BGR.
"""
super(Hue, self).__init__()
......@@ -34,9 +34,13 @@ class Hue(ImageAugmentor):
def _augment(self, img, hue):
m = cv2.COLOR_BGR2HSV if not self.rgb else cv2.COLOR_RGB2HSV
hsv = cv2.cvtColor(img, m)
# Note, OpenCV used 0-179 degree instead of 0-359 degree
# https://docs.opencv.org/3.2.0/de/d25/imgproc_color_conversions.html#color_convert_rgb_hsv
if hsv.dtype.itemsize == 1:
# OpenCV uses 0-179 for 8-bit images
hsv[..., 0] = (hsv[..., 0] + hue) % 180
else:
# OpenCV uses 0-360 for floating point images
hsv[..., 0] = (hsv[..., 0] + 2 * hue) % 360
m = cv2.COLOR_HSV2BGR if not self.rgb else cv2.COLOR_HSV2RGB
img = cv2.cvtColor(hsv, m)
return img
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment