Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
947 views
in Technique[技术] by (71.8m points)

c# - The best way to reduce quantity of colors in bitmap palette

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...