开发者

What is the best algorithm for finding the closest color in an array to another color? [closed]

开发者 https://www.devze.com 2022-12-11 00:45 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.

I have a color (RGB) being read from a sensor. I also have a list of "known" colors, each paired with a string name.

What would the best way (ie behave like a human choosing colors) to pull the name of the nearest color out of this开发者_运维百科 list?

I've tried a shortest cartesian distance with RGB, but that makes grey closer to green than to black or white.


Rather than using RGB, try using an HSL (Hue, Saturation, Lightness) or HSV (Hue, Saturation and Value) color model. Then experiment with different elements of bias, e.g. hue being more important than luminance when you're calculating the distance.


Jon Skeet is right. You need to use a system with Hue as a component instead of RGB if you are concerned about the color component of the match differing too much. HSL or HSV will both work fine for this purpose.

Then you need to fiddle with the distance formula to weight up hue until you are happy with the results. Note that you will find the problem is actually essentially insoluble unless you have a large number of colors to match against or your input colors are confined to a small range of possible values. This is because although it might seem you can pin any color you want to one of 8 (red, orange yellow, green, blue, violet, black and white) or one of 16, in reality you'll find your algorithm will always find what seem to be obviously incorrect matches because with 3 axis of movement (hue, saturation, value or red, green, blue), there are a lot more "basic" colors than you might think at first glance.


I would think that if you treat colors as RGB coordinates in 3-space and compute distance from sampled to known values, you could determine the closest match. I probably would also try scaling R G B according to eye sensitivity (ie, Y = 0.3*R + 0.59*G + 0.11*B) you'd achieve the best result

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号