HSV模型原理以及利用HSV模型随机增强图像
- 图像HSV模型简介
- RGB模型转HSV模型
- opencv关于HSV模型实验
- 随机增强图像HSV
图像HSV模型简介
RGB模型转HSV模型
opencv关于HSV模型实验
import cv2 import numpy as np rgb = np.array([110, 20, 50], dtype=np.uint8).reshape((1, 1, 3)) hsv = cv2.cvtColor(rgb, cv2.COLOR_RGB2HSV) print(hsv)
import cv2 import numpy as np hue = np.zeros((100, 256, 1), dtype=np.uint8) sat = np.tile(np.arange(0, 256, dtype=np.uint8).reshape((1, 256, 1)), (100, 1, 1)) val = np.ones((100, 256, 1), dtype=np.uint8) * 255 img_hsv = cv2.merge((hue, sat, val)) img = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR) # img = cv2.resize(img, (720, 100)) cv2.imshow("img", img) cv2.waitKey(0)
import cv2 import numpy as np hue = np.zeros((100, 256, 1), dtype=np.uint8) sat = np.ones((100, 256, 1), dtype=np.uint8) * 255 val = np.tile(np.arange(0, 256, dtype=np.uint8).reshape((1, 256, 1)), (100, 1, 1)) img_hsv = cv2.merge((hue, sat, val)) img = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR) # img = cv2.resize(img, (720, 100)) cv2.imshow("img", img) cv2.waitKey(0)
随机增强图像HSV
import cv2 import numpy as np def augment_hsv(img, h_gain=0.5, s_gain=0.5, v_gain=0.5): r = np.random.uniform(-1, 1, 3) * [h_gain, s_gain, v_gain] + 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) aug_img = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR) return aug_img