问题描述
我有一个位图,其中包含许多不同的颜色.但是我需要根据此图像设计一种40种或更少颜色的方案.这就是为什么我需要减少位图调色板中颜色数量的方法.
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:
我尝试自己做:获得最近的4或9像素(正方形)&混合它.我也尝试减小位图的大小并拉伸"位图.最后,我试图吸收最接近的颜色.但是我使用上面的方法获得了最好的结果.
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.
这就是为什么我在寻找更好的方法来减少位图调色板中的颜色数量.
P.S.图像按比例缩放,我还在它们之上绘制了一个网格.
P.S. Images were scaled, and I also drew a grid on top of them.
已更新:
我尝试使用 scolorq ,但结果相同.问题仍然有意义.
I tried to use scolorq, but result was the same. Question still relevant.
推荐答案
可以使用此处.它实现了8种算法,并具有大量的选择.
A great app with source code in C# is available here. It implements 8 algorithms and has tons of options.
这篇关于减少位图调色板中颜色数量的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!