簡體   English   中英

如何在ushort數組中讀取圖像的像素數據並將此ushort數組導出為c#中的二進制文件

[英]How to read pixel data of image in an ushort array and export this ushort array as a binary file in c#

我有一個Tiff圖像,它是一個16位圖像,現在我想將像素數據讀取為ushort數組,並將其存儲為文本格式的二進制格式。

我有示例代碼。

// Load file meta data with FileInfo
        FileInfo fileInfo = new FileInfo(path + "output.tif");

            // The byte[] to save the data in
            byte[] data = new byte[fileInfo.Length];
            // Load a filestream and put its content into the byte[]
            using (FileStream fs = fileInfo.OpenRead())
            {
                fs.Read(data, 0, data.Length);
            }

          ushort[] result = Array.ConvertAll(data, b => (ushort)b);

但是它正在讀取元數據,我想讀取像素數據。

您絕對應該使用外部庫來處理tiff文件。 這是一種復雜的文件格式,可以包含多個圖像,可以壓縮圖像等。因此,僅讀取字節數組絕對不會幫助您獲取像素值。 即使它僅包含像素的顏色值,您仍然必須將每兩個字節合並為一個16位圖像。

在示例代碼中,您只是將字節轉換為ushorts,這意味着您在數字之前加零,這不會更改值。

首先,映像已經是二進制文件。

在這里,我不明白將圖像顯式轉換為ushort數組的目的。 除此之外,您可以簡單地將Tiff圖像的所有字節保存到文本文件中,如下所示:

File.WriteAllBytes(@"Text file path", File.ReadAllBytes(@"Tiff image file path"));

然后,要從二進制文件中讀回它,您可以簡單地從保存的文本文件中讀取所有字節,並使用MemoryStream並將其保存回映像中。

using (Image image = Image.FromStream(new MemoryStream(File.ReadAllBytes(@"Text file path"))))
{
    image.Save(@"Path to save image", ImageFormat.Tiff);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM