[Insight-users] Reaing RAW Images with SimpleITK for C# Windows

Sebastian Widz widz at infovision.pl
Sat Oct 27 13:52:28 EDT 2012


Dear Dan and Bradley,

Many thanks for your reply.
I have tried the way Dan suggested.
It seems that I can read RAW images now.

I have made one more test: I have converted the RAW image with ImageJ
software to Analyze7.5 format and load with SimpleITK standard procedure
(ImageFileReader). The only thing that bothers me is that after displaying
both (RAW and Analyze) RAW is upside down comparing to Analyze, and I am not
sure why.

Below I put placed code. If you think it is OK and would like to use it as
the example, feel free.
            
           [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);
        }

Regards,
Sebastian

-----Original Message-----
From: Bradley Lowekamp [mailto:blowekamp at mail.nih.gov] 
Sent: Saturday, October 27, 2012 3:20 PM
To: Dan Mueller
Cc: Sebastian Widz; insight-users at itk.org
Subject: Re: [Insight-users] Reaing RAW Images with SimpleITK for C# Windows

Hello,

Another approach would be to manually write a MetaImageIO header that refers
to you raw data, then just load it normally into SimpleITK. More information
about that can be found here:

http://www.itk.org/Wiki/ITK/MetaIO/Documentation#Quick_Start

Brad

On Oct 27, 2012, at 5:38 AM, Dan Mueller <dan.muel at gmail.com> wrote:

> Hi Sebastian,
> 
> Because the file format you are trying to read is not supported by 
> ITK, you will need to manually import the image using
> sitkImportImageFilter:
>   
> http://www.itk.org/SimpleITKDoxygen/html/sitkImportImageFilter_8h.html
> 
> I don't have time to write an example at the moment, but it would 
> involve reading the raw file using System.IO.File.ReadAllBytes, then 
> choosing the desired import method (i.e. ImportAsUInt8, ImportAsFloat,
> etc) and specifying the image size/spacing/origin/direction/num 
> components, and finally calling update on the import filter. This will 
> give you an ITK image wrapped around the raw byte array.
> 
> Please let us know how you get on. If you get stuck, I may have time 
> later this week to whip up a quick example.
> 
> HTH
> 
> Cheers, Dan
> 
> On 27 October 2012 17:50, Sebastian Widz <widz at infovision.pl> wrote:
>> Hi,
>> 
>> 
>> 
>> how to 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?
>> 
>> 
>> 
>> Regards, Sebastian
>> 
>> 
>> _____________________________________
>> Powered by www.kitware.com
>> 
>> Visit other Kitware open-source projects at 
>> http://www.kitware.com/opensource/opensource.html
>> 
>> Kitware offers ITK Training Courses, for more information visit:
>> http://www.kitware.com/products/protraining.php
>> 
>> Please keep messages on-topic and check the ITK FAQ at:
>> http://www.itk.org/Wiki/ITK_FAQ
>> 
>> Follow this link to subscribe/unsubscribe:
>> http://www.itk.org/mailman/listinfo/insight-users
>> 
> _____________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at 
> http://www.kitware.com/opensource/opensource.html
> 
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
> 
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users




More information about the Insight-users mailing list