得到的图像的主色的快速的方式图像、快速、方式

2023-09-11 01:48:37 作者:聽這一季的憂傷

我有一个关于如何获得的图像(照片)的主色的问题。我认为这个算法:遍历所有像素,并得到他们的颜色,红色,绿色,黄色,橙色,蓝色,洋红,青色,白色,灰色或黑色(当然是有一定的余量)和它的黑暗(光线暗或正常)事后检查哪些颜色最发生。我认为这是缓慢的,不是很precise。有没有更好的办法?

I have a question about how to get the dominant color of an image (a photo). I thought of this algorithm: loop through all pixels and get their color, either red, green, yellow, orange, blue, magenta, cyan, white, grey or black (with some margin of course) and it's darkness (light, dark or normal) and afterwards check which colors occurred the most. I think this is slow and not very precise. Is there a better way?

如果它很重要,它是从iPhone或iPod touch摄像头,最多为5 MPX采取一个UIImage。它要快的原因是简要表示进度指示器没有多大的意义,因为这是对人的视力不好,或根本没有看见可言的应用程序。因为它是用于移动设备,也可以不采取非常存储器(至多50 MB)的

If it matters, it's a UIImage taken from an iPhone or iPod touch camera which is at most 5 Mpx. The reason it has to be fast is that simply showing a progress indicator doesn't make very much sense as this is for an app for people with bad sight, or no sight at all. Because it's for a mobile device, it may not take very much memory (at most 50 MB).

推荐答案

您的一般方法应该可行,但我想强调一些细节。

Your general approach should work, but I'd highlight some details.

相反的颜色您给列表中,产生了一批在彩色光谱来计算像素色垃圾箱。下面是有一些算法是另一个问题:如何产生的频谱调色板使容器的数量可配置的,这样你就可以在实验中得到你想要的结果。

Instead of your given list of colors, generate a number of color "bins" in the color spectrum to count pixels. Here's another question that has some algorithms for that: How to generate spectrum color palettes Make the number of bins configurable, so you can experiment to get the results you want.

接下来,为您考虑每一个像素,你需要找到最近色宾递增。你需要定义最近;看到这篇文章的色差:http://en.wikipedia.org/wiki/Color_difference

Next, for each pixel you're considering, you need to find the "nearest" color bin to increment. You'll need to define "nearest"; see this article on "color difference": http://en.wikipedia.org/wiki/Color_difference

有关性能,你并不需要看每个像素。由于图像元素通常是大面积覆盖(如天空,草地等),就可以得到你想要的只有采样几个像素的结果。我猜,你可以得到很好的效果采样每10个像素,甚至每100。您可以与该因素试验为好。

For performance, you don't need to look at every pixel. Since image elements usually cover large areas (e.g. the sky, grass, etc.), you can get the result you want by only sampling a few pixels. I'd guess that you could get good results sampling every 10th pixel, or even every 100th. You can experiment with that factor as well.

最后,你的问题的标题说,平均,但我抗拒的冲动实际平均像素。这可以给你一个惊人的结果。例如,见得到的平均颜色在这个演示: http://jsfiddle.net/xLF38/

Finally, your question title says "average", but I'd resist the urge to actually average pixels. That can give you a surprising result. For example, see the resulting average color in this demo: http://jsfiddle.net/xLF38/