22 template <
class ImageType>
24 SetITKImageInformation ( itk::SmartPointer<ImageType>& image,
const mxArray* originArray,
const mxArray* spacingArray )
26 typename ImageType::PointType origin;
27 for (
int i = 0; i < ImageType::ImageDimension; i += 1 )
31 mwSize dimOrgin = mxGetNumberOfElements(originArray);
32 double * data = mxGetPr(originArray);
33 for (
int i = 0; i <
utl::min((
int)dimOrgin, (
int)ImageType::ImageDimension); i += 1 )
36 typename ImageType::SpacingType spacing;
37 for (
int i = 0; i < ImageType::ImageDimension; i += 1 )
41 mwSize dimSpacing = mxGetNumberOfElements(spacingArray);
42 double * data = mxGetPr(spacingArray);
43 for (
int i = 0; i <
utl::min((
int)dimSpacing, (
int)ImageType::ImageDimension); i += 1 )
46 image->SetOrigin(origin);
47 image->SetSpacing(spacing);
52 const int nlhs,
const int nrhs,
const bool isVectorImage)
56 mxArray* originArray=NULL;
57 mxArray* spacingArray=NULL;
64 mwSize dimArray = mxGetNumberOfDimensions(prhs[1]);
65 const mwSize* dims = mxGetDimensions(prhs[1]);
70 typedef itk::VectorImage<T, 3> ImageType;
71 typename ImageType::Pointer image = ImageType::New();
78 typedef itk::Image<T, 4> ImageType;
79 typename ImageType::Pointer image = ImageType::New();
89 int nrhs,
const mxArray *prhs[])
94 utlGlobalException(mxGetClassID(prhs[0])!=mxCHAR_CLASS,
"the first input has to be a string");
96 bool isVectorImage = utl::GetScalarStructDef<bool>(nrhs==3?prhs[2]:NULL,
"vectorImage",
false);
98 if (mxGetClassID(prhs[1]) == mxSINGLE_CLASS)
100 std::cout <<
"mxSINGLE_CLASS, float" << std::endl << std::flush;
101 callFunction<float>(plhs,prhs,nlhs,nrhs, isVectorImage);
103 else if (mxGetClassID(prhs[1]) == mxLOGICAL_CLASS)
105 std::cout <<
"mxLOGICAL_CLASS, mxLogical" << std::endl << std::flush;
106 callFunction<mxLogical>(plhs,prhs,nlhs,nrhs, isVectorImage);
108 else if (mxGetClassID(prhs[1]) == mxUINT8_CLASS)
110 std::cout <<
"mxUINT8_CLASS, unsigned char" << std::endl << std::flush;
111 callFunction<unsigned char>(plhs,prhs,nlhs,nrhs, isVectorImage);
113 else if (mxGetClassID(prhs[1]) == mxUINT16_CLASS)
115 std::cout <<
"mxUINT16_CLASS, unsigned short int" << std::endl << std::flush;
116 callFunction<unsigned short int>(plhs,prhs,nlhs,nrhs, isVectorImage);
118 else if (mxGetClassID(prhs[1]) == mxUINT32_CLASS)
120 std::cout <<
"mxUINT32_CLASS, unsigned int" << std::endl << std::flush;
121 callFunction<unsigned int>(plhs,prhs,nlhs,nrhs, isVectorImage);
123 else if (mxGetClassID(prhs[1]) == mxUINT64_CLASS)
125 std::cout <<
"mxUINT64_CLASS, uint64_T" << std::endl << std::flush;
126 callFunction<uint64_T>(plhs,prhs,nlhs,nrhs, isVectorImage);
128 else if (mxGetClassID(prhs[1]) == mxINT8_CLASS)
130 std::cout <<
"mxINT8_CLASS, signed char" << std::endl << std::flush;
131 callFunction<signed char>(plhs,prhs,nlhs,nrhs, isVectorImage);
133 else if (mxGetClassID(prhs[1]) == mxINT16_CLASS)
135 std::cout <<
"mxINT16_CLASS, short int" << std::endl << std::flush;
136 callFunction<short int>(plhs,prhs,nlhs,nrhs, isVectorImage);
138 else if (mxGetClassID(prhs[1]) == mxINT32_CLASS)
140 std::cout <<
"mxINT32_CLASS, int" << std::endl << std::flush;
141 callFunction<int>(plhs,prhs,nlhs,nrhs, isVectorImage);
143 else if (mxGetClassID(prhs[1]) == mxINT64_CLASS)
145 std::cout <<
"mxINT64_CLASS, int64_T" << std::endl << std::flush;
146 callFunction<int64_T>(plhs,prhs,nlhs,nrhs, isVectorImage);
148 else if (mxGetClassID(prhs[1]) == mxDOUBLE_CLASS)
150 std::cout <<
"mxDOUBLE_CLASS, double" << std::endl << std::flush;
151 callFunction<double>(plhs,prhs,nlhs,nrhs, isVectorImage);
mxArray * GetArrayStruct(const mxArray *pr_struct, const char *name)
void SetITKImageInformation(itk::SmartPointer< ImageType > &image, const mxArray *originArray, const mxArray *spacingArray)
bool SaveImage(const SmartPointer< ImageType > &image, const std::string &filename, const std::string &printInfo="Writing Image:")
void GetITKVectorImageFromMXArray(const mxArray *pr, SmartPointer< VectorImage< T, 3 > > &image)
const T & min(const T &a, const T &b)
Return the minimum between a and b.
#define utlGlobalException(cond, expout)
void callFunction(mxArray *plhs[], const mxArray *prhs[], const int nlhs, const int nrhs, const bool isVectorImage)
void GetString(const mxArray *pr, std::string &str)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
void GetITKImageFromMXArray(const mxArray *pr, SmartPointer< Image< T, VImageDimension > > &image)