[Insight-users] Re: Error using ImageReader class: Reading RAW

Luis Ibanez luis.ibanez@kitware.com
Sat, 01 Mar 2003 18:15:24 -0500


Hi Neha,

RAW files cannot be read using the basic factory
mechanism. The reason is that a raw file is incomplete
on information. There is no way in which ITK could
recover:

1) image size in pixels
2) spacing between pixels
3) image origin

For this reason, the RawImageIO object is not
registered along with the default ImageIO Factories.

You can still read Raw image by explicitly instantiating
the RawImageIO object and connecting it to the
ImageFileReader.  Something like:

typedef itk::ImageFileReader< ImageType >  ReaderType;
typedef itk::RawImageIO  ImageIOType;

ReaderType::Pointer reader = ReaderType::New();

ImageIOType::Pointer rawImageIO = ImageIOType::New();

reader->SetImageIO( rawImageIO );

reader->SetFileName( "image.raw" );

// Here, you provide the minimal information
// of the image to the ImageIO object

rawImageIO->SetOrigin( 0, 0.0 );  // origin in millimeters
rawImageIO->SetOrigin( 1, 0.0 );
rawImageIO->SetOrigin( 2, 0.0 );

rawImageIO->SetDimensions( 0, 256 );  // size in pixels
rawImageIO->SetDimensions( 1, 256 );
rawImageIO->SetDimensions( 2, 200 );

rawImageIO->SetSpacing( 0,  0.83 );  // spacing in millimeters
rawImageIO->SetSpacing( 0,  0.83 );
rawImageIO->SetSpacing( 0,  3.00 );


try {
   reader->Update();
}
catch( itk::Exception & exc )
{
   std::cerr << exc << std::endl;
}


---

The big drawback of this approach is that you have
to put this extra information in your code.

My suggestion is that instead of using the RawImageIO,
you write a MetaImageHeader.  MetaImage offers the
possibility of refering to an external data file.
In this case, your image.raw file.

The MetaImage header file is a simple text file that
contains only the extra information about spacing,
origin and size.

A simple way of creating MetaImage headers is to use
the demo-application "MetaImageImporter" which is
available in the cvs module :  "InsightApplications"


MetaImages can be read through the standard Factory
mechanism. In this way you get the best of both
approaches, at the expense of only having to define
a small text file (of about 10 lines).

MetaImage is probably the more versatil reader. With
it, you can use any of the basic C++ types as pixel
types.



Please let us know if you find further problems.


Thanks


     Luis


