Getting Started with the NRRD Format: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
Line 43: Line 43:
  #include <algorithm>
  #include <algorithm>
  #include <iostream>
  #include <iostream>
 
  #include <itkImage.h>
  #include <itkImage.h>
  #include <itkExceptionObject.h>
  #include <itkExceptionObject.h>

Revision as of 15:48, 2 December 2005

The NRRD format is a little bit different from other image formats. This format has spatial dimensions and other dimensions. The number of spatial dimensions in the image determ,ines the number of dimensions in the image. Other dimensions are loaded into a itk::Vector and used as the pixel type for the image. The user must know apriori the size of the vector upon instantiation of the Reader type since Vectors have a static size.


Using NRRD to Load Diffusion Weighted Images

Diffusion weighted image are used to generate the diffusion tensor. This subsequently can be used to generate scalar measures as well as fiber tracts. The use of the NRRD format to describe DTI data is described in greater detail on the NA-MIC Wiki Pages. Here is an example of how to read and write the diffusion weighted images in NRRD format using ITK. Since it was not clear upon looking at only the Doxygen pages for the IO factory method. Here is a basic example of the image format:

  • testNrrd.nhdr
NRRD0005
content: SomeIDNumber42
type: short
dimension: 4
space: right-anterior-superior
sizes: 2 2 2 2
thicknesses:  NaN  NaN 2.5  NaN
space directions: (2,0,0) (0,2.5,0) (0,0,2) none
centerings: cell cell cell none
kinds: space space space list
endian: little
encoding: ascii
space units: "mm" "mm" "mm"
space origin: (0,0,0)
data file: testNrrd.ascii
measurement frame: (1,0,0) (0,0,1) (0,1,0)
modality:=DWMRI
DWMRI_b-value:=1000
DWMRI_gradient_0000:= 0  0  0
DWMRI_gradient_0001:= 1  0  1
  • testNrrd.ascii
2 2
2 2
2 2
2 2
1 1
1 1
1 1
1 1

Here is an outline of a basic program to read and write the NRRD format:

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>

#include <itkImage.h>
#include <itkExceptionObject.h>
#include <itkImageFileWriter.h>
#include <itkImageFileReader.h>
#include <itkMetaDataObject.h>
#include <itkNrrdImageIO.h>



int main(int argc, char* argv[])
{  
  typedef itk::Vector<signed short,2>       VectorType;
  typedef itk::Image<VectorType,3>	    DiffusionImageType;
  typedef DiffusionImageType::Pointer	    DiffusionImagePointer;
   
   
  typedef itk::ImageFileReader<DiffusionImageType> FileReaderType;
  FileReaderType::Pointer reader = FileReaderType::New();
  reader->SetFileName(argv[1]);
  reader->Update();
   
  itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New();
  // This statement is to be added to the ITK CVS Release
  io->SetNrrdVectorType( nrrdKindList );
  io->SetFileType( itk::ImageIOBase::ASCII );
 
  typedef itk::ImageFileWriter<DiffusionImageType> WriterType;
  WriterType::Pointer nrrdWriter = WriterType::New();
  nrrdWriter->UseInputMetaDataDictionaryOn();
  nrrdWriter->SetInput( reader->GetOutput() );
  nrrdWriter->SetImageIO(io);
  nrrdWriter->SetFileName(argv[2]);
  try
    {
    nrrdWriter->Update();
    }
  catch (itk::ExceptionObject e)
    {
    std::cout << e << std::endl;
    }

  return 0;
}