C# – Color Image Quantization in .NET

cemgucvgdi+image processingnet

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.

Best Answer

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.