"舍入"颜色值到最近的一个小组颜色颜色、小组、最近、QUOT

2023-09-10 23:12:51 作者:靓妹乔治.

preamble

作为一个项目,我的工作,我想提供搜索我们的系统映像的便捷方式的一部分。我们目前提供由各种类型的用户添加的元数据(例如标题,描述,关键字),并通过各种元数据,我们提取(例如EXIF,IPTC,XMP,等等)进行搜索。我还要添加一个颜色搜索类似你可以在谷歌的图片搜索看看。

As a part of a project I'm working on I am trying to provide a convenient way to search for images in our system. We currently provide searching by various types of user added metadata (e.g. title, description, keywords) and by various metadata which we extract (e.g. EXIF, IPTC, XMP, etc). I would also like to add a "colour search" similar to what you can see in google's image search.

该项目采用PHP,我们可以使用ImageMagick的扩展段和量化的图像,并从图像中提取最显著的色彩;我不能完全肯定的结果,我到达这里的,但他们似乎相当准确,肯定比没有好。

The project uses PHP and we can use the Imagemagick extension to segment and quantize the image and extract the most "significant" colours from the image; I'm not entirely certain of the results I'm getting here, but they seem reasonably accurate and certainly better than nothing.

问题

这是我在困难上转换这些显著的颜色为一组重presentative的颜色,如该位当你看Google's图片搜索有一组12种颜色存在。我想数学圆我的颜色值最接近重presentative颜色,这样我可以索引图像,我发现,然后面我的搜索结果这样的颜色。

The bit that I'm having difficulty on is converting these significant colours into a set of representative colours, e.g. when you look at Google's image search there is a set of 12 colours there. I'd like to mathematically "round" my colour value to the nearest representative colour, so that I can index the image with the colours that I detect and then facet my search results that way.

有什么建议?

推荐答案

第一步是定义要比较的颜色。

The first step would be to define the colors you want to compare to.

第二步是找到你的颜色的最小距离为您在previous一步中选择一种颜色。为了能够测量需要在其中建模颜色的欧几里得空间距离。

The second step is to find the smallest distance from your color to one of the colors you chose in the previous step. In order to be able to measure that distance you need an Euclidian space in which to model colors.

。当然,简单的选择是将RGB空间

Of course the simple choice would be to the RGB space

与C 1 两种颜色之间的距离(r 1 ,G 1 ,B 1 )和ç 2 (R 2 ,G 2 ,B 2 )将

And the distance between two colors C1(r1, g1, b1) and C2(r2, g2, b2) would be

的sqrt((R 1 - R的 2 ) 2 +(G 1 - 克 2 ) 2 +(B 1 - B 2 ) 2 )

sqrt( (r1 - r2)2 + (g1 - g2)2 + (b1 - b2)2 ).

但是,如果你需要更多的precision这将是最好使用色调,色度,亮度双锥的空间,HSL缸的衍生物。

But if you need more precision it would be better to use the Hue-Chroma-Lightness bicone space, a derivative of the HSL cylinder.

在RGB空间事情直线前进的R,G和B,其中每一个单独的轴。在HCL我们需要计算的坐标上的每个轴

In the RGB space things were straight forward as R, G and B where each on a separate axis. In HCL we need to compute the coordinates on each of the axis.

首先我们计算色度(这是一个有点不同,饱和度)为:

First of all we compute the chroma (which is a bit different from saturation) as:

色度= MAX(红,绿,蓝) - 分(红,绿,蓝)

然后我们正常化我们的H,C和L值,从而使得H从0到2(以覆盖一个圆,如果我们乘以PI和采取弧度为单位),C变为从0到1的(半径三角圈)和L变为从-1(黑色)到1(白)

Then we normalize our H, C and L value so that H goes from 0 to 2 (to cover a circle if we multiply by PI and take radians as the unit), C goes from 0 to 1 (the radius of the trigonometric circle) and L goes from -1 (Black) to 1 (White).

接下来我们的以Z = L 无需任何转换,因为它是从它会沿着垂直轴图像清晰。

Next we take z = L without any transformations as it is clear from the image that it goes along the vertical axis.

我们可以很容易地观察到,对于一种颜色,色度是从z轴的距离和色调是角度。所以,我们得到

We can easily observe that for a color, Chroma is the distance from the z axis and Hue is the angle. So we get

X = C * COS(H * PI)和 Y = C *罪(H * PI)

目前点x,y和z都将在[-1,1]和两种颜色之间的距离将是,使用相同的配方如上述,

At this point x, y and z will all be in [-1, 1] and the distance between two colors will be, using the same formula as above,

的sqrt((X 1 - X 2 ) 2 +(Y 1 - ÿ 2 ) 2 +(Z 1 - z 2 ) 2 )

sqrt( (x1 - x2)2 + (y1 - y2)2 + (z1 - z2)2 ).

要根据人的色彩感觉变得更加precision并找到最接近的颜色,你可以使用 CIE -L * AB 的造型空间和计算距离,这些算法的。的原理是一样的,作为上述psented两种情况$ P $,只有算法是更复杂的

To get even more precision and find the closest color according to human color perception you could use the CIE-L*ab modeling space and compute the distance with one of these algorithms. The principles are the same as for the two cases presented above, only the algorithms are more complex.