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

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

问题描述

我有一个 rgb 值,如果它不存在于我的数据库的颜色表中,我需要找到最接近的颜色.我正在考虑比较所有值并找到差异(红色、绿色和蓝色)然后取平均值.最低的平均偏差应该是最接近的颜色.在我看来应该有更好的方法.有什么想法吗?

解决方案

将颜色视为 3 维空间中的向量,然后您可以使用 3d 毕达哥拉斯轻松计算其差异:

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

但是,请注意,由于颜色会被不太完美的眼睛所解读,您可能需要调整颜色以避免它们具有相同的重要性.

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

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

由于眼睛对绿色最敏感,对蓝色最不敏感,因此仅蓝色分量不同的两种颜色必须具有更大的数值差异才能被视为更不同"绿色组件.

还有多种方法可以优化此计算.例如,由于您对实际的 d 值并不真正感兴趣,您可以省去平方根:

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

请注意,在许多基于 C 语法的编程语言(如 C#)中,^ 并不意味着提升的幂",而是二进制异或".

因此,如果这是 C#,您将使用 Math.Pow 来计算该部分,或者只是展开并进行乘法.

添加:根据维基百科色差的页面判断,有各种标准试图处理感知差异.例如,CIE94 使用了不同的公式,在 L*C*h 颜色模型中,看起来值得研究,但这取决于您希望它的准确程度.>

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?

解决方案

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)

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.

For instance, using a typical weighted approach:

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

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".

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

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天全站免登陆