【发布时间】:2018-12-12 00:47:30
【问题描述】:
我正在创建一个 16 位灰度图像并使用 C# 将其保存为 PNG。当我使用 GIMP 或 OpenCV 加载图像时,图像以 8 位而不是 16 位的精度显示。你知道我的代码有什么问题吗?
1) 这是用于创建 PNG 的代码
public static void Create16BitGrayscaleImage(int imageWidthInPixels, int imageHeightInPixels, ushort[,] colours,
string imageFilePath)
{
// Multiplying by 2 because it has two bytes per pixel
ushort[] pixelData = new ushort[imageWidthInPixels * imageHeightInPixels * 2];
for (int y = 0; y < imageHeightInPixels; ++y)
{
for (int x = 0; x < imageWidthInPixels; ++x)
{
int index = y * imageWidthInPixels + x;
pixelData[index] = colours[x, y];
}
}
BitmapSource bmpSource = BitmapSource.Create(imageWidthInPixels, imageHeightInPixels, 86, 86,
PixelFormats.Gray16, null, pixelData, imageWidthInPixels * 2);
using (Stream str = new FileStream(imageFilePath, FileMode.Create))
{
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bmpSource));
enc.Save(str);
}
}
2) 这是读取图像属性的 Python 代码:
import cv2
img = cv2.imread(image_path)
【问题讨论】:
-
如果答案帮助你接受它!
标签: c# bitmap png grayscale 16-bit