快速获取图像主色的方法 [英] Fast way of getting the dominant color of an image

查看:37
本文介绍了快速获取图像主色的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

[编者注:以下段落经过编辑以适应 Mike Fairhurst 的评论.]

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

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

这篇关于快速获取图像主色的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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