22 #include "itkImageFileReader.h" 24 template <
typename T,
unsigned int dim,
bool isVectorImage>
26 const int nlhs,
const int nrhs)
31 double * out_origin=0;
32 double * out_spacing=0;
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]);
43 typedef itk::VectorImage<T, dim> ImageType;
44 typename ImageType::Pointer imageVec = ImageType::New();
50 typename ImageType::SpacingType spacing = imageVec->GetSpacing();
51 typename ImageType::PointType origin = imageVec->GetOrigin();
52 for (
int i = 0; i < dim; i += 1 )
54 out_origin[i] = origin[i];
55 out_spacing[i] = spacing[i];
61 typedef itk::Image<T, dim> ImageType;
62 typename ImageType::Pointer image4D = ImageType::New();
68 typename ImageType::SpacingType spacing = image4D->GetSpacing();
69 typename ImageType::PointType origin = image4D->GetOrigin();
70 for (
int i = 0; i < dim; i += 1 )
72 out_origin[i] = origin[i];
73 out_spacing[i] = spacing[i];
80 int nrhs,
const mxArray *prhs[])
85 utlGlobalException(mxGetClassID(prhs[0])!=mxCHAR_CLASS,
"the first input has to be a string");
88 const unsigned int Dimension = 4;
90 typedef itk::VectorImage<float, Dimension> MultiVolumeVectorImageType;
91 typedef itk::ImageFileReader<MultiVolumeVectorImageType> ReaderType;
93 ReaderType::Pointer reader = ReaderType::New();
94 reader->SetFileName(filename);
95 reader->UpdateOutputInformation();
96 MultiVolumeVectorImageType::Pointer image = reader->GetOutput();
98 unsigned int numberOfComponentsPerPixel = image->GetNumberOfComponentsPerPixel();
99 bool isVectorImage = numberOfComponentsPerPixel>1;
103 MultiVolumeVectorImageType::SizeType size = image->GetLargestPossibleRegion().GetSize();
106 for (
int i = Dimension-1; i > 2 ; i-- )
115 isVectorImage? callFunction<double,4,true>(plhs,prhs,nlhs,nrhs) : callFunction<double,4,false>(plhs,prhs,nlhs,nrhs);
117 isVectorImage? callFunction<double,3,true>(plhs,prhs,nlhs,nrhs) : callFunction<double,3,false>(plhs,prhs,nlhs,nrhs);
void GetMXArrayFromITKVectorImage(const SmartPointer< VectorImage< T, VImageDimension > > &image, mxArray *&pr)
bool ReadImage(const std::string &filename, SmartPointer< ImageType > &image, const std::string &printInfo="Reading Image:")
#define utlGlobalException(cond, expout)
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)
void GetMXArrayFromITKImage(const SmartPointer< Image< T, VImageDimension > > &image, mxArray *&pr)