|
import cv2 |
|
import numpy as np |
|
import math |
|
|
|
|
|
def control_HSV(img, h_deg, s_mag, v_mag): |
|
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) |
|
print(h_deg, s_mag, v_mag) |
|
|
|
img_hsv[:,:,(0)] = img_hsv[:,:,(0)] + h_deg |
|
img_hsv[:,:,(1)] = img_hsv[:,:,(1)] * s_mag |
|
img_hsv[:,:,(2)] = img_hsv[:,:,(2)] * v_mag |
|
|
|
img_bgr = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR) |
|
return img_bgr |
|
|
|
|
|
def control_contrast(img, contrast): |
|
|
|
factor = (259 *(contrast + 255)) / (255 *(259 - contrast)) |
|
|
|
newImage = np.array(img, dtype = 'float64') |
|
|
|
newImage = np.clip((newImage[:,:,:] - 128) * factor + 128, 0, 255) |
|
|
|
newImage = np.array(newImage, dtype = 'uint8') |
|
|
|
return newImage |
|
|
|
|
|
def __clamp(value: float, min_val: int = 0, max_val: int = 255) -> int: |
|
|
|
return int(round(max(min(value, max_val), min_val))) |
|
|
|
|
|
def kelvin_to_rgb(kelvin): |
|
temperature = kelvin / 100.0 |
|
|
|
if temperature < 66.0: |
|
red = 255 |
|
else: |
|
|
|
|
|
|
|
|
|
|
|
red = temperature - 55.0 |
|
red = 351.97690566805693 + 0.114206453784165 * red - 40.25366309332127 * math.log(red) |
|
|
|
|
|
if temperature < 66.0: |
|
|
|
|
|
|
|
|
|
|
|
green = temperature - 2 |
|
green = -155.25485562709179 - 0.44596950469579133 * green + 104.49216199393888 * math.log(green) |
|
else: |
|
|
|
|
|
|
|
|
|
|
|
green = temperature - 50.0 |
|
green = 325.4494125711974 + 0.07943456536662342 * green - 28.0852963507957 * math.log(green) |
|
|
|
|
|
if temperature >= 66.0: |
|
blue = 255 |
|
elif temperature <= 20.0: |
|
blue = 0 |
|
else: |
|
|
|
|
|
|
|
|
|
|
|
blue = temperature - 10 |
|
blue = -254.76935184120902 + 0.8274096064007395 * blue + 115.67994401066147 * math.log(blue) |
|
|
|
return __clamp(red, 0, 255), __clamp(blue, 0, 255), __clamp(green, 0, 255) |
|
|
|
def control_kelvin(img, kelvin): |
|
r, g, b = kelvin_to_rgb(int(kelvin)) |
|
color_matrix = (r / 255.0, 0.0, 0.0, 0.0, 0.0, g / 255.0, 0.0, 0.0, 0.0, 0.0, b / 255.0, 0.0) |
|
new_img = img.convert('RGB', color_matrix) |
|
|
|
return new_img |
|
|