从图像创建调色板的方法?

发布于 2024-10-07 06:19:44 字数 76 浏览 5 评论 0原文

假设我有一张图像,其颜色深度和尺寸未指定。
有哪些方法可以将其压缩为 n 调色板?
我的 google-fu 对这个很弱。

Say I have an image, of unspecified color depth and dimensions.
What ways are there of compressing this down to an n-color palette?
My google-fu is weak with this one.

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

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

发布评论

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

评论(2

无声静候 2024-10-14 06:19:44

最简单的方法就是查看图像并创建一个将像素颜色映射到整数的字典。对于每个像素,如果其颜色在字典中,则增加其计数。如果不是,请将其添加计数为 1。这将给出每种颜色在图像中出现的次数。

然后,按计数排序,您将找到图像中 256 种最常见的颜色。这些颜色构成了你的调色板。

然后,再次迭代图像。对于每个像素,找到最接近该像素颜色的调色板颜色,并将该像素的索引设置为该调色板索引。

这将是一个很好的“第一步”,但在具有大量颜色的图像中,它可能无法很好地找到调色板。在字典阶段,您可能希望组合“足够接近”的颜色,以避免许多非常相似的颜色都得分很低,即使它们在一起很常见。

为了获得更好的结果,您需要查看抖动技术。

Well the simplest approach is simply to look over the image and create a dictionary mapping pixel colours to ints. For each pixel, if its colour is in the dictionary, increment its count. If it isn't, add it with a count of 1. This gives you the number of times each colour appears in the image.

Then, sort by count and you will find the 256 most-common colours in the image. Those colours make up your palette.

Then, iterate over the image again. For each pixel, find the palette colour which is closest to that pixel's colour, and set that pixel's index to that palette index.

That will be a good "first go", but in images with a lot of colours it might not do such a good job of finding the palette. In the dictionary phase, you may want to combine colours which are "close enough" to avoid having a lot of very similar colours all scoring poorly, even though together they would be very common.

For better results, you will want to look at dithering techniques.

猥琐帝 2024-10-14 06:19:44

该问题称为颜色量化。例如,请参阅 pngquant

如果您正在寻找算法,请搜索 Median Cut、Octtree、K-Means、Linde–Buzo–Gray、NeuQuant。理想情况下是在 Google Scholar 上,因为常规结果会被隐藏的付费专区充斥。

The problem is called color quantization. See pngquant for example.

If you're looking for algorithms, then search for Median Cut, Octtree, K-Means, Linde–Buzo–Gray, NeuQuant. Ideally on Google Scholar, as regular results are spammed by cloaking paywalls.

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