-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adam Oswald
authored and
huggingface-web
committed
Nov 7, 2022
1 parent
e7af5d9
commit 7a1112b
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import numpy as np | ||
import cv2 as cv | ||
|
||
|
||
def transfer_color(source_image: np.ndarray, target_image: np.ndarray) -> np.ndarray: | ||
"""Color transfer between images | ||
Args: | ||
source_image (np.ndarray): Color source image | ||
target_image (np.ndarray): Target image | ||
Returns: | ||
np.ndarray: The result of the color transfer | ||
Reference: | ||
doi: 10.1109/38.946629 | ||
""" | ||
# RGB -> L*a*b* | ||
src_img = cv.cvtColor(source_image, cv.COLOR_RGB2Lab) | ||
dst_img = cv.cvtColor(target_image, cv.COLOR_RGB2Lab) | ||
|
||
# Calculate mean and std | ||
src_means, src_stds = src_img.mean(axis=(0, 1)), src_img.std(axis=(0, 1)) | ||
dst_means, dst_stds = dst_img.mean(axis=(0, 1)), dst_img.std(axis=(0, 1)) | ||
|
||
# Transfer | ||
dst_img = dst_img - dst_means.reshape((1, 1, 3)) | ||
dst_img *= (dst_stds / src_stds).reshape((1, 1, 3)) | ||
dst_img += src_means.reshape((1, 1, 3)) | ||
|
||
# L*a*b* -> RGB | ||
dst_img = np.clip(dst_img, 0, 255).astype(np.uint8) | ||
dst_img = cv.cvtColor(dst_img, cv.COLOR_LAB2RGB) | ||
return dst_img |