给定一个RGB值,这将是找到最匹配的数据库的最佳方法是什么? [英] Given an RGB value what would be the best way to find the closest match in the database?

查看:140
本文介绍了给定一个RGB值,这将是找到最匹配的数据库的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RGB值,如果它没有颜色表存在于我的数据库,我需要找到最接近的颜色。我想比较所有值,并找到差异(红,绿,蓝),然后取平均值。最低的平均偏差应该是最接近的颜色。似乎对我来说,应该有一个更好的办法。任何想法?

I have a rgb value and if it doesn't exist in the color table in my database I need to find the closest color. I was thinking of comparing all values and finding the difference(in red,green,and blue) then take the average. The lowest average deviation should be the closest color. There seems to me like there should be a better way. Any ideas?

推荐答案

考虑颜色在3维空间中的向量,就可以轻松地通过使用3D毕达哥拉斯计算的区别:

Consider a color as a vector in 3-dimensional space, you can then easily compute the difference by using 3d pythagoras:

d = sqrt((r2-r1)^2 + (g2-g1)^2 + (b2-b1)^2)

不过,需要注意的是,由于颜色受制于除pretation通过不那么完美的眼睛,你可能需要调整颜色,以避免它们具有相同的重要性。

However, note that due to colors being subject to interpretation by not-so-perfect eyes, you might want to adjust the colors to avoid them having the same importance.

例如,使用一个典型的加权方法

d = sqrt(((r2-r1)*0.3)^2 + ((g2-g1)*0.59)^2 + ((b2-b1)*0.11)^2)

由于眼睛最敏感的绿色,和最不敏感的蓝色,两种颜色只相差在蓝色分量因此必须有一个较大的数值差被认为是更不同的比一个是相同的数字差在绿色成分。

Since eyes are most sensitive to green, and least sensitive to blue, two colors that differ only in the blue component must thus have a larger numeric difference to be considered "more different" than one that is the same numeric difference in the green component.

还有各种各样的方法来优化这个计算。例如,因为你不是在实际真正感兴趣ð的价值,你可以用平方根分配:

There's also various ways to optimize this calculation. For instance, since you're not really interested in the actual d value, you can dispense with the square root:

d =   ((r2-r1)*0.30)^2
    + ((g2-g1)*0.59)^2
    + ((b2-b1)*0.11)^2

请注意这里的许多C语法为基础的编程语言(如C#), ^ 并不意味着提高到的力量,而是二元独家或者

Note here that in many C-syntax-based programming languages (like C#), ^ does not mean "raise to the power of", but rather "binary exclusive or".

因此​​,如果这是C#中,你可以使用 Math.Pow 来计算的那部分,或者只是扩大和做乘法。

So if this was C#, you would use Math.Pow to calculate that part, or just expand and do the multiplication.

添加:通过维基百科 色差的页面来看,有不同的标准,尝试处理感知差异。例如,一个叫CIE94使用不同的配方,在 L * C * H 颜色模型,看起来像它是值得研究的,但是这取决于你想如何准确它是

Added: Judging by the page on Color difference on Wikipedia, there's various standards that try to handle perceptual differences. For instance, the one called CIE94 uses a different formula, in the L*C*h color model that looks like it's worth looking into, but it depends on how accurate you want it to be.

这篇关于给定一个RGB值,这将是找到最匹配的数据库的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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