DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
mexITKImageRead.cxx
Go to the documentation of this file.
1 
19 #include "mex.h"
20 #include "utlMEX.h"
21 
22 #include "itkImageFileReader.h"
23 
24 template <typename T, unsigned int dim, bool isVectorImage>
25  inline void callFunction(mxArray* plhs[], const mxArray* prhs[],
26  const int nlhs,const int nrhs)
27 {
28 
29  std::string filename = utl::GetString(prhs[0]);
30 
31  double * out_origin=0;
32  double * out_spacing=0;
33  if (nlhs>1)
34  {
35  plhs[1] = utl::CreateMatrix<T>(1, dim);
36  plhs[2] = utl::CreateMatrix<T>(1, dim);
37  out_origin = mxGetPr(plhs[1]);
38  out_spacing = mxGetPr(plhs[2]);
39  }
40 
41  if (isVectorImage)
42  {
43  typedef itk::VectorImage<T, dim> ImageType;
44  typename ImageType::Pointer imageVec = ImageType::New();
45  itk::ReadImage(filename, imageVec);
46  itk::GetMXArrayFromITKVectorImage(imageVec, plhs[0]);
47 
48  if (nlhs>1)
49  {
50  typename ImageType::SpacingType spacing = imageVec->GetSpacing();
51  typename ImageType::PointType origin = imageVec->GetOrigin();
52  for ( int i = 0; i < dim; i += 1 )
53  {
54  out_origin[i] = origin[i];
55  out_spacing[i] = spacing[i];
56  }
57  }
58  }
59  else
60  {
61  typedef itk::Image<T, dim> ImageType;
62  typename ImageType::Pointer image4D = ImageType::New();
63  itk::ReadImage(filename, image4D);
64  itk::GetMXArrayFromITKImage(image4D, plhs[0]);
65 
66  if (nlhs>1)
67  {
68  typename ImageType::SpacingType spacing = image4D->GetSpacing();
69  typename ImageType::PointType origin = image4D->GetOrigin();
70  for ( int i = 0; i < dim; i += 1 )
71  {
72  out_origin[i] = origin[i];
73  out_spacing[i] = spacing[i];
74  }
75  }
76  }
77 }
78 
79 void mexFunction(int nlhs, mxArray *plhs[],
80  int nrhs, const mxArray *prhs[])
81 {
82  utlGlobalException(nrhs!=1, "Bad number of inputs arguments");
83  utlGlobalException(nlhs!=1 && nlhs!=3, "Bad number of outputs arguments");
84 
85  utlGlobalException(mxGetClassID(prhs[0])!=mxCHAR_CLASS, "the first input has to be a string");
86  std::string filename = utl::GetString(prhs[0]);
87 
88  const unsigned int Dimension = 4;
89 
90  typedef itk::VectorImage<float, Dimension> MultiVolumeVectorImageType;
91  typedef itk::ImageFileReader<MultiVolumeVectorImageType> ReaderType;
92 
93  ReaderType::Pointer reader = ReaderType::New();
94  reader->SetFileName(filename);
95  reader->UpdateOutputInformation();
96  MultiVolumeVectorImageType::Pointer image = reader->GetOutput();
97 
98  unsigned int numberOfComponentsPerPixel = image->GetNumberOfComponentsPerPixel();
99  bool isVectorImage = numberOfComponentsPerPixel>1;
100 
101  // image->Print(std::cout<<"image=");
102  // itk::PrintVectorImage(image,"image");
103  MultiVolumeVectorImageType::SizeType size = image->GetLargestPossibleRegion().GetSize();
104  int dim = Dimension;
105  // if size=[1,1,1,1], dim=3. dim is 3 or 4.
106  for ( int i = Dimension-1; i > 2 ; i-- )
107  {
108  if (size[i]==1)
109  dim--;
110  else
111  break;
112  }
113 
114  if (dim==4)
115  isVectorImage? callFunction<double,4,true>(plhs,prhs,nlhs,nrhs) : callFunction<double,4,false>(plhs,prhs,nlhs,nrhs);
116  else if (dim==3)
117  isVectorImage? callFunction<double,3,true>(plhs,prhs,nlhs,nrhs) : callFunction<double,3,false>(plhs,prhs,nlhs,nrhs);
118  // else if (dim==2)
119  // isVectorImage? callFunction<double,2,true>(plhs,prhs,nlhs,nrhs) : callFunction<double,2,false>(plhs,prhs,nlhs,nrhs);
120  // else if (dim==1)
121  // isVectorImage? callFunction<double,1,true>(plhs,prhs,nlhs,nrhs) : callFunction<double,1,false>(plhs,prhs,nlhs,nrhs);
122 }
123 
void GetMXArrayFromITKVectorImage(const SmartPointer< VectorImage< T, VImageDimension > > &image, mxArray *&pr)
Definition: mexITK.h:96
bool ReadImage(const std::string &filename, SmartPointer< ImageType > &image, const std::string &printInfo="Reading Image:")
Definition: utlITK.h:110
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
void callFunction(mxArray *plhs[], const mxArray *prhs[], const int nlhs, const int nrhs)
void GetString(const mxArray *pr, std::string &str)
Definition: mexutils.h:275
void GetMXArrayFromITKImage(const SmartPointer< Image< T, VImageDimension > > &image, mxArray *&pr)
Definition: mexITK.h:32