Super-Fast Color Transfer Algorithm
The Algorithm
The algorithm that I’m going to mention is based on the following paper:
In this paper, Reinhard and colleagues demonstrate that by utilizing the L a b* color space and the mean and standard deviation of each L*, a*, and b* channel, respectively, that the color can be transferred between two images.
The algorithm performs the following:
- Input a source and a target image. The source image contains the color space that you want your target image to mimic.
- Convert both the source and the target image to the Lab* color space. The Lab* color space models perceptual uniformity, where a small change in an amount of color value should also produce a relatively equal change in color importance. The Lab* color space does a substantially better job mimicking how humans interpret color than the standard RGB color space, and as you’ll see, works very well for color transfer.
- Split the channels for both the source and target.
- Compute the mean and standard deviation of each of the Lab* channels for the source and target images.
- Subtract the mean of the Lab* channels of the target image from target channels.
- Scale the target channels by the ratio of the standard deviation of the target divided by the standard deviation of the source, multiplied by the target channels.
- Add in the means of the Lab* channels for the source.
- Clip any values that fall outside the range [0, 255].
- Merge the channels back together.
- Convert back to the RGB color space from the Lab* space.
The Code
Make sure to have OpenCV for Python installed before running this code:
The Results
Image to transfer colors from:
Image to transfer colors onto:
The resultant image:
Originally published at https://blog.codezest.in on February 6, 2018.