From 992d8af24216bf9884ab41913f18d65c9def4b56 Mon Sep 17 00:00:00 2001 From: Josh Veitch-Michaelis Date: Tue, 28 Apr 2020 20:59:44 +0100 Subject: [PATCH] faster hsv augmentation (#1110) As per https://github.com/ultralytics/yolov3/issues/1096 --- utils/datasets.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/utils/datasets.py b/utils/datasets.py index 4fc33e87..3ea42031 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -514,9 +514,16 @@ def load_image(self, index): def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): - x = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains - img_hsv = (cv2.cvtColor(img, cv2.COLOR_BGR2HSV) * x).clip(None, 255).astype(np.uint8) - np.clip(img_hsv[:, :, 0], None, 179, out=img_hsv[:, :, 0]) # inplace hue clip (0 - 179 deg) + r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains + hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) + dtype = img.dtype # uint8 + + x = np.arange(0, 256, dtype=np.int16) + lut_hue = ((x * r[0]) % 180).astype(dtype) + lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) + lut_val = np.clip(x * r[2], 0, 255).astype(dtype) + + img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype) cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed # Histogram equalization