Question

How do I read RAW files with SimpleITK for C#?

I use SimpleITK C# version 0.5.1 on Windows (64 bit)

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.

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. Is there a way of passing the image type and image parameters in SimpleITK since the standard ITK templates are hidden?

Was it helpful?

Solution

Thanks to Insight-Users group I got some clues to solve this issue. The problem is that ImageFileReader cannot read RAW files. The solution is to read binary data manualy and use ImportImageFilter to build the Image object. 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);
    }

OTHER TIPS

This question is also answered in the 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. The RawImageIO class is not available in SimpleITK so there is no direct way to programmatically hard code this header information. 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. 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:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top