Python中的主颜色检测

发布于 2024-12-10 03:12:07 字数 438 浏览 2 评论 0原文

我有大约 3000 张图像和 13 种不同的颜色(其中大多数图像的背景是白色)。如果图像的主颜色是这 13 种不同颜色之一,我希望将它们关联起来。

我见过类似的问题,例如 使用 python 进行图像颜色检测 要求平均颜色算法。我已经使用 Python 图像库和直方图复制了该代码,并使其正常工作 - 但我发现它对于确定主颜色不太可靠。

有什么想法吗?或者可以解决这个问题的图书馆?

提前致谢!

:编辑: 谢谢大家 - 你们几乎都说了同样的话,创建“桶”并用图像的每个最近像素增加桶计数。我似乎收到很多返回“白色”或“米色”的图像,这也是大多数图像的背景。有没有办法解决或忽略背景?

再次感谢。

I have about 3000 images and 13 different colors (the background of the majority of these images is white). If the main color of an image is one of those 13 different colors, I'd like them to be associated.

I've seen similar questions like Image color detection using python that ask for an average color algorithm. I've pretty much copied that code, using the Python Image Library and histograms, and gotten it to work - but I find that it's not too reliable for determining main colors.

Any ideas? Or libraries that could address this?

Thanks in advance!

:EDIT:
Thanks guys - you all pretty much said the same thing, to create "buckets" and increase the bucket count with each nearest pixel of the image. I seem to be getting a lot of images returning "White" or "Beige," which is also the background on most of these images. Is there a way to work around or ignore the background?

Thanks again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記柔刀 2024-12-17 03:12:07

您可以使用 getcolors 函数获取图像中所有颜色的列表。它返回以下形式的元组列表:

(N, COLOR)

其中 N 是颜色 COLOR 在图像中出现的次数。要获得最大出现的颜色,您可以将列表传递给 max 函数:

>>> from PIL import Image
>>> im = Image.open("test.jpg")
>>> max(im.getcolors(im.size[0]*im.size[1]))
(183, (255, 79, 79))

请注意,我将 im.size[0]*im.size[1] 传递给 getcolors 函数,因为这是最大 maxcolors 值(请参阅文档了解详细信息)。

You can use the getcolors function to get a list of all colors in the image. It returns a list of tuples in the form:

(N, COLOR)

where N is the number of times the color COLOR occurs in the image. To get the maximum occurring color, you can pass the list to the max function:

>>> from PIL import Image
>>> im = Image.open("test.jpg")
>>> max(im.getcolors(im.size[0]*im.size[1]))
(183, (255, 79, 79))

Note that I passed im.size[0]*im.size[1] to the getcolors function because that is the maximum maxcolors value (see the docs for details).

木森分化 2024-12-17 03:12:07

就我个人而言,我会将颜色空间分为 8-16 个主要颜色,然后对于每个像素,我会将最接近的颜色桶增加 1。最后,像素数量最多的桶的颜色获胜。

基本上,考虑中位数而不是平均值。您只关心图像中的颜色,而平均颜色通常会给您一个全新的颜色。

Personally I would split the color space into 8-16 main colors, then for each pixel I'd increment the closest colored bucket by one. At the end the color of the bucket with the highest amount of pixels wins.

Basically, think median instead of average. You only really care about the colors in the image, whereas averaging colors usually gives you a whole new color.

鹤舞 2024-12-17 03:12:07

由于您正在尝试匹配少量预先存在的颜色,因此您可以尝试不同的方法。针对所有颜色测试每张图像,看看哪一个最匹配。

至于进行匹配,我首先将每个图像调整为较小的尺寸,以减少您为每个图像所做的工作量;我们对图像颜色的感知不太依赖于细节的数量。对于较小图像的每个像素,找出 13 种颜色中最接近的一种。如果它在某个阈值内,则为该颜色增加一个计数器。最后,13个人中数最多的人获胜。

Since you're trying to match a small number of preexisting colors, you can try a different approach. Test each image against all of the colors, and see which one is the closest match.

As for doing the match, I'd start by resizing each image to a smaller size to reduce the amount of work you'll be doing for each; our perception of the color of an image isn't too dependent on the amount of detail. For each pixel of the smaller image, find which of the 13 colors is the closest. If it's within some threshold, bump a counter for that color. At the end whichever of the 13 has the highest count is the winner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文