---------------------------
Neha D wrote:
> Hi Luis,
> 
> My problem is solved with using latest service pack of MSVC. Thanks a 
> lot for your suggestions on this problem. But I am still facing the 
> problem reading the raw file. I am follwing these steps mentioned in 
> software guide
> 
> *********************************************************************************** 
> 
> 
> typedef itk::Image< unsigned short, 2 > ImageType;
> typedef itk::ImageFileReader< ImageType > ReaderType;
> ReaderType::Pointer reader = ReaderType::New();
> 
> const char * filename = "pp37.raw";
>  
> reader->SetFileName( filename );
> 
> reader->Update();
> 
> ImageType::Pointer image = reader->GetOutput();
> 
> ************************************************************************
> 
> here program crashes in reader->update statement. I have debuged the 
> program. but reader pointer is not null. what can be the reason ? ( 
> using getoutput() before first call to update method can crash the 
> program. so i am calling update() method first.
> 
> My another question is --> in example program "RawReadWriteImage", 
> program asks for 3-D image, while raw files are 2-D images. So, 
> obviously, when I give raw image ( 512 K size which uses 2 channels ), 
> output is not same as input image. ( Number of slices are not asked to 
> enter ) Output image is of size exactly half of input image.
> 
> My third question is -->
> 
> Can ITK programs read images of all datatypes ( unsigned char, unsigned 
> short, real double, etc..and both orderBytes (big-endian and little 
> endian ) ?
> 
> I was going to try all image types, but I am stuck at reading the image 
> file.
> 
> Thanks,
> 
> Shilpa
> 
> 
>  
> 
>  
> 
>  */Luis Ibanez <luis.ibanez@kitware.com>/* wrote:
> 
>     Hi Neha,
> 
>     "type_info" should be defined in the std:: namespace
>     when you enable RTTI (Run Time Type Information)
>     in the Project Settings menu of VC++.
> 
>     If you are using CMake for generating your project,
>     this option should already be enabled. The same goes
>     for enabling "Exception" handling.
> 
>     Please go to the "Project Settings" menu, select
>     the C++ tab and look for the section where RTTI
>     and Exceptions are selected. Both options should
>     be already on.
> 
>     If they are off, something is going wrong with
>     the CMake configuration.
> 
>     Another option is that you may be missing one
>     of the service packs for Visual Studio 6.0.
>     I think the latest one is Service Pack 5 (SP5).
> 
>     Please verify which one is the latest service
>     pack of your VC++ compiler.
> 
>     -----
> 
>     The example code that you wrote looks fine.
>     The only detail is that y! ou don't need to create
>     the image with "New()" since later you are
>     getting the image from the reader using the
>     GetOutput() method.
> 
>     You could do simply:
> 
>     ImageType::Pointer image = filter->GetOutput();
> 
>     Also note that you are defining a 3D image,
>     but reading a PNG file. PNG files only support
>     2D images. This current code will read a 2D
>     image as a single slice of a 3D image. We
>     call this a degenerated volume. Note that
>     some filter will miss-behave with such images.
>     For example any filter requiring a 3D neighborhood
>     to operate on. (for example, Median filter,
>     Erosion/Dilation filters...)
> 
>     -----
> 
> 
>     Please let us know what you find concerning
>     the Project settings and the service pack
>     of your compiler.
> 
>     Thanks
> 
> 
>     Luis
> 
> 
>     -------------------------------
>     Neha D wrote:
>      > Hi Luis,
>      >
>      > Thanks for your lot of help. I could build my own example program of
>      > creati! ng image istance, out of ITK source. Error was do to not
>     using
>      > proper libraries.
>      >
>      > Anyway, I am facing problems while using ImageReader class. I
>     have used
>      > follwoing simple code , which is given in software guide.
>      >
>      > -------------------------------------------------------------
>      >
>      > #include "itkImage.h"
>      > #include "itkImageFileReader.h"
>      >
>      > #include
>      > int main()
>      > {
>      > typedef itk::Image< unsigned short, 3 > ImageType;
>      > ImageType::Pointer image = ImageType::New();
>      >
>      > std::cout << "This program read the 2-D PNG image!" << std::endl;
>      >
>      > typedef itk::ImageFileReader< ImageType > ReaderType;
>      > ReaderType::Pointer reader = ReaderType::New();
>      > const char * filename = "image001.png";
>      >
>      > reader->SetFileName( filename ); reader->Update();
>      > image = reader->GetOutput();
>      > re! turn 0;
>      > }
>      >
>      > --------------------------------------------------------------
>      >
>      > Here again, cmake build is successful, but i get following error
>     in MSVC
>      > build.
>      >
>      > *itkImageIOBase.h*(108) : error C2039: 'type_info' : is not a
>     member of
>      > 'std'
>      >
>      >
>     ---------------------------------------------------------------------------------------------------------------
>      >
>      > This can be related to adding include directoried in cmake build.
>     but
>      > can't figure out exact problem. How can i find out where this
>     variable
>      > is defined.
>      >
>      > Thanks.
>      >
> 
> 
> ------------------------------------------------------------------------
> Do you Yahoo!?
> Yahoo! Mail Plus 
> <http://rd.yahoo.com/mail/mailsig/*http://mailplus.yahoo.com> - 
> Powerful. Affordable. Sign up now 
> <http://rd.yahoo.com/mail/mailsig/*http://mailplus.yahoo.com>