DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
mexITK.h
Go to the documentation of this file.
1 
18 #ifndef __mexITK_h
19 #define __mexITK_h
20 
21 #include <mex.h>
22 #include "utlITK.h"
23 #include "mexutils.h"
24 #include "itkImageRegionConstIterator.h"
25 #include "itkImageRegionIterator.h"
26 
27 namespace itk
28 {
29 
30 template <class T, unsigned int VImageDimension>
31 inline void
32 GetMXArrayFromITKImage ( const SmartPointer<Image<T,VImageDimension> >& image, mxArray*& pr )
33 {
34  typedef Image<T,VImageDimension> ImageType;
35  // utlException(IsImageEmpty(image), "the image is empty");
36  utlException(!image, "the image point is NULL");
37 
38  typename ImageType::RegionType region = image->GetLargestPossibleRegion();
39  typename ImageType::SizeType size = region.GetSize();
40 
41  utlException(VImageDimension>4, "the dimension of image is too large! VImageDimension=" << VImageDimension);
42  pr = utl::Create4DImage<T>(VImageDimension>=1?size[0]:1, VImageDimension>=2?size[1]:1, VImageDimension>=3?size[2]:1, VImageDimension==4?size[3]:1);
43  T * data = (T*)mxGetData(pr);
44 
45  ImageRegionConstIterator<ImageType> imageIt(image, region);
46  unsigned int count = 0;
47  for (imageIt.GoToBegin(); !imageIt.IsAtEnd(); ++imageIt, count++)
48  {
49  data[count] = imageIt.Get();
50  }
51 }
52 
53 template <class T, unsigned int VImageDimension>
54 inline void
55 GetITKImageFromMXArray ( const mxArray* pr, SmartPointer<Image<T,VImageDimension> > & image )
56 {
57  typedef Image<T, VImageDimension> ImageType;
58  if (!image)
59  image = ImageType::New();
60 
61  mwSize dimArray = mxGetNumberOfDimensions(pr);
62  const mwSize* dims = mxGetDimensions(pr);
63 
64  utlException(dimArray>VImageDimension, "pr has more dimensions than " << VImageDimension);
65 
66  typename ImageType::RegionType imageRegion;
67  typename ImageType::SizeType imageSize;
68  typename ImageType::IndexType imageIndex;
69 
70  for ( int i = 0; i < VImageDimension; i += 1 )
71  {
72  if (i<dimArray)
73  imageSize[i] = static_cast<unsigned int>(dims[i]);
74  else
75  imageSize[i] = 1;
76  imageIndex[i] = 0;
77  }
78 
79  imageRegion.SetSize(imageSize);
80  imageRegion.SetIndex(imageIndex);
81  image->SetRegions(imageRegion);
82 
83  image->Allocate();
84 
85  ImageRegionIterator<ImageType> imageIt(image, imageRegion);
86  unsigned int count = 0;
87  T * data = (T*)mxGetData(pr);
88  for (imageIt.GoToBegin(); !imageIt.IsAtEnd(); ++imageIt, count++)
89  {
90  imageIt.Set(data[count]);
91  }
92 }
93 
94 template <class T, unsigned int VImageDimension>
95 inline void
96 GetMXArrayFromITKVectorImage ( const SmartPointer<VectorImage<T,VImageDimension> >& image, mxArray*& pr )
97 {
98  typedef VectorImage<T,VImageDimension> ImageType;
99  // utlException(IsImageEmpty(image), "the image is empty");
100  utlException(!image, "the image point is NULL");
101 
102  typename ImageType::RegionType region = image->GetLargestPossibleRegion();
103  typename ImageType::SizeType size = region.GetSize();
104  int numberOfComponent = image->GetNumberOfComponentsPerPixel();
105  int sizeEachVolume = 1;
106  for ( int i = 0; i < VImageDimension; i += 1 )
107  sizeEachVolume *= size[i];
108 
109  utlException(VImageDimension>3, "the dimension of image is too large! VImageDimension=" << VImageDimension);
110  pr = utl::Create4DImage<T>(VImageDimension>=1?size[0]:1, VImageDimension>=2?size[1]:1, VImageDimension>=3?size[2]:1, numberOfComponent);
111  T * data = (T*)mxGetData(pr);
112 
113  typename ImageType::PixelType pixel;
114  ImageRegionConstIterator<ImageType> imageIt(image, region);
115  unsigned int count = 0;
116  for (imageIt.GoToBegin(); !imageIt.IsAtEnd(); ++imageIt, count++)
117  {
118  pixel = imageIt.Get();
119  for ( int i = 0; i < numberOfComponent; i += 1 )
120  {
121  data[count+i*sizeEachVolume] = pixel[i];
122  }
123  }
124 }
125 
126 template <class T>
127 inline void
128 GetITKVectorImageFromMXArray ( const mxArray* pr, SmartPointer<VectorImage<T,3> >& image )
129 {
130  typedef VectorImage<T, 3> ImageType;
131  if (!image)
132  image = ImageType::New();
133 
134  mwSize VImageDimension = mxGetNumberOfDimensions(pr);
135  const mwSize* dims = mxGetDimensions(pr);
136  int numberOfComponent = dims[VImageDimension-1];
137 
138  utlException(VImageDimension>4, "pr has more dimensions than 4");
139 
140  typename ImageType::RegionType imageRegion;
141  typename ImageType::SizeType imageSize;
142  typename ImageType::IndexType imageIndex;
143 
144  unsigned int realDim = utl::min(3, (int)VImageDimension-1);
145  int sizeEachVolume = 1;
146  for ( int i = 0; i < 3; i += 1 )
147  {
148  if (i<realDim)
149  {
150  sizeEachVolume *= dims[i];
151  imageSize[i] = static_cast<unsigned int>(dims[i]);
152  }
153  else
154  imageSize[i] = 1;
155  imageIndex[i] = 0;
156  }
157 
158  imageRegion.SetSize(imageSize);
159  imageRegion.SetIndex(imageIndex);
160  image->SetRegions(imageRegion);
161  image->SetNumberOfComponentsPerPixel(numberOfComponent);
162 
163  image->Allocate();
164 
165  typename ImageType::PixelType pixel;
166  pixel.SetSize(numberOfComponent);
167  ImageRegionIterator<ImageType> imageIt(image, imageRegion);
168  unsigned int count = 0;
169  T * data = (T*)mxGetData(pr);
170  for (imageIt.GoToBegin(); !imageIt.IsAtEnd(); ++imageIt, count++)
171  {
172  for ( int i = 0; i < numberOfComponent; i += 1 )
173  pixel[i] = data[count+i*sizeEachVolume];
174  imageIt.Set(pixel);
175  }
176 }
177 
178 }
179 
180 #endif
181 
void GetMXArrayFromITKVectorImage(const SmartPointer< VectorImage< T, VImageDimension > > &image, mxArray *&pr)
Definition: mexITK.h:96
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
void GetITKVectorImageFromMXArray(const mxArray *pr, SmartPointer< VectorImage< T, 3 > > &image)
Definition: mexITK.h:128
int mwSize
Definition: utils.h:38
const T & min(const T &a, const T &b)
Return the minimum between a and b.
Definition: utlCore.h:257
Contains miscellaneous functions for mex files. utl functions for mex code. Some codes are from spams...
void GetMXArrayFromITKImage(const SmartPointer< Image< T, VImageDimension > > &image, mxArray *&pr)
Definition: mexITK.h:32
void GetITKImageFromMXArray(const mxArray *pr, SmartPointer< Image< T, VImageDimension > > &image)
Definition: mexITK.h:55