Python - 在图像中查找优势/最常见的颜色 [英] Python - Find dominant/most common color in an image

查看:284
本文介绍了Python - 在图像中查找优势/最常见的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来找到图像中使用python最主要的颜色/色调。平均阴影或最常见的RGB将做。我查看了Python Imaging库,但没有找到任何与他们手册中的内容相关的信息,也没有找到任何与VTK相关的信息。

I'm looking for a way to find the most dominant color/tone in an image using python. Either the average shade or the most common out of RGB will do. I've looked at the Python Imaging library, and could not find anything relating to what I was looking for in their manual, and also briefly at VTK.

找到一个PHP脚本,它需要什么,此处(需要登录才能下载)。脚本似乎将图像调整为150 * 150,以显示主色。然而,在那之后,我相当失落。我确实考虑写一些东西,将图像缩放到一个小的大小,然后检查每个其他像素,所以它的图像,虽然我认为这将是非常低效的(虽然实现这个想法作为一个C Python模块可能是一个想法)。

I did however find a PHP script which does what I need, here (login required to download). The script seems to resize the image to 150*150, to bring out the dominant colors. However, after that, I am fairly lost. I did consider writing something that would resize the image to a small size then check every other pixel or so for it's image, though I imagine this would be very inefficient (though implementing this idea as a C python module might be an idea).

但是,毕竟,我还是很累。所以我转向你,SO。

However, after all of that, I am still stumped. So I turn to you, SO. Is there an easy, efficient way to find the dominant color in an image.

推荐答案

这里是使用 PIL Scipy的群集软件包

为了简单起见,我将文件名硬编码为image.jpg。调整图像大小是为了速度:如果你不介意等待,注释掉resize调用。当在此蓝辣椒的示例图像上运行时,通常会说主色是#d8c865 ,其大致对应于两个辣椒的左下方的亮黄色区域。我说通常是因为使用的聚类算法具有一定程度的随机性。有多种方法可以改变这一点,但出于你的目的,它可能适合。 (如果您需要确定性结果,请查看kmeans2()变量上的选项。)

For simplicity I've hardcoded the filename as "image.jpg". Resizing the image is for speed: if you don't mind the wait, comment out the resize call. When run on this sample image of blue peppers it usually says the dominant colour is #d8c865, which corresponds roughly to the bright yellowish area to the lower left of the two peppers. I say "usually" because the clustering algorithm used has a degree of randomness to it. There are various ways you could change this, but for your purposes it may suit well. (Check out the options on the kmeans2() variant if you need deterministic results.)

import struct
import Image
import scipy
import scipy.misc
import scipy.cluster

NUM_CLUSTERS = 5

print 'reading image'
im = Image.open('image.jpg')
im = im.resize((150, 150))      # optional, to reduce time
ar = scipy.misc.fromimage(im)
shape = ar.shape
ar = ar.reshape(scipy.product(shape[:2]), shape[2])

print 'finding clusters'
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
print 'cluster centres:\n', codes

vecs, dist = scipy.cluster.vq.vq(ar, codes)         # assign codes
counts, bins = scipy.histogram(vecs, len(codes))    # count occurrences

index_max = scipy.argmax(counts)                    # find most frequent
peak = codes[index_max]
colour = ''.join(chr(c) for c in peak).encode('hex')
print 'most frequent is %s (#%s)' % (peak, colour)

注意:当我扩展集群数量从5至10或15,它经常得到的结果是绿色或蓝色的。给定输入图像,这些是合理的结果...我不能告诉哪个颜色在该图像中占主导地位,所以我不会错误的算法!

Note: when I expand the number of clusters to find from 5 to 10 or 15, it frequently gave results that were greenish or bluish. Given the input image, those are reasonable results too... I can't tell which colour is really dominant in that image either, so I don't fault the algorithm!

也有一个小的奖励:保存缩小的图像只有最常见的颜色:

Also a small bonus: save the reduced-size image with only the N most-frequent colours:

# bonus: save image using only the N most common colours
c = ar.copy()
for i, code in enumerate(codes):
    c[scipy.r_[scipy.where(vecs==i)],:] = code
scipy.misc.imsave('clusters.png', c.reshape(*shape))
print 'saved clustered image'

这篇关于Python - 在图像中查找优势/最常见的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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