调节图片目标SHV参数测试python3

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕容洛凝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值