我需要使用單色相機 API,該 API 在其手冊中宣告如下:
每個像素(10、11 或 12 位)以 16 位編碼。像素值放在 16 位的 LSB 上。
我將使用 WPF/C# 和 Bitmap;或者我可以將 WPF 與 OpenGL 一起使用。我沒有任何專業知識。
將像素下轉換為 8 位的唯一方法是什么?(這里有人提到)
我在這里遇到了最接近的問題,但沒有答案。
uj5u.com熱心網友回復:
您可以使用Format設定為的 BitmapSource PixelFormats.Gray16。
然后將源像素值轉換為 16 位像素值,如下所示:
public static int ConvertTo16Bit(int pixelValue, int sourceBitsPerPixel)
{
const int maxTargetValue = (1 << 16) - 1;
int maxSourceValue = (1 << sourceBitsPerPixel) - 1;
return maxTargetValue * pixelValue / maxSourceValue;
}
像這樣轉換像素緩沖區陣列:
public static void ConverTo16Bit(
ushort[] target, ushort[] source, int sourceBitsPerPixel)
{
const int maxTargetValue = (1 << 16) - 1;
int maxSourceValue = (1 << sourceBitsPerPixel) - 1;
for (int i = 0; i < source.Length; i )
{
target[i] = (ushort)(maxTargetValue * source[i] / maxSourceValue);
}
}
或者當你想創建一個新的目標像素緩沖區時,像這樣:
public static ushort[] ConverTo16Bit(ushort[] source, int sourceBitsPerPixel)
{
const int maxTargetValue = (1 << 16) - 1;
int maxSourceValue = (1 << sourceBitsPerPixel) - 1;
return source
.Select(value => (ushort)(maxTargetValue * value / maxSourceValue))
.ToArray();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/417876.html
標籤:
上一篇:Wpf在運行時為空白
