[Insight-users] Write hdr/img format from a series of raw images (float format)

Luis Ibanez luis.ibanez at kitware.com
Sat Mar 21 13:09:13 EDT 2009


Hi Ching-Yi,


The easiest way to do this is with the two following steps:


A) Write a MetaImageHeader for your RAW images

      its content should look like:

NDims = 3
DimSize = 256 256 20
ElementSpacing = 0.5 0.5 0.8
Position = 0 0 0
ElementByteOrderMSB = False
ElementType = MET_UCHAR
ElementDataFile = image%03d.raw 0 20 1

    assuming that your raw files are called

                  image000.raw
                  image001.raw
                  ...
                  image020.raw

     and of course, you should change all the
     values above in order to match the size
     spacing, origin and pixel type and endianness
     of your raw images.


B) Use the example

      Insight/Examples/IO/ImageReadWrite.cxx

    (but change the Dimension in line 95 from "2" to "3".


     Then simply compile it and run it as:


        ImageReadWrite.exe  myInput.mhd  output.hdr



    Regards,


        Luis

-----------------------
Ching-Yi Hsieh wrote:
> Hi,
> Could anyone give me some directions to write this code in ITK?
> I did check /InsightToolkit-3.12.0/Testing/Code/IO/itkAnalyzeImageIOTest.cxx
> Do I only modify this subrountine and put this in my 
> readrawimages_writehdr.cxx?
> Thanks for your time and help.
> 
> Regards,
> Ching-Yi
> 
> static int WriteTestFiles(const std::string AugmentName)
> {
> #include "LittleEndian_hdr.h"
> #include "LittleEndian_img.h"
> #include "BigEndian_hdr.h"
> #include "BigEndian_img.h"
>   std::string LittleEndianHdrName=AugmentName+"LittleEndian.hdr";
>   std::ofstream little_hdr(LittleEndianHdrName.c_str(), std::ios::binary 
> | std::ios::out);
>   if(!little_hdr.is_open())
>     {
>     return EXIT_FAILURE;
>     }
>   //std::cout << LittleEndianHdrName << " written" << std::endl;
>   little_hdr.write(reinterpret_cast<const char 
> *>(LittleEndian_hdr),sizeof(LittleEndian_hdr));
>   little_hdr.close();
> 
>   std::string LittleEndianZName(AugmentName);
>   LittleEndianZName += "LittleEndianZ.hdr";
>   std::ofstream  littlez_hdr(LittleEndianZName.c_str(), std::ios::binary 
> | std::ios::out);
>   if(!littlez_hdr.is_open())
>     {
>     return EXIT_FAILURE;
>     }
>   littlez_hdr.write(reinterpret_cast<const char 
> *>(LittleEndian_hdr),sizeof(LittleEndian_hdr));
> 
>   std::string LittleEndianImgName=AugmentName+"LittleEndian.img";
>   std::ofstream little_img(LittleEndianImgName.c_str(), std::ios::binary 
> | std::ios::out);
>   if(!little_img.is_open())
>     {
>     return EXIT_FAILURE;
>     }
>   // write out compressed.
>   little_img.write(reinterpret_cast<const char 
> *>(LittleEndian_img),sizeof(LittleEndian_img));
>   little_img.close();
> 
>   // write out compressed image
>   std::string ImageZFilename(AugmentName);
>   ImageZFilename += "LittleEndianZ.img.gz";
>   gzFile  file_p = ::gzopen( ImageZFilename.c_str(), "wb" );
>   if( file_p==NULL )
>     {
>     return EXIT_FAILURE;
>     }
>   ::gzwrite(file_p,reinterpret_cast<const char *>(LittleEndian_img),
>             sizeof(LittleEndian_img));
>   ::gzclose(file_p);
> ...}
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _____________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> 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