2

I have a bitmap which contains a lot of different colors. But I need to make a scheme from this image with 40 colors or less. That's why I need method to reduce quantity of colors in the bitmap palette.

I'm using code below for my app:

        using System.Windows.Media.Imaging; //BitmapSource

        ==================================================

        //Create palette of requied color. Pay heed to maxColorCount it 
        //can have any value, but PixelFormats supports only 2,4,16 and 256 colors.
        var myPalette = new BitmapPalette(ConvertBitmap(b) as BitmapSource, 16);

        newFormatedBitmapSource.DestinationPalette = myPalette;

        //Set PixelFormats
        newFormatedBitmapSource.DestinationFormat = PixelFormats.Indexed8;
        newFormatedBitmapSource.EndInit();

        b = BitmapFromSource(newFormatedBitmapSource);

It works, but very often as a result I recieve an image with bright pixels on a mostly solid background of darker colours:

enter image description here enter image description here

I tried to do it by myself: get nearest 4 or 9 pixels (square) & blend it. Also I tried to reduce bitmap size and "stretch" the bitmap. In the end I was trying to absorb nearest colors. But the best result I got using method above.

That's why I'm looking for better way to reduce quantity of colors in bitmap palette.

P.S. Images were scaled, and I also drew a grid on top of them.

UPDATED:

I tried to use scolorq, but result was the same. Question still relevant.

melvas
  • 2,346
  • 25
  • 29
  • 1
    and your question is? – Daniel A. White Feb 18 '14 at 14:11
  • I'm looking for better way to reduce quantity of colors in bitmap palette – melvas Feb 18 '14 at 14:12
  • 2
    what research have you done? there are a lot of resources out there on how to do that. – Daniel A. White Feb 18 '14 at 14:12
  • I tried to do it by myself: get nearest 4 or 9 pixels (square) & blend it. Also I tried to reduce bitmap size and "stretch" the bitmap. But the best result I got using method above. – melvas Feb 18 '14 at 14:19
  • In the end I was trying to absorb nearest colors. – melvas Feb 18 '14 at 14:24
  • 1
    A very good utility is the scolorq: A Spatial Color Quantization Utility. The site has a source code(in c++ of cource for speed). More more details [scolorq: A Spatial Color Quantization Utility](http://www.cs.berkeley.edu/~dcoetzee/downloads/scolorq/#sampleimages). – γηράσκω δ' αεί πολλά διδασκόμε Feb 19 '14 at 10:00
  • valter, thanks a lot for your comment, but it looks like scolorq gives the same result as the wpf function above :( But I'll check it out as soon as I can. – melvas Feb 19 '14 at 10:12
  • Native code that implements this algorithm can be found here: http://www.codeproject.com/Articles/1433/CQuantizer Try it with your images. You can use it as is using ,NET-native interop, or rewrite in C#. – Alex F Nov 05 '14 at 09:33
  • thanks for your comment, Alex. I'll try it as soon as I can – melvas Nov 05 '14 at 14:39

1 Answers1

4

A great app with source code in C# is available here. It implements 8 algorithms and has tons of options.

Eldarien
  • 813
  • 6
  • 10