python直接在RGB调整饱和度,亮度,对比度

  • 参考https://github.com/CoinCheung/BiSeNet,不用转hsv
import numpy as np
import os, sys, glob, cv2

def adj_saturation(im, rate):
	M = np.float32([
            [1+2*rate, 1-rate, 1-rate],
            [1-rate, 1+2*rate, 1-rate],
            [1-rate, 1-rate, 1+2*rate]
        ])
	shape = im.shape
	im = np.matmul(im.reshape(-1, 3), M).reshape(shape)/3
	im = np.clip(im, 0, 255).astype(np.uint8)
	return im

def adj_brightness(im, rate):
	table = np.array([i * rate for i in range(256)]).clip(0, 255).astype(np.uint8)
	return table[im]

def adj_contrast(im, rate):
	table = np.array([74 + (i - 74) * rate for i in range(256)]).clip(0, 255).astype(np.uint8)
	return table[im]
	
a=[0.6, 1.4]
rate = np.random.uniform(*a)
print(rate)
rate = np.random.uniform(*a)
print(rate)
rate = np.random.uniform(*a)
print(rate)
im=cv2.imread(sys.argv[1])
cv2.imshow('raw', im)
num=int(sys.argv[2])
if num == 1: im = adj_saturation(im, rate)
elif num == 2: im = adj_brightness(im, rate)
else: im = adj_contrast(im, rate)
cv2.imshow('adj', im)
cv2.waitKey()

你可能感兴趣的:(小功能,opencv,python)