计算HSV空间中颜色之间的距离 [英] Calculate distance between colors in HSV space

查看:1085
本文介绍了计算HSV空间中颜色之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在HSV空间中找到两种颜色之间的距离度量.

I intend to find a distance metric between two colours in HSV space.

假设每个颜色元素具有3个成分:色相,饱和度和值.色相介于0到360之间,饱和度介于0到1之间,值介于0到255之间.

Suppose that each colour element has 3 components: hue, saturation, and value. Hue is ranged between 0 to 360, saturation is ranged between 0 to 1, and value is ranged between 0 to 255.

色相也具有圆形属性,例如,色相中的359色相比10色更接近0.

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

有人可以在这里提供一个很好的指标来计算HSV空间中2个颜色元素之间的距离吗?

Can anyone provide a good metric to calculate the distance between 2 colour element in HSV space here?

推荐答案

首先一个简短警告:计算颜色距离没有道理(在大多数情况下).如果不考虑比色法中50年研究的结果,类似

First a short warning: Computing the distance of colors does not make sense (in most cases). Without considering the results of 50 years of research in Colorimetry, things like the CIECAM02 Color Space or perceptual linearity of distance measures, the result of such a distance measure will be counterintuitive. Colors that are "similar" according to your distance measure will appear "very different" to a viewer, and other colors, that have a large "distance" will be undistinguishable by viewers. However...

实际问题似乎主要针对色相"部分,它是介于0和360之间的值.实际上,0和360的值是相同的-它们都代表红色",如图所示.这张图片:

The actual question seems to aim mainly at the "Hue" part, which is a value between 0 and 360. And in fact, the values of 0 and 360 are the same - they both represent "red", as shown in this image:

现在,计算这两个值的差可归结为计算圆周为360的圆上两个点的距离.您已经知道这些值严格在[0,360]范围内.如果您不知道,则必须使用浮点模运算来实现在这个范围内.

Now, computing the difference of two of these values boils down to computing the distance of two points on a circle with a circumference of 360. You already know that the values are strictly in the range [0,360). If you did not know that, you would have to use the Floating-Point Modulo Operation to bring them into this range.

然后,您可以将这些色调值h0h1之间的距离计算为

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

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

想象一下,将两个点都画在一个圆上,然后拾取它们描述的较小的一块蛋糕",即它们之间的距离以顺时针或逆时针顺序排列.

Imagine this as painting both points on a circle, and picking the smaller "piece of the cake" that they describe - that is, the distance between them either in clockwise or in counterclockwise order.

编辑扩展评论:

EDIT Extended for the comment:

  • 色调"元素的范围为[0,360].使用上面的公式,您可以计算两个色相之间的距离.该距离在[0,180]范围内.将距离除以180.0将得出[0,1]

    • The "Hue" elements are in the range [0,360]. With the above formula, you can compute a distance between two hues. This distance is in the range [0,180]. Dividing the distance by 180.0 will result in a value in [0,1]

      饱和度"元素在[0,1]范围内.两个饱和度之间的(绝对)差也将在[0,1]范围内.

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

      值"元素在[0,255]范围内.因此,两个值之间的绝对差也将在[0,255]范围内.将该差异除以255.0将得出[0,1]中的值.

      The "Value" elements are in the range [0,255]. The absolute difference between two values will thus be in the range [0,255] as well. Dividing this difference by 255.0 will result in a value in [0,1].

      因此,假设您有两个HSV元组.分别称为(h0,s0,v0)(h1,s1,v1).然后,您可以计算出距离,如下所示:

      So imagine you have two HSV tuples. Call them (h0,s0,v0) and (h1,s1,v1). Then you can compute 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
      

      每个值都将在[0,1]范围内.您可以计算该元组的长度:

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

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

      ,此距离将是HSV空间的指标.

      and this distance will be a metric for the HSV space.

      这篇关于计算HSV空间中颜色之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