Navigation Icon

Delta E (CIE 2000)

CIE2000 color difference formula is a perceptually weighted aglorithm that calculates the distance between two colors in the LAB colorspace. The shorter the distance, the more closely the two colors match. It is computationally more expensive than using the standard Euclidean distance calculation in LAB but offers a modest improvement.

The difference is subtle, but in this example you see where the CIE2000 algo shifts the greens more.

picture

Source

picture

LAB Distance

picture

CIE2000 Distance

picture

Palette Image


While experimenting with these color difference formulas, some time was spent trying to speed them up when using Python. This script implements a brute-force approach (using Python arrays and nested for-loops), a Numpy approach, a JobLib + Numpy approach and finally a pytorch (cuda) implementation. Each of which can calculate either the straight-forward LAB distance calculation... $$\Delta E = \sqrt{(L_1 - L_2)^2 + (a_1 - a_2)^2 + (b_1 - b_2)^2}$$ ...or the CIE2000 calculation. The program takes a source image and a palette image as inputs, and then attempts to map the source to the palette by comparing every pixel in the source to every pixel in the palette and choosing the color with the shortest distance.

In a typical case, the brute-force method took 45mins, the Numpy method took 7.5mins, the JobLib/Numpy method took 2.5mins and pytorch took 2.5secs (the clear winner).



Code

The code can found here.

usage: decie2000.py [-h] -i INPUT -p PALETTE -o OUTPUT [-j JOBS] [-b] [-l]

Replaces colors in input using matched colors from palette.

options:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        The input file to process.
  -p PALETTE, --palette PALETTE
                        The palette file.
  -o OUTPUT, --output OUTPUT
                        The output file to save the result.
  -j JOBS, --jobs JOBS  Number of jobs to run in parallel.
  -b, --bruteforce      Run without numpy.
  -l, --labonly         Uses Euclidean distance (faster) instead of CIE2000 algo.


Notes

As an alternative, you can use Imagemagick's convert command to remap an image using the simpler calculation with dithering and perhaps a few other heuristics to get decent results (below).

Make a palette image:

convert <input>.png -colors 512 -unique-colors <palette>.png

Remap an image to the palette:

convert <input>.png -colorspace LAB +dither -remap <palette>.png <output>.png

For more information about the CIE2000 formula, Bruce Lindbloom's site has everything you'd need to know about color difference calculations in LAB/XYZ colorspaces.