检查颜色相似度的算法

发布于 2024-10-24 09:10:23 字数 367 浏览 4 评论 0原文

我正在寻找一种算法,可以比较两种 RGB 颜色并生成它们的相似度值(其中相似度意味着“与人类平均感知相似”)。

有什么想法吗?

编辑

由于我无法再回答,我决定将我的“解决方案”作为对问题的编辑。

我决定在我的应用程序中使用(非常)小的真彩色子集,以便我可以自己处理颜色比较。我使用大约 30 种颜色,并在它们之间使用硬编码距离。

因为它是一个 iPhone 应用程序,所以我使用 Objective-C,其实现或多或少是一个代表下表的矩阵,它显示了颜色之间的距离。

在此处输入图像描述

I'm looking for an algorithm that compares two RGB colors and generates a value of their similarity (where similarity means "similar with respect to average human perception").

Any ideas?

EDIT:

Since I cannot answer anymore I decided to put my "solution" as an edit to the question.

I decided to go with a (very) small subset of true-color in my app, so that I can handle comparison of colors by my own. I work with about 30 colors and use hard-coded distances between them.

Since it was an iPhone app I worked with objective-C and the implementation is more or less a matrix representing the table below, which shows the distances between the colors.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

爺獨霸怡葒院 2024-10-31 09:10:23

RGB 距离欧几里得空间与“平均人类感知”不太相似。

您可以使用 YUV 色彩空间,它考虑到了这个因素:

 |  Y' |     |  0.299     0.587    0.114   | | R |
 |  U  |  =  | -0.14713  -0.28886  0.436   | | G |
 |  V  |     |  0.615    -0.51499 -0.10001 | | B |

您还可以使用 CIE 用于此目的的色彩空间。

编辑:

我要提到YUV色彩空间是一种廉价的近似值,可以通过简单的公式计算。但它在感知上并不统一。感知均匀意味着颜色值的相同量的变化应该产生大约相同的视觉重要性的变化。
如果您需要更精确和严格的指标,您一定要考虑 CIELAB 色彩空间或其他感知统一的色彩空间空间(即使没有简单的转换公式)。

RGB distance in the euclidean space is not very similar to "average human perception".

You can use YUV color space, it takes into account this factor :

 |  Y' |     |  0.299     0.587    0.114   | | R |
 |  U  |  =  | -0.14713  -0.28886  0.436   | | G |
 |  V  |     |  0.615    -0.51499 -0.10001 | | B |

You can also use the CIE color space for this purpose.

EDIT:

I shall mention that YUV color space is an inexpensive approximation that can be computed via simple formulas. But it is not perceptually uniform. Perceptually uniform means that a change of the same amount in a color value should produce a change of about the same visual importance.
If you need a more precise and rigourous metric you must definitely consider CIELAB color space or an another perceptually uniform space (even if there are no simple formulas for conversion).

自由如风 2024-10-31 09:10:23

我建议使用 CIE94 (DeltaE-1994),据说它可以很好地代表人类颜色感知。我在计算机视觉相关的应用程序中经常使用它,并且我对结果非常满意。

然而,执行这样的比较的计算量相当大:

  1. 两种颜色的 RGB 到 XYZ
  2. XYZ 到 LAB 两种颜色
  3. Diff = DeltaE94(LABColor1,LABColor2)代码>

公式(伪代码):

I would recommend using CIE94 (DeltaE-1994), it's said to be a decent representation of the human color perception. I've used it quite a bit in my computer-vision related applications, and I am rather happy with the result.

It's however rather computational expensive to perform such a comparison:

  1. RGB to XYZ for both colors
  2. XYZ to LAB for both colors
  3. Diff = DeltaE94(LABColor1,LABColor2)

Formulas (pseudocode):

小清晰的声音 2024-10-31 09:10:23

这里有一篇关于颜色距离主题的精彩文章:
http://www.compuphase.com/cmetric.htm

如果该资源消失了作者的结论是可以使用此公式(在 C 代码中)实现两种 RGB 颜色之间距离的最佳低成本近似。

typedef struct {
   unsigned char r, g, b;
} RGB;

double ColourDistance(RGB e1, RGB e2)
{
  long rmean = ( (long)e1.r + (long)e2.r ) / 2;
  long r = (long)e1.r - (long)e2.r;
  long g = (long)e1.g - (long)e2.g;
  long b = (long)e1.b - (long)e2.b;
  return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
}

There's an excellent write up on the subject of colour distances here:
http://www.compuphase.com/cmetric.htm

In case that resource disappears the author's conclusion is that the best low-cost approximation to the distance between two RGB colours can be achieved using this formula (in C code).

typedef struct {
   unsigned char r, g, b;
} RGB;

double ColourDistance(RGB e1, RGB e2)
{
  long rmean = ( (long)e1.r + (long)e2.r ) / 2;
  long r = (long)e1.r - (long)e2.r;
  long g = (long)e1.g - (long)e2.g;
  long b = (long)e1.b - (long)e2.b;
  return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
}
喜爱皱眉﹌ 2024-10-31 09:10:23

人类对色度的感知比对强度的感知弱。

例如,在商业视频中,YCbCr/YPbPr 色彩空间(也称为 Y'UV)会降低色度信息的分辨率,但保留亮度 (Y)。在数字视频压缩中,例如 4:2:0 和 4:2:2,由于感知相对较弱,因此会降低色度比特率。

我相信您可以计算一个距离函数,该距离函数的优先级高于亮度 (Y),而优先级低于色度。

此外,在低强度下,人类视觉几乎是黑白的。因此,优先级函数是非线性的,因为对于低亮度 (Y),您对色度的权重越来越小。

更科学的公式:http://en.wikipedia.org/wiki/Color_difference

Human perception is weaker in chroma than intensity.

For example, in commercial video, the YCbCr/YPbPr color spaces (also called Y'UV) reduces the resolution of the chroma info but preserves the luma (Y). In digital video compression such as 4:2:0 and 4:2:2 reduces the chroma bitrate due to relatively weaker perception.

I believe that you can calculate a distance function giving higher priority over luma (Y) and less priority over chroma.

Also, under low intensity, human vision is practically black-and-white. Therefore, the priority function is non-linear in that for low luma (Y) you put less and less weight on chroma.

More scientific formulas: http://en.wikipedia.org/wiki/Color_difference

怂人 2024-10-31 09:10:23

颜色感知不是欧几里得的。任何距离公式都既足够好,同时又很糟糕。任何基于欧几里得距离(RGB、HSV、Luv、Lab...)的测量对于相似的颜色都足够好,显示水绿色接近青色。但对于非接近值来说,它是任意的。例如,红色更接近绿色还是更接近蓝色?

来自 Charles Poynton 的颜色常见问题解答

XYZ 和 RGB 系统相差甚远
表现出感知的一致性。
找到 XYZ 的变换
合理感知均匀的空间
在 CIE 上花费了十年或更长时间
最终没有一个系统能够
达成一致。

Color perception is not Euclidean. Any distance formula will be both good enough and terrible at the same time. Any measure based on Euclidean distance (RGB, HSV, Luv, Lab, ...) will be good enough for similar colors, showing aqua being close to teal. But for non-close values it gets to be arbitrary. For instance, is red closer to green or to blue?

From Charles Poynton's Color FAQ:

The XYZ and RGB systems are far from
exhibiting perceptual uniformity.
Finding a transformation of XYZ into a
reasonably perceptually-uniform space
consumed a decade or more at the CIE
and in the end no single system could
be agreed.

别想她 2024-10-31 09:10:23

RGB 立方体中的颜色相似度通过欧几里得距离来测量(使用毕达哥拉斯公式)。

编辑:再想一想,对于大多数其他色彩空间来说也应该如此。

Color similarity in the RGB cube is measured by the euclidean distance (use pythagoras formula).

EDIT: On a second thought, this should be true for most other color spaces too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文