24 #include "itkImageRegionConstIterator.h" 25 #include "itkImageRegionIterator.h" 30 template <
class T,
unsigned int VImageDimension>
34 typedef Image<T,VImageDimension> ImageType;
38 typename ImageType::RegionType region = image->GetLargestPossibleRegion();
39 typename ImageType::SizeType size = region.GetSize();
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);
45 ImageRegionConstIterator<ImageType> imageIt(image, region);
46 unsigned int count = 0;
47 for (imageIt.GoToBegin(); !imageIt.IsAtEnd(); ++imageIt, count++)
49 data[count] = imageIt.Get();
53 template <
class T,
unsigned int VImageDimension>
57 typedef Image<T, VImageDimension> ImageType;
59 image = ImageType::New();
61 mwSize dimArray = mxGetNumberOfDimensions(pr);
62 const mwSize* dims = mxGetDimensions(pr);
64 utlException(dimArray>VImageDimension,
"pr has more dimensions than " << VImageDimension);
66 typename ImageType::RegionType imageRegion;
67 typename ImageType::SizeType imageSize;
68 typename ImageType::IndexType imageIndex;
70 for (
int i = 0; i < VImageDimension; i += 1 )
73 imageSize[i] =
static_cast<unsigned int>(dims[i]);
79 imageRegion.SetSize(imageSize);
80 imageRegion.SetIndex(imageIndex);
81 image->SetRegions(imageRegion);
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++)
90 imageIt.Set(data[count]);
94 template <
class T,
unsigned int VImageDimension>
98 typedef VectorImage<T,VImageDimension> ImageType;
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];
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);
113 typename ImageType::PixelType pixel;
114 ImageRegionConstIterator<ImageType> imageIt(image, region);
115 unsigned int count = 0;
116 for (imageIt.GoToBegin(); !imageIt.IsAtEnd(); ++imageIt, count++)
118 pixel = imageIt.Get();
119 for (
int i = 0; i < numberOfComponent; i += 1 )
121 data[count+i*sizeEachVolume] = pixel[i];
130 typedef VectorImage<T, 3> ImageType;
132 image = ImageType::New();
134 mwSize VImageDimension = mxGetNumberOfDimensions(pr);
135 const mwSize* dims = mxGetDimensions(pr);
136 int numberOfComponent = dims[VImageDimension-1];
138 utlException(VImageDimension>4,
"pr has more dimensions than 4");
140 typename ImageType::RegionType imageRegion;
141 typename ImageType::SizeType imageSize;
142 typename ImageType::IndexType imageIndex;
144 unsigned int realDim =
utl::min(3, (
int)VImageDimension-1);
145 int sizeEachVolume = 1;
146 for (
int i = 0; i < 3; i += 1 )
150 sizeEachVolume *= dims[i];
151 imageSize[i] =
static_cast<unsigned int>(dims[i]);
158 imageRegion.SetSize(imageSize);
159 imageRegion.SetIndex(imageIndex);
160 image->SetRegions(imageRegion);
161 image->SetNumberOfComponentsPerPixel(numberOfComponent);
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++)
172 for (
int i = 0; i < numberOfComponent; i += 1 )
173 pixel[i] = data[count+i*sizeEachVolume];
void GetMXArrayFromITKVectorImage(const SmartPointer< VectorImage< T, VImageDimension > > &image, mxArray *&pr)
#define utlException(cond, expout)
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.
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)
void GetITKImageFromMXArray(const mxArray *pr, SmartPointer< Image< T, VImageDimension > > &image)