Calculate distance between colors in HSV space

I intend to find the metric of the distance between two colors in HSV space.

Suppose each color element has 3 components: hue, saturation, and value. Hue varies from 0 to 360, saturation varies from 0 to 1, and the value varies from 0 to 255.

Hue also has a circular property, for example, 359 in hue is closer to 0 in hue value than 10 in hue.

Can someone provide a good metric for calculating the distance between 2 color elements in HSV space?

+8
source share
2 answers

First a short warning: Calculating the distance of colors does not make sense (in most cases). Without looking at the results of 50 years of research at Colorimetry , things like the CIECAM02 color space or the perceptual linearity of distance measures, the result of such distance measure will contradict each other. Colors that are “similar” according to your distance measure will look “very different” to the viewer, and other colors that have a large “distance” will not be available to viewers. However ... sub>


The actual question, apparently, focuses mainly on the “Hue” part, which is a value from 0 to 360. And in fact, the values ​​0 and 360 are the same - they both represent “red”, as shown in this image:

Hue

Now the calculation of the difference between these two values ​​is reduced to calculating the distance of two points on a circle with a circle of 360. You already know that the values ​​are strictly in the range [0.360). If you did not know this, you will need to use the Flo-Point Modulo Operation to bring them into this range.

Then you can calculate the distance between these hue values, h0 and h1 , as

 hueDistance = min(abs(h1-h0), 360-abs(h1-h0)); 

Imagine this, how to draw both points on a circle, and select the smaller "piece of cake" that they describe, that is, the distance between them either clockwise or counterclockwise.


EDIT Extended for comment:

  • Hue elements are in the range [0,360]. Using the above formula, you can calculate the distance between two shades. This distance is in the range [0.180]. Dividing the distance by 180.0 will result in a value of [0.1]

  • Saturation elements are in the range [0,1]. The (absolute) difference between the two saturations will also be in the range [0,1].

  • The "Value" elements are in the range [0.255]. Thus, the absolute difference between the two values ​​will be in the range [0.255]. Dividing this difference by 255.0 will result in the value in [0,1].

So imagine you have two sets of HSV. Name them (h0,s0,v0) and (h1,s1,v1) . Then you can calculate the distances as follows:

 dh = min(abs(h1-h0), 360-abs(h1-h0)) / 180.0 ds = abs(s1-s0) dv = abs(v1-v0) / 255.0 

Each of these values ​​will be in the range [0,1]. You can calculate the length of this tuple:

 distance = sqrt(dh*dh+ds*ds+dv*dv) 

and this distance will be metric for the HSV space.

+12
source

The indicated hsv values, normalized to the ranges [0, 2pi], [0, 1], [0, 1], this formula will project the colors into the HSV cone and give you the square (Cartesian) distance in this cone:

  ( sin(h1)*s1*v1 - sin(h2)*s2*v2 )^2 + ( cos(h1)*s1*v1 - cos(h2)*s2*v2 )^2 + ( v1 - v2 )^2 
+3
source

Source: https://habr.com/ru/post/1247616/


All Articles