【发布时间】:2016-07-17 06:09:08
【问题描述】:
我正在尝试通过获取所有像素并保存在Colors 列表中来计算给定图像的最佳调色板(对于 gif,最多 256 种颜色)。
var bmp = new WriteableBitmap(bitmapSource);
bmp.Lock();
int stride = bmp.PixelWidth * 4;
int size = bmp.PixelHeight * stride;
var imgData = new byte[size];
//int index = y * stride + 4 * x; //To acess a specific pixel.
Marshal.Copy(bmp.BackBuffer, imgData, 0, imgData.Length);
bmp.Unlock();
var colorList = new List<Color>();
//I'm not sure if this is right.
for (int index = 0; index < imgData.Length - 1; index += 4)
{
colorList.Add(Color.FromArgb(imgData[index], imgData[index + 1],
imgData[index + 2], imgData[index + 3]));
}
//Here is the main problem.
var palette = colorList.Distinct().Take(255);
目前,我能够区分所有颜色,并且只选择前 255 种颜色。但我需要先按用途订购。我该怎么做?
另外,你们还有其他方法吗?
【问题讨论】:
-
作为一个快速的想法,如何使用字典作为键颜色和作为值的 int 来计算该颜色的像素数?
-
我正在考虑这个问题,但我仍然需要计算颜色并在循环内更新
Dictionary。 -
您可能想做一些类似 K 表示聚类的事情,而不是仅仅选择 K 种最常见的颜色。