Monday, September 12, 2011

The typical Weighted Euclidean color distance values are wrong.

There's a rather common set of weights for Euclidean Color Distance, to better approximate the human eye.

(30*(r1-r2))**2 + (59*(g1-g2))**2 + (11*(b1-b2))**2

30, 59, and 11 for RGB weights.

Well, they seemed rather pulled out of one's ass. And I'm not the first to notice. But, I did bother to code up a routine to compare CIELab to color distance weights and find out, by brute forcing each and every color distance to find out where the weights there should be.

22.216091748149788, 42.88860259783791, 34.895305654012304

Or 22, 43, 35 since we're very likely going with integer math. This is just brute force average best approximation of weighted Euclidean to CIELab.

From the cited Color metric article:

  • Several individuals suggested a weighted Euclidean distance in R'G'B', according to the formula:
    This function has practically the same result as YUV. Its simplicity and speed of calculation make it a better choice than YUV.
  • As explained in the section "gamma correction" below, the perception of brightness by the human eye is non-linear. From the experiments it appears that the curve for this non-linearity is not the same for each colour. The weighted Euclidean distance presented works quite well for the subset of colours where the "red" signal is 128 or more (on a scale of 0-255). For the other half of the full R'G'B' cube, this different weighting produced better results:

 2, 4, 3, are pretty close to my values (they are weights and thus need to be proportional to each other not similar in absolute magnitude). 2/9, 4/9, 3/9 or 22.22, 44.44, 33.33 when you bring the weights up to how many parts I have. So 22.22 vs 22.21...  44.4 vs 42.8 ... 33.3 vs 34.9.

I also ran the colors for CIELuv.

R G B weights:
0.2753667361197665, 0.3846815980739518, 0.3399516658062818

28, 38, 34


Kay said...

I'm not even going to attempt this one.

MarcV said...

The weights are for 1.0 gamma?

Tatarize said...

The wrong weights are. The 30, 59, and 11 for RGB weights are how much each color component contributes to gamma. In fact a lot of color spaces that use gamma will produce it with exactly those weights. The problem is the question people typically want with the weighted distribution is a slightly better than Euclidean metric for determining how far apart different colors are from each other. You'll run into those numbers to try to make the color distance metrics a bit better, and all they'll do is give gamma a huge role to play as a substitute for actual color distance.

Anonymous said...

Interesting, thanks for sharing. Thinking about it, a crude approximation that I've used in the past isn't even that far off:

d1 * d1 + (d2 * d2 << 2) + (d3 * d3 << 1)

The weights are (1, 4, 2) and their square roots are (1, 2, 1.41) so the ratios roughly correspond :)