寻找最接近的 RGB 颜色
有人告诉我使用距离公式来查找颜色是否与其他颜色匹配,所以我得到了,
struct RGB_SPACE
{
float R, G, B;
};
RGB_SPACE p = (255, 164, 32); //pre-defined
RGB_SPACE u = (192, 35, 111); //user defined
long distance = static_cast<long>(pow(u.R - p.R, 2) + pow(u.G - p.G, 2) + pow(u.B - p.B, 2));
这仅给出了一个距离,但我如何知道颜色是否与用户定义的颜色匹配至少 25%?
我不仅不确定,而且有一个想法,检查每个颜色值,看看差异是否为 25%。例如。
float R = u.R/p.R * 100;
float G = u.G/p.G * 100;
float B = u.B/p.B * 100;
if (R <= 25 && G <= 25 && B <= 25)
{
//color matches with pre-defined color.
}
I was told to use distance formula to find if the color matches the other one so I have,
struct RGB_SPACE
{
float R, G, B;
};
RGB_SPACE p = (255, 164, 32); //pre-defined
RGB_SPACE u = (192, 35, 111); //user defined
long distance = static_cast<long>(pow(u.R - p.R, 2) + pow(u.G - p.G, 2) + pow(u.B - p.B, 2));
this gives just a distance, but how would i know if the color matches the user-defined by at least 25%?
I'm not just sure but I have an idea to check each color value to see if the difference is 25%. for example.
float R = u.R/p.R * 100;
float G = u.G/p.G * 100;
float B = u.B/p.B * 100;
if (R <= 25 && G <= 25 && B <= 25)
{
//color matches with pre-defined color.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议不要检查 RGB 空间。如果你有 (0,0,0) 和 (100,0,0),根据 cababungas 公式,它们是相似的(以及根据卡萨布兰卡公式,它认为太多颜色相似)。然而,它们看起来很不同。
HSL 和 HSV 颜色模型基于人类对颜色的解释,然后您可以轻松指定距离色调、饱和度和亮度彼此独立(取决于“相似”在您的情况下的含义)。
I would suggest not to check in RGB space. If you have (0,0,0) and (100,0,0) they are similar according to cababungas formula (as well as according to casablanca's which considers too many colors similar). However, they LOOK pretty different.
The HSL and HSV color models are based on human interpretation of colors and you can then easily specify a distance for hue, saturation and brightness independently of each other (depending on what "similar" means in your case).
“匹配率至少为 25%”并不是一个明确定义的问题。匹配率至少为 25%,根据什么指标?有很多可能的选择。如果比较 RGB 颜色,最明显的是从向量范数导出的距离度量。最重要的三个是:
当然,还有很多其他可能性。您可以检查它们是否彼此相距一定距离:如果您希望在一个颜色通道中允许最多 25% 的差异(超出可能的 RGB 值的范围),则这 3 种方法使用的阈值是 3/4分别为 *255、sqrt(3)/4*255 和 255/4。但这是一个非常粗略的指标。
测量颜色之间距离的更好方法是将颜色转换为感知均匀的颜色空间,例如 CIELAB并在那里进行比较;关于这个主题也有一篇相当不错的维基百科文章。根据您的预期应用,这可能有点过分了,但在这些颜色空间中,测量的距离与人类视觉系统感知的距离具有最佳的相关性。
"Matches by at least 25%" is not a well-defined problem. Matches by at least 25% of what, and according to what metric? There's tons of possible choices. If you compare RGB colors, the obvious ones are distance metrics derived from vector norms. The three most important ones are:
There's lots of other possibilities, of course. You can check if they're within some distance of each other: If you want to allow up to 25% difference (over the range of possible RGB values) in one color channel, the thresholds to use for the 3 methods are 3/4*255, sqrt(3)/4*255 and 255/4, respectively. This is a very coarse metric though.
A better way to measure distances between colors is to convert your colors to a perceptually uniform color space like CIELAB and do the comparison there; there's a fairly good Wikipedia article on the subject, too. That might be overkill depending on your intended application, but those are the color spaces where measured distances have the best correlation with distances perceived by the human visual system.
请注意,最大可能距离介于 (255, 255, 255) 和 (0, 0, 0) 之间,距离为
3 * 255^2
。显然,这两种颜色匹配度最低(0% 匹配),并且它们的距离为 100%。那么至少 25% 匹配意味着距离小于 75%,即3 / 4 * 3 * 255^2 = 9 / 4 * 255 * 255
。所以你可以检查是否:Note that the maximum possible distance is between (255, 255, 255) and (0, 0, 0), which are at a distance of
3 * 255^2
. Obviously these two colours match the least (0% match) and they are a distance 100% apart. Then at least a 25% match means a distance less than 75%, i.e.3 / 4 * 3 * 255^2 = 9 / 4 * 255 * 255
. So you could just check if: