我有一张 Gray8 图片。 例如,从左到右从黑到白,但在真实的项目中,有真实的图片。 我的最终目标是以 255 的橙白色像素绘制,并以 Gray8 值绘制较少的其他像素,仅用于显示。
他是我所有测试的样本 Xaml 代码
private FormatConvertedBitmap ApplyLut(List<Color> lut, BitmapSource source)
{
FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source = source;
BitmapPalette myPalette = new BitmapPalette(lut);
newFormatedBitmapSource.DestinationPalette = myPalette;
if (lut.Count < 6)
{
// If y try Indexed1, Indexed2, or Indexed4 it's the same result,
// and for Index8, it seems that nothing his apply
newFormatedBitmapSource.DestinationFormat = PixelFormats.Indexed4;
}
else
{
// for 256 colors list, previous PixelsFormat doesn't work
// and for Index8, it seems that nothing his apply
newFormatedBitmapSource.DestinationFormat = PixelFormats.Indexed8;
}
newFormatedBitmapSource.EndInit();
return newFormatedBitmapSource;
}
}
左侧第一张图像是原始图像,第二张测试使用 2 种颜色,下一张测试使用 5 种颜色,最后测试使用 256 种颜色。
我读过很多主题, Stackoverflow 主题 Stackoverflow 主题 微软文档 微软文档 和其他一些但不理解一些事情。
首先,据我了解,LUT 是一个数组,或者是一个将灰度值(0 到 255)与 RGB 值相关联的字典。 如果是这样,为什么在我的 2 种颜色样本上,深色值是橙色,透明值是蓝色? 为什么5种颜色的样品有混合的? 这很好,但不是我想要的。
第二,为什么256色样本,似乎什么都没有发生?
第三,如何将橙色应用于 255 的像素?
您可以在这里检索原始图像https://www.cjoint.com/c/NDtrfpLR4uS
我希望 [0, 254] 之间的灰色像素保持其值,并且当显示图片时,255 处的像素显示为橙色。
如果有人知道如何使用 emgu 做到这一点,我也很感兴趣。
谢谢
您不想创建
FormatConvertedBitmap
,而是创建具有相同原始像素缓冲区但不同 BitmapSource
的 BitmapPalette
。
假设源位图的
Format
为 Indexed8
(如示例位图)或 Gray8
,您可以直接从原始缓冲区的副本创建一个新的 BitmapSource,如下所示:
var colors = Enumerable.Range(0, 255)
.Select(i => Color.FromRgb((byte)i, (byte)i, (byte)i))
.ToList();
colors.Add(Colors.Orange);
var palette = new BitmapPalette(colors);
var width = source.PixelWidth;
var height = source.PixelHeight;
var buffer = new byte[width * height];
source.CopyPixels(buffer, width, 0);
var result = BitmapSource.Create(
width, height, 96, 96, PixelFormats.Indexed8, palette, buffer, width);
结果: