ios 快速获取图像主色的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5050250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:57:17  来源:igfitidea点击:

Fast way of getting the dominant color of an image

algorithmimageioscolorsphoto

提问by

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?

我有一个关于如何获得图像(照片)的主色的问题。我想到了这个算法:遍历所有像素并获取它们的颜色,红色、绿色、黄色、橙色、蓝色、洋红色、青色、白色、灰色或黑色(当然有一些边缘),它是黑暗的(亮、暗或正常),然后检查出现最多的颜色。我认为这很慢而且不是很精确。有没有更好的办法?



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).

如果重要的话,它是从 iPhone 或 iPod touch 相机拍摄的最多 5 Mpx 的 UIImage。它必须很快的原因是仅仅显示一个进度指示器并没有多大意义,因为这适用于视力不好或根本没有视力的人的应用程序。因为它是针对移动设备的,所以它可能不会占用太多内存(最多 50 MB)。

采纳答案by payne

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: Generating spectrum color palettesMake the number of bins configurable, so you can experiment to get the results you want.

代替给定的颜色列表,在色谱中生成许多颜色“箱”来计算像素。这是另一个有一些算法的问题:生成光谱调色板使 bin 的数量可配置,以便您可以尝试获得所需的结果。

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

接下来,对于您正在考虑的每个像素,您需要找到“最近”的颜色箱来增加。你需要定义“最近的”;请参阅有关“色差”的这篇文章:http: //en.wikipedia.org/wiki/Color_difference

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.

为了性能,您不需要查看每个像素。由于图像元素通常覆盖较大的区域(例如,天空、草地等),因此您只需对几个像素进行采样即可获得所需的结果。我猜你可以每 10 个像素,甚至每 100 个像素进行一次采样,得到很好的结果。您也可以尝试使用该因素。

[Editor's note: The paragraph below was edited to accommodate Mike Fairhurst's comment.]

[编者注:编辑了以下段落以适应 Mike Fairhurst 的评论。]

Averaging pixels can also be done, as in this demo:jsfiddle.net/MUsT8/

也可以完成平均像素,如本演示中所示:jsfiddle.net/MUsT8/