Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,617 Bytes
af44a4b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
import numpy as np
import cv2
from .helper import gen_lensmask
def brightness_brighten_shfit_HSV(img, severity=1):
"""
The RGB image is mapping to HSV, and then enhance the brightness by V channel
severity=[1,2,3,4,5] is corresponding to c=[0.1, 0.2, 0.3, 0.4, 0.5]
@param img: Input image, H x W x RGB, value range [0, 255]
@param severity: Severity of distortion, [1, 5]
@return: Degraded image, H x W x RGB, value range [0, 255]
"""
c = [0.1, 0.2, 0.3, 0.4, 0.5][severity-1]
img = np.float32(np.array(img) / 255.)
img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
img_hsv[:, :, 2] += c
img_lq = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
return np.uint8(np.clip(img_lq, 0, 1) * 255.)
def brightness_brighten_shfit_RGB(img, severity=1):
"""
The RGB image is directly enhanced by RGB mean shift
severity=[1,2,3,4,5] is corresponding to c=[0.1, 0.15, 0.2, 0.27, 0.35]
@param img: Input image, H x W x RGB, value range [0, 255]
@param severity: Severity of distortion, [1, 5]
@return: Degraded image, H x W x RGB, value range [0, 255]
"""
c = [0.1, 0.15, 0.2, 0.27, 0.35][severity-1]
img = np.float32(np.array(img) / 255.)
img_lq = img + c
return np.uint8(np.clip(img_lq, 0, 1) * 255.)
def brightness_brighten_gamma_RGB(img, severity=1):
"""
The RGB image is enhanced by V channel with a gamma function
severity=[1,2,3,4,5] is corresponding to gamma=[0.8, 0.7, 0.6, 0.45, 0.3]
@param img: Input image, H x W x RGB, value range [0, 255]
@param severity: Severity of distortion, [1, 5]
@return: Degraded image, H x W x RGB, value range [0, 255]
"""
gamma = [0.8, 0.7, 0.6, 0.45, 0.3][severity-1]
img = np.array(img / 255.)
img_lq = img ** gamma
return np.uint8(np.clip(img_lq, 0, 1) * 255.)
def brightness_brighten_gamma_HSV(img, severity=1):
"""
The RGB image is enhanced by V channel with a gamma function
severity=[1,2,3,4,5] is corresponding to gamma=[0.7, 0.55, 0.4, 0.25, 0.1]
@param img: Input image, H x W x RGB, value range [0, 255]
@param severity: Severity of distortion, [1, 5]
@return: Degraded image, H x W x RGB, value range [0, 255]
"""
gamma = [0.7, 0.58, 0.47, 0.36, 0.25][severity-1]
img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
img_hsv = np.array(img_hsv / 255.)
img_hsv[:, :, 2] = img_hsv[:, :, 2] ** gamma
img_lq = np.uint8(np.clip(img_hsv, 0, 1) * 255.)
img_lq = cv2.cvtColor(img_lq, cv2.COLOR_HSV2RGB)
return img_lq
def brightness_darken_shfit_HSV(img, severity=1):
"""
The RGB image is mapping to HSV, and then darken the brightness by V channel
severity=[1,2,3,4,5] is corresponding to c=[0.1, 0.2, 0.3, 0.4, 0.5]
@param img: Input image, H x W x RGB, value range [0, 255]
@param severity: Severity of distortion, [1, 5]
@return: Degraded image, H x W x RGB, value range [0, 255]
"""
c = [0.1, 0.2, 0.3, 0.4, 0.5][severity-1]
img = np.float32(np.array(img) / 255.)
img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
img_hsv[:, :, 2] -= c
img_lq = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
return np.uint8(np.clip(img_lq, 0, 1) * 255.)
def brightness_darken_shfit_RGB(img, severity=1):
"""
The RGB image's brightness is directly reduced by RGB mean shift
severity=[1,2,3,4,5] is corresponding to c=[0.1, 0.15, 0.2, 0.27, 0.35]
@param img: Input image, H x W x RGB, value range [0, 255]
@param severity: Severity of distortion, [1, 5]
@return: Degraded image, H x W x RGB, value range [0, 255]
"""
c = [0.1, 0.15, 0.2, 0.27, 0.35][severity-1]
img = np.float32(np.array(img)/255.)
img_lq = img - c
return np.uint8(np.clip(img_lq, 0, 1) * 255.)
def brightness_darken_gamma_RGB(img, severity=1):
"""
The RGB image is darkened by V channel with a gamma function
severity=[1,2,3,4,5] is corresponding to gamma=[1.4, 1.7, 2.1, 2.6, 3.2]
@param img: Input image, H x W x RGB, value range [0, 255]
@param severity: Severity of distortion, [1, 5]
@return: Degraded image, H x W x RGB, value range [0, 255]
"""
gamma = [1.4, 1.7, 2.1, 2.6, 3.2][severity-1]
img = np.array(img / 255.)
img_lq = img ** gamma
return np.uint8(np.clip(img_lq, 0, 1) * 255.)
def brightness_darken_gamma_HSV(img, severity=1):
"""
The RGB image is enhanced by V channel with a gamma function
severity=[1,2,3,4,5] is corresponding to gamma=[1.5, 1.8, 2.2, 2.7, 3.5]
@param img: Input image, H x W x RGB, value range [0, 255]
@param severity: Severity of distortion, [1, 5]
@return: Degraded image, H x W x RGB, value range [0, 255]
"""
gamma = [1.5, 1.8, 2.2, 2.7, 3.5][severity-1]
img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
img_hsv = np.array(img_hsv / 255.)
img_hsv[:, :, 2] = img_hsv[:, :, 2] ** gamma
img_lq = np.uint8(np.clip(img_hsv, 0, 1) * 255.)
img_lq = cv2.cvtColor(img_lq, cv2.COLOR_HSV2RGB)
return img_lq
def brightness_vignette(img, severity=1):
"""
The RGB image is suffered from the vignette effect.
severity=[1,2,3,4,5] is corresponding to gamma=[0.5, 0.875, 1.25, 1.625, 2]
@param img: Input image, H x W x RGB, value range [0, 255]
@param severity: Severity of distortion, [1, 5]
@return: Degraded image, H x W x RGB, value range [0, 255]
"""
gamma = [0.5, 0.875, 1.25, 1.625, 2][severity - 1]
img = np.array(img)
h, w = img.shape[:2]
mask = gen_lensmask(h, w, gamma=gamma)[:, :, None]
img_lq = mask * img
return np.uint8(np.clip(img_lq, 0, 255))
|