mirror of
https://github.com/ultralytics/ultralytics.git
synced 2025-09-15 15:48:41 +08:00
ultralytics 8.3.79 Fix shift in HSV augmentation (#19311)
Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com> Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> Co-authored-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com> Co-authored-by: UltralyticsAssistant <web@ultralytics.com> Co-authored-by: Laughing-q <1185102784@qq.com> Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com> Co-authored-by: JacekMaksymiuk <32569908+JacekMaksymiuk@users.noreply.github.com> Co-authored-by: Ultralytics AI Assistant <135830346+UltralyticsAssistant@users.noreply.github.com> Co-authored-by: Laughing <61612323+Laughing-q@users.noreply.github.com>
This commit is contained in:
parent
e96404c919
commit
c23f68ac67
@ -1,6 +1,6 @@
|
||||
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
||||
|
||||
__version__ = "8.3.78"
|
||||
__version__ = "8.3.79"
|
||||
|
||||
import os
|
||||
|
||||
|
||||
@ -1364,17 +1364,26 @@ class RandomHSV:
|
||||
>>> hsv_augmenter(labels)
|
||||
>>> augmented_img = labels["img"]
|
||||
"""
|
||||
img = labels["img"]
|
||||
if self.hgain or self.sgain or self.vgain:
|
||||
r = np.random.uniform(-1, 1, 3) * [self.hgain, self.sgain, self.vgain] + 1 # random gains
|
||||
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
|
||||
img = labels["img"]
|
||||
dtype = img.dtype # uint8
|
||||
|
||||
x = np.arange(0, 256, dtype=r.dtype)
|
||||
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)
|
||||
# Original implementation (bug) from ultralytics<=8.3.78
|
||||
# r = np.random.uniform(-1, 1, 3) * [self.hgain, self.sgain, self.vgain] + 1 # random gains
|
||||
# x = np.arange(0, 256, dtype=r.dtype)
|
||||
# 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)
|
||||
|
||||
# Fixed implementation in https://github.com/ultralytics/ultralytics/pull/19311
|
||||
r = np.random.uniform(-1, 1, 3) * (self.hgain, self.sgain, self.vgain) * (180, 255, 255) # random gains
|
||||
x = np.arange(0, 256, dtype=r.dtype)
|
||||
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)
|
||||
lut_sat[0] = 0 # prevent pure white changing color
|
||||
|
||||
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
|
||||
im_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val)))
|
||||
cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
|
||||
return labels
|
||||
|
||||
Loading…
Reference in New Issue
Block a user