Skip to content

pengbo-learn/python-color-transfer

Repository files navigation

Color Transfer in Python

Three methods of color transfer implemented in Python.

Output Examples

Input image Reference image Mean std transfer Lab mean transfer Pdf transfer + Regrain
example1_input example1_ref example1_mt example1_lt example1_pdf-reg
example2_input example2_ref example2_mt example2_lt example2_pdf-reg
example1_input example1_ref example1_mt example1_lt example1_pdf-reg
example1_input example1_ref example1_mt example1_lt example1_pdf-reg

Methods

Let input image be $I$, reference image be $R$ and output image be $O$. Let $f{I}(r, g, b)$, $f{R}(r, g, b)$ be probability density functions of $I$ and $R$'s rgb values.

  • Mean std transfer

    $$O = (I - mean(I)) / std(I) * std(R) + mean(R).$$

  • Lab mean transfer1

    $$I' = rgb2lab(I)$$ $$R' = rgb2lab(R)$$ $$O' = (I' - mean(I')) / std(I') * std(R') + mean(R')$$ $$O = lab2rgb(O')$$

  • Pdf transfer2

    $O = t(I)$, where $t: R^3\rightarrow R^3$ is a continous mapping so that $f{t(I)}(r, g, b) = f{R}(r, g, b)$.

Requirements

Installation

From PyPi

pip install python-color-transfer

From source

git clone https://github.com/pengbo-learn/python-color-transfer.git
cd python-color-transfer

pip install -r requirements.txt

Demo

python demo.py 
Output
demo_images/house.jpeg: 512x768x3
demo_images/hats.png: 512x768x3
Pdf transfer time: 1.47s
Regrain time: 1.16s
Mean std transfer time: 0.09s
Lab Mean std transfer time: 0.09s
Saved to demo_images/house_display.png

demo_images/fallingwater.png: 727x483x3
demo_images/autumn.jpg: 727x1000x3
Pdf transfer time: 1.87s
Regrain time: 0.87s
Mean std transfer time: 0.12s
Lab Mean std transfer time: 0.11s
Saved to demo_images/fallingwater_display.png

demo_images/tower.jpeg: 743x1280x3
demo_images/sunset.jpg: 743x1114x3
Pdf transfer time: 2.95s
Regrain time: 2.83s
Mean std transfer time: 0.23s
Lab Mean std transfer time: 0.21s
Saved to demo_images/tower_display.png
  
demo_images/scotland_house.png: 361x481x3
demo_images/scotland_plain.png: 361x481x3
Pdf transfer time: 0.67s
Regrain time: 0.49s
Mean std transfer time: 0.04s
Lab Mean std transfer time: 0.22s
Saved to demo_images/scotland_display.png

Usage

from pathlib import Path

import cv2
from python_color_transfer.color_transfer import ColorTransfer

# Using demo images
input_image = 'demo_images/house.jpeg'
ref_image = 'demo_images/hats.png'

# input image and reference image
img_arr_in = cv2.imread(input_image)
img_arr_ref = cv2.imread(ref_image)

# Initialize the class
PT = ColorTransfer()

# Pdf transfer
img_arr_pdf_reg = PT.pdf_transfer(img_arr_in=img_arr_in,
                                  img_arr_ref=img_arr_ref,
                                  regrain=True)
# Mean std transfer
img_arr_mt = PT.mean_std_transfer(img_arr_in=img_arr_in,
                                  img_arr_ref=img_arr_ref)
# Lab mean transfer
img_arr_lt = PT.lab_transfer(img_arr_in=img_arr_in, img_arr_ref=img_arr_ref)

# Save the example results
img_name = Path(input_image).stem
for method, img in [('pdf-reg', img_arr_pdf_reg), ('mt', img_arr_mt),
                   ('lt', img_arr_lt)]:
    cv2.imwrite(f'{img_name}_{method}.jpg', img)

Footnotes

  1. Lab mean transfer: Color Transfer between Images by Erik Reinhard, Michael Ashikhmin, Bruce Gooch and Peter Shirley.
    Open source's python implementation

  2. Pdf transfer: Automated colour grading using colour distribution transfer by F. Pitie , A. Kokaram and R. Dahyot.
    Author's matlab implementation