import cv2
import numpy as np
import os
from pathlib import Path
def augment_hsv(image, hgain=0.1, sgain=0.9, vgain=0.9):#最大区间
# HSV color-space augmentation
if hgain or sgain or vgain:
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
hue, sat, val = cv2.split(cv2.cvtColor(image, cv2.COLOR_BGR2HSV))
dtype = image.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)
# print(r)
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=image) # no return needed
image_dst = cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR)
return image_dst
else:
return image
'''
run:python test_hsv.py
'''
if __name__ == '__main__':
# "它是什么颜色(H)?","颜色深不深(S)?","亮不亮(V)?"
image = cv2.imread('/xdqs-00030_1_xmin2882ymin2693xmax3036ymax2880.jpg')
save_img = r"/custom_yolov5_augimg_test/saveimg/"
# rad = np.random.uniform(size=10)
# print(Path(image).name)
for idx in range(0, 200):
# cv2.imshow('org_img.jpg', image)
img_hsv = augment_hsv(image, hgain=0.009, sgain=0.22, vgain=0.4)
cv2.imwrite(save_img+'img_hsv: %d' % idx+'.jpg', img_hsv)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
调节图片目标SHV参数测试python3
于 2022-03-28 17:39:33 首次发布