|
import cv2 |
|
import numpy as np |
|
|
|
|
|
|
|
def HWC3(x): |
|
assert x.dtype == np.uint8 |
|
if x.ndim == 2: |
|
x = x[:, :, None] |
|
assert x.ndim == 3 |
|
H, W, C = x.shape |
|
assert C == 1 or C == 3 or C == 4 |
|
if C == 3: |
|
return x |
|
if C == 1: |
|
return np.concatenate([x, x, x], axis=2) |
|
if C == 4: |
|
color = x[:, :, 0:3].astype(np.float32) |
|
alpha = x[:, :, 3:4].astype(np.float32) / 255.0 |
|
y = color * alpha + 255.0 * (1.0 - alpha) |
|
y = y.clip(0, 255).astype(np.uint8) |
|
return y |
|
|
|
|
|
def resize_image(input_image, resolution): |
|
H, W, C = input_image.shape |
|
H = float(H) |
|
W = float(W) |
|
k = float(resolution) / min(H, W) |
|
H *= k |
|
W *= k |
|
H = int(np.round(H / 64.0)) * 64 |
|
W = int(np.round(W / 64.0)) * 64 |
|
img = cv2.resize(input_image, (W, H), interpolation=cv2.INTER_LANCZOS4 if k > 1 else cv2.INTER_AREA) |
|
return img |
|
|
|
def apply_color(image, color_map): |
|
image = cv2.cvtColor(image, cv2.COLOR_RGB2LAB) |
|
color_map = cv2.cvtColor(color_map, cv2.COLOR_RGB2LAB) |
|
|
|
l, _, _ = cv2.split(image) |
|
_, a, b = cv2.split(color_map) |
|
|
|
merged = cv2.merge([l, a, b]) |
|
|
|
result = cv2.cvtColor(merged, cv2.COLOR_LAB2RGB) |
|
return result |
|
|