MINC/SoftwareDevelopment/EZMINC/ITK Integration

From Wikibooks, open books for an open world
(Redirected from MINC/EZMINC/ITK Integration)
Jump to navigation Jump to search

Better MINC1/2 support for ITK[edit | edit source]

If you are not satisfied with MINC2 only interface provided by ITK, or need access to MINC file attributes, or want to read/write 4D data use ITK interface provided by EZMINC

Installing[edit | edit source]

Compile EZMINC with ITK support, see [EZMINC_Installing] If you are not using ITK plugin (currently plugin is not recommended) add following code to "mincify" your ITK program:

#include "itkMincImageIOFactory.h" 
#include "itkMincImageIO.h"
//....
int main(int argc,char **argv)
{
  //registering the MINC_IO factory
  itk::ObjectFactoryBase::RegisterFactory(itk::MincImageIOFactory::New()); 
  //....

Example of working with DTI data[edit | edit source]

Code from itk_dti.cpp from examples:

   typedef itk::VectorImage<float, 3>  DTIImageType;
   typedef std::vector<double> double_vector;
   //registering the MINC_IO factory
   itk::ObjectFactoryBase::RegisterFactory(itk::MincImageIOFactory::New());
   //....
   itk::ImageFileReader<ImageType >::Pointer reader = itk::ImageFileReader<ImageType >::New();
   reader->SetFileName(input_file);
   reader->Update();
   ImageType::Pointer img=reader->GetOutput();
   double_vector bvalues,direction_x,direction_y,direction_z;
   //making sure that all vcrtors contain the same number of parameters (just in case)
   if(!itk::ExposeMetaData<double_vector>( img->GetMetaDataDictionary() , "acquisition:bvalues",bvalues) ||
       !itk::ExposeMetaData<double_vector>( img->GetMetaDataDictionary() , "acquisition:direction_x",direction_x) ||
       !itk::ExposeMetaData<double_vector>( img->GetMetaDataDictionary() , "acquisition:direction_y",direction_y) ||
       !itk::ExposeMetaData<double_vector>( img->GetMetaDataDictionary() , "acquisition:direction_z",direction_z))
   {
     std::cerr<<"Image doesn't have information on DTI gradients, can't process!"<<std::endl;
     return 2;
   }
   if(bvalues.size()!=direction_x.size() || 
      bvalues.size()!=direction_y.size() ||
      bvalues.size()!=direction_z.size() )
   {
     std::cerr<<"Different number of components of gradients"<<std::endl;
       return 2;
   }
   std::cout<<"Found "<<bvalues.size()<<" gradient directions"<<std::endl;

Using minc XFM files[edit | edit source]

  • applying transformation

code from itk_resample.cpp

#include "itkMincImageIOFactory.h"
#include "itkMincImageIO.h"
#include "minc_helpers.h"
typedef itk::ResampleImageFilter<minc::image3d, minc::image3d> FilterType;
typedef minc::XfmTransform  TransformType;
//...
TransformType::Pointer transform = TransformType::New();
//reading a minc style xfm file
transform->OpenXfm(xfm_f.c_str());
transform->Invert();
filter->SetTransform( transform );
//...

  • Storing affine transformation in .xfm file

#include <minc_helpers.h>
//...
itk::AffineTransform<double,3>  m_AffineTransform;
//...
minc::write_linear_xfm(output_xfm, m_AffineTransform->GetMatrix(), m_AffineTransform->GetOffset());

  • Reading affine transformation from .xfm file

itk::AffineTransform<double,3>  m_AffineTransform;
//...
itk::Matrix<double,3,3> rotation;
itk::Vector<double,3> translation;
read_linear_xfm(input_xfm,rotation,translation);
//...
m_AffineTransform.SetOffset(translation);
m_AffineTransform.SetMatrix(rotation);

  • reading/writing .tag files

 std::vector<int> labels;
 std::vector<itk::Point<double>,3> tags;
 read_tags(tags, labels, input_tag);
 //....
 write_tags(tags, labels, ouput_tag);