.NET 中的彩色图像量化
我想减少 C# 中位图的唯一颜色的数量。
我想这样做的原因是,最初使用三种颜色创建的图像,但由于许多因素(包括压缩)现在具有超过三种颜色(即相邻像素相互影响)
知道如何做到这一点吗?
该解决方案可能是将整个位图从 RGB 转换为索引颜色系统,或者是一些可以应用于单个像素的函数。
任何 GDI+ 或 Emgu (opencv) 解决方案都适合我。
I want to reduce the number of unique colors of a bitmap in c#.
The reason I want to do this is that an image which is initially created with three color but due to many factors (including compression) has now more than three colors (i.e neighbour pixels has affected each other)
Any idea of how to do that?
The solution maybe something to convert the whole bitmap from RGB to Indexed color system or some function that can be applied to a single pixel.
Any GDI+ or Emgu (opencv) solutions are good for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请访问 http://nquant.codeplex 查看 nQuant .com。这比 Magnus 引用的 MSDN 文章中的代码质量要高得多。它还考虑了 Alpha 层,而 msdn 文章仅评估 RGB。源代码可用,并且附带 博客文章,详细讨论了代码和算法。
Check out nQuant at http://nquant.codeplex.com. This yields much higher quality than the code in the MSDN article that Magnus references. It also takes the Alpha layer into consideration while the msdn article only evaluates RGB. Source code is available and there is an accompanying blog post that discusses the code and algorithm in detail.
msdn 上有一篇文章,名为优化 ASP.NET 图像的颜色量化这可能对您有帮助,它有很好的 示例代码。
There is an article on msdn called Optimizing Color Quantization for ASP.NET Images that might help you, it has good example code.
我刚刚偶然发现了这个问题,虽然这是一个相当老的问题,但也许提到去年我做了我的 绘图库公共(NuGet),它也恰好支持量化。
如果您有
Bitmap
实例,则量化如下简单:原始位图:
使用自定义 8 色调色板和银色背景的量化位图(在此调色板中显示为白色):
在上面的示例中,我使用了
FromCustomPalette
方法,但还有许多其他预定义的方法PredefinedColorsQuantizer
和 OptimizedPaletteQuantizer 类(有关图像和代码示例,请参阅成员)。由于减少颜色可能会严重影响结果的质量,因此您可能需要在量化中使用抖动:
即使使用相同的颜色,差异也相当显着:
您会在
OrderedDitherer
,ErrorDiffusionDitherer
,RandomNoiseDitherer
和 InterleavedGradientNoiseDitherer 类。要在应用程序中尝试可能的内置量化器和抖动器,您可以使用我的成像工具 应用程序。在链接中,您还可以找到其源代码,其中提供了一些更高级的示例,包括可取消的异步转换和进度跟踪等。
I've just stumbled upon this question and though it is quite an old one maybe it still can be useful to mention that last year I made my Drawing Libraries public (NuGet), which happens to support quantization, too.
If you have a
Bitmap
instance, quantization is as simple as follows:Original bitmap:
Quantized bitmap using a custom 8 colors palette and silver background (which appears white with this palette):
In the example above I used the
FromCustomPalette
method but there are many other predefined quantizers available in thePredefinedColorsQuantizer
and OptimizedPaletteQuantizer classes (see the members for image and code examples).And since reducing colors may severely affect the quality of the result you might want to use dithering with the quantization:
The difference is quite significant, even though the same colors are used:
You will find a lot of image examples in the description of the
OrderedDitherer
,ErrorDiffusionDitherer
,RandomNoiseDitherer
and InterleavedGradientNoiseDitherer classes.To try the possible built-in quantizers and ditherers in an application you can use my Imaging Tools app. In the link you can find also its source, which provides a bit more advanced examples with cancellable async conversions with progress tracking, etc.