简体   繁体   English

使用SimpleITK for C#读取RAW图像文件

[英]Reading RAW Image files using SimpleITK for C#

How do I read RAW files with SimpleITK for C#? 如何使用SimpleITK for C#读取RAW文件?

I use SimpleITK C# version 0.5.1 on Windows (64 bit) 我在Windows上使用SimpleITK C#版本0.5.1(64位)

My code is: 我的代码是:

String fileName = @"d:\Temp\MRI\t1_icbm_normal_1mm_pn3_rf20.rawb";
ImageFileReader reader = new ImageFileReader();
reader.SetFileName(fileName);
Image image = reader.Execute();

It works fine for .png or analyze images for example but not for RAW images. 它适用于.png或分析图像,例如但不适用于RAW图像。

I get the following error: 我收到以下错误:

System.ApplicationException : Exception thrown in SimpleITK ImageFileReader_Execute: ..\\..\\..\\..\\..\SimpleITK\Code\IO\src\sitkImageReaderBase.cxx:44:
sitk::ERROR: Unable to determine ImageIO reader for "d:\Temp\MRI\t1_icbm_normal_1mm_pn3_rf20.rawb"

I understand that the ImageFileReader cannot recognize the file type. 我知道ImageFileReader无法识别文件类型。 Is there a way of passing the image type and image parameters in SimpleITK since the standard ITK templates are hidden? 有没有办法在SimpleITK中传递图像类型和图像参数,因为隐藏了标准的ITK模板?

Thanks to Insight-Users group I got some clues to solve this issue. 感谢Insight-Users小组,我得到了解决此问题的一些线索。 The problem is that ImageFileReader cannot read RAW files. 问题是ImageFileReader无法读取RAW文件。 The solution is to read binary data manualy and use ImportImageFilter to build the Image object. 解决方案是手动读取二进制数据并使用ImportImageFilter构建Image对象。 The code is as follows: 代码如下:

    [Test]
    public void ReadImageRAW()
    {
        String fileName = @"d:\Temp\MRI\t1_icbm_normal_1mm_pn3_rf20.rawb";

        Byte[] imageData = System.IO.File.ReadAllBytes(fileName);

        UInt32 width = 181;
        UInt32 height = 217;
        UInt32 depth = 181;

        ImportImageFilter importImageFilter = new ImportImageFilter();

        importImageFilter.SetSize(new VectorUInt32(new UInt32[] {width, height, depth}));

        importImageFilter.SetDirection(new VectorDouble(new Double[] {1, 0, 0, 
                                                                      0, 1, 0, 
                                                                      0, 0, 1}));

        importImageFilter.SetOrigin(new VectorDouble(new Double[] { 0, 0, 0 }));

        importImageFilter.SetSpacing(new VectorDouble(new Double[] {1, 1, 1}));

        GCHandle hObject = GCHandle.Alloc(imageData, GCHandleType.Pinned);
        IntPtr imageDataPtr = hObject.AddrOfPinnedObject();

        importImageFilter.SetBufferAsUInt8(imageDataPtr);

        Image importedImage = importImageFilter.Execute();
        SimpleITK.Show(importedImage);
    }

This question is also answered in the SimpelITK FAQ [1]: SimpelITK FAQ [1]也回答了这个问题:

In general raw image files are missing information. 通常,原始图像文件缺少信息。 They do not contain the nessesary header information to describe the basic size and type for the data, so this format is intrinsically deficient. 它们不包含用于描述数据的基本大小和类型的nessesary头信息,因此这种格式本质上是不足的。 The RawImageIO class is not available in SimpleITK so there is no direct way to programmatically hard code this header information. RawImageIO类在SimpleITK中不可用,因此没有直接的方法以编程方式对此标头信息进行硬编码。 The suggested way is to create a Meta image header file (*.mhd) which references the raw data file and describes the size and type of the data. 建议的方法是创建一个元图像头文件(* .mhd),它引用原始数据文件并描述数据的大小和类型。 The documentation on how to write a Meta image header can be found here. 有关如何编写元图像标题的文档可以在这里找到。 The following is a sample Meta image header file, perhaps of name sample.mhd: 以下是一个示例元图像头文件,可能名称为sample.mhd:

ObjectType = Image
NDims = 3
DimSize = 256 256 64
ElementType = MET_USHORT
ElementDataFile = image.raw (this tag must be last in a MetaImageHeader)

[1] http://www.itk.org/Wiki/SimpleITK/FAQ#How_do_I_read_a_RAW_image_into_SimpleITK.3F [1] http://www.itk.org/Wiki/SimpleITK/FAQ#How_do_I_read_a_RAW_image_into_SimpleITK.3F

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM