有没有一种快速的方法来找到一种给定颜色的最接近的调色板颜色索引? [英] Is there any fast way to find the nearest pallette color index of one given color?

查看:100
本文介绍了有没有一种快速的方法来找到一种给定颜色的最接近的调色板颜色索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
这些天,我正在用图像做一些事情.

现在我面临的问题是,有什么快速的方法可以找到一种给定颜色的最接近的调色板颜色索引?

例如,给您一种颜色A和一种颜色的调色板B,该调色板包含许多颜色,您应该在B中找到最接近颜色A的一种颜色,然后改用该颜色.

所以最基本的方式就像是波纹管
对于调色板中的每种颜色D,我们得到它,并使用公式
计算D和A的距离
d =(Ra-Rd)^ 2 +(Ga-Gd)^ 2 +(Ba-Bd)^ 2(Ra,Rd,Ga,Gd,Ba,Bd表示红色,绿色,红色A和颜色D)

并找到最小的d,那么我们可以找到最接近的颜色.但是如果调色板有很多颜色,则速度会很慢.

有什么更快的方法吗?

Hello,
These days, I''m doing something with image.

Now the problem I faced is that is there any fast way to find the nearest pallette color index of one given color?

Such as give you a color A, and one color pallete B, the pallette contains many colors, you should find one color in B that is cloest to color A, and then use this color instead.

So the most basic way is like bellow
For each color D in the pallette, we get it and calc the distance of D and A using formula

d = (Ra-Rd)^2 + (Ga-Gd)^2 + (Ba-Bd)^2 (Ra,Rd,Ga,Gd,Ba,Bd means Red,Green,Red of color A and color D)

And find the smallest d, then we can find the nearest color. But if the pallette has many colors, the speed is very slow.

Is there any faster way?

推荐答案

使用调色板或索引配色方案在当今非常异乎寻常,大多数图形卡,打印机和其他设备可以很好地呈现像素的全像素格式.每像素24位或更多.所以,我很好奇您为什么会需要它.您确定您确实需要吗?

如果您确实需要提高您描述的过程的性能,则该补救措施可能对于搜索的性能问题非常典型.这正在使用一些冗余( http://en.wikipedia.org/wiki/Redundancy_ %28engineering%29 [ ^ ] )和一些缓存算法( http://en.wikipedia.org/wiki/Caching_algorithm [ ^ ]).

您可以创建一些中间的查找表,用于通过一两个步骤缩小搜索范围.这是一个简单的示例:您可以将所有RGB颜色空间划分为较小的多维数据集.可以使用记录RStart, GStart, BStart, Size定位多维数据集;您可以使所有多维数据集具有相同的大小,因此Size将是一个常量,并且结构将只是索引IndexR, IndexG, IndexB.可以一步计算出每种输入颜色都属于某个多维数据集.要在调色板中查找点,请以字典System.Collections.Generic.Dictionary<CubeIndex, PalettPointList>的形式创建一个查找结构,其中CubeIndex是上述多维数据集的索引;和PalettPointList = System.Collections.Generic.Dictionary<Color>应该列出进入列表的所有调色板点.您应按照每个多维数据集至少包含一个调色板点的方式来选择多维数据集的大小,但每个多维数据集中不应包含太多调色板点.如果调色板非常不均匀,则可以通过使用非恒定的多维数据集大小和/或使用平行六面体代替多维数据集,或者可以通过在一个层次结构中引入更多的多维数据集来优化查找结构.

您需要使用一组调色板和算法来弄清楚我们使缓存机制接近最佳状态并满足您的性能要求的方法.

—SA
Using palettes or Indexed color scheme is pretty exotic these days where most of graphic cards, printers and other devices nicely render full pixel formats of 24 bits per pixel or more. So, I''m curious why you may need it. Are you sure you really need it?

If you really need to improve performance of the process you describe, the remedy could be very typical for performance problem of the search. This is using some redundancy (http://en.wikipedia.org/wiki/Redundancy_%28engineering%29[^]) and some caching algorithm (http://en.wikipedia.org/wiki/Caching_algorithm[^]).

You can create some intermediate lookup table used to narrow down the search in one or two steps. Here is a simple example: you can divide all the RGB color space in smaller cubes. The cube can be positioned using the record RStart, GStart, BStart, Size; you can make all cubes of the same size, so Size will be a constant, and the structure will be just index IndexR, IndexG, IndexB. Each input color can be calculated as belonging to some cube in one step. To find a point in a palette, create a lookup structure in the form of dictionary System.Collections.Generic.Dictionary<CubeIndex, PalettPointList>, where CubeIndex is the index if the cube described above; and PalettPointList = System.Collections.Generic.Dictionary<Color> should list all the palette points getting into the list. You should select the size of the cube the way each cube contained at least one palette point, but not too many palette points in each cube. If the palette is very non-uniform, you can possibly optimize the lookup structure by using non-constant cube size and/or using parallelepipeds instead of cubes or possibly by introducing more them one level of hierarchy.

You need to play with the set of your palettes and the algorithm to figure our the way to get caching mechanism close to the optimum and meed your performance requirements.

—SA


这篇关于有没有一种快速的方法来找到一种给定颜色的最接近的调色板颜色索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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