DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utlITK.h
Go to the documentation of this file.
1 
20 #ifndef __utlITK_h
21 #define __utlITK_h
22 
23 #include <itkImage.h>
24 #include <itkVectorImage.h>
25 #include <itkImageFileReader.h>
26 #include <itkImageFileWriter.h>
27 #include <itkTimeProbe.h>
28 #include <itkImageRegionIteratorWithIndex.h>
29 #include <itkVariableLengthVector.h>
30 #include <itkNumericTraits.h>
31 
32 #include "utlCore.h"
33 #include "utlITKConceptChecking.h"
34 
35 #include "utlITKMacro.h"
36 
37 namespace itk
38 {
39 
43 template <class T>
44 void
45 PrintVariableLengthVector(const VariableLengthVector<T>vec, const std::string& str="", const char* separate=" ", std::ostream& os=std::cout)
46 {
47  char tmp[1024];
48  int NSize=vec.GetSize();
49  std::vector<double> st = utl::GetContainerStats(vec.GetDataPointer(), vec.GetDataPointer()+vec.Size());
50  sprintf(tmp, "%-8s(%p): size = %lu, stat = { %g, %g [%g], %g } : ", str==""?"vector":str.c_str(), &vec, (long unsigned int)NSize, st[0], st[2], st[3], st[1] );
51  std::string strr(tmp);
52  if (NSize>0)
53  {
54  os << strr << " = [ ";
55  for ( int i = 0; i < NSize-1; i += 1 )
56  os << vec[i] << separate;
57  os << vec[NSize-1] << " ];\n";
58  }
59  else
60  os << strr << " is empty vector" << std::endl;
61 
62 }
63 
64 template <class T>
65 VariableLengthVector<T>
66 VnlVectorToVariableLengthVector ( const vnl_vector<T>& vec )
67 {
68  VariableLengthVector<T> v(vec.size());
69  for ( int i = 0; i < vec.size(); i += 1 )
70  v[i] = vec[i];
71  return v;
72 }
73 
74 template <class T>
75 vnl_vector<T>
76 VariableLengthVectorToVnlVector ( const VariableLengthVector<T>& vec )
77 {
78  vnl_vector<T> v(vec.GetSize());
79  for ( int i = 0; i < vec.GetSize(); i += 1 )
80  v[i] = vec[i];
81  return v;
82 }
83 
85 template <class ImageType>
86 bool
87 ReadImageInformation (const std::string& filename, SmartPointer<ImageType>& image)
88 {
89  typedef itk::ImageFileReader<ImageType> ReaderType;
90  typename ReaderType::Pointer reader = ReaderType::New();
91 
92  reader->SetFileName(filename);
93  try
94  {
95  reader->UpdateOutputInformation();
96  }
97  catch (itk::ExceptionObject & err)
98  {
99  std::cout << "ExceptionObject caught !" << std::endl;
100  std::cout << err << std::endl;
101  return false;
102  }
103  image = reader->GetOutput();
104  return true;
105 }
106 
108 template <class ImageType>
109 bool
110 ReadImage (const std::string& filename, SmartPointer<ImageType>& image, const std::string& printInfo="Reading Image:")
111 {
112  typedef itk::ImageFileReader<ImageType> ReaderType;
113  typename ReaderType::Pointer reader = ReaderType::New();
114 
115  reader->SetFileName(filename);
116  try
117  {
118  if (utl::IsLogNormal())
119  std::cout << printInfo << " " << filename << std::endl;
120  reader->Update();
121  }
122  catch (itk::ExceptionObject & err)
123  {
124  std::cout << "ExceptionObject caught !" << std::endl;
125  std::cout << err << std::endl;
126  return false;
127  }
128  image = reader->GetOutput();
129  return true;
130 }
131 
132 template <class ImageType, class ReaderType>
133 bool
134 ReadImage (const std::string& filename, SmartPointer<ImageType>& image, const std::string& printInfo="Reading Image:")
135 {
136  typename ReaderType::Pointer reader = ReaderType::New();
137 
138  reader->SetFileName(filename);
139  try
140  {
141  if (utl::IsLogNormal())
142  std::cout << printInfo << " " << filename << std::endl;
143  reader->Update();
144  }
145  catch (itk::ExceptionObject & err)
146  {
147  std::cout << "ExceptionObject caught !" << std::endl;
148  std::cout << err << std::endl;
149  return false;
150  }
151  image = reader->GetOutput();
152  return true;
153 }
154 
155 template <class ImageType>
156 bool
157 SaveImage (const SmartPointer<ImageType>& image, const std::string& filename, const std::string& printInfo="Writing Image:")
158 {
159  typedef itk::ImageFileWriter<ImageType> WriterType;
160  typename WriterType::Pointer writer = WriterType::New();
161 
162  writer->SetFileName(filename);
163  writer->SetInput(image);
164  try
165  {
166  if (utl::IsLogNormal())
167  std::cout << printInfo << " " << filename << std::endl;
168  writer->Update();
169  }
170  catch (itk::ExceptionObject & err)
171  {
172  std::cout << "ExceptionObject caught !" << std::endl;
173  std::cout << err << std::endl;
174  return false;
175  }
176  return true;
177 }
178 
179 template <class ImageType, class WriterType>
180 bool
181 SaveImage (const SmartPointer<ImageType>& image, const std::string& filename, const std::string& printInfo="Writing Image:")
182 {
183  typename WriterType::Pointer writer = WriterType::New();
184 
185  writer->SetFileName(filename);
186  writer->SetInput(image);
187  try
188  {
189  if (utl::IsLogNormal())
190  std::cout << printInfo << " " << filename << std::endl;
191  writer->Update();
192  }
193  catch (itk::ExceptionObject & err)
194  {
195  std::cout << "ExceptionObject caught !" << std::endl;
196  std::cout << err << std::endl;
197  return false;
198  }
199  return true;
200 }
201 
202 inline int
203 GetImageType(const std::string& filename)
204 {
205  if (utl::IsEndingWith(filename, ".spr"))
206  return IMAGE_SPARSE;
207  else if (utl::IsEndingWith(filename, ".vlv"))
208  return IMAGE_VARIABLELENGTH;
209  else
210  {
211  typedef itk::VectorImage<float, 4> MultiVolumeVectorImageType;
212  typedef itk::ImageFileReader<MultiVolumeVectorImageType> ReaderType;
213 
214  ReaderType::Pointer reader = ReaderType::New();
215  reader->SetFileName(filename);
216  reader->UpdateOutputInformation();
217  MultiVolumeVectorImageType::Pointer image = reader->GetOutput();
218 
219  unsigned int numberOfComponentsPerPixel = image->GetNumberOfComponentsPerPixel();
220  if (numberOfComponentsPerPixel > 1)
221  return IMAGE_VECTOR;
222  else
223  return IMAGE_ND;
224  }
225 }
226 
227 template <class ImageType>
228 inline int
229 GetImageType(const SmartPointer<ImageType>& image)
230 {
231  std::string name = image->GetNameOfClass();
232  if (name=="VectorImage")
233  return IMAGE_VECTOR;
234  else if (name=="Image")
235  {
236  typedef typename ImageType::PixelType PType;
237  if (std::is_scalar<PType>::value)
238  return IMAGE_ND;
239  else
240  {
241  PType p;
242  if (utl::IsInstanceOf<itk::VariableLengthVector<double>>(p) || utl::IsInstanceOf<itk::VariableLengthVector<float>>(p) || utl::IsInstanceOf<itk::VariableLengthVector<int>>(p))
243  return IMAGE_VARIABLELENGTH;
244  else
245  utlException(true, "wrong Image type");
246  }
247  }
248  else if (name=="SpatiallyDenseSparseVectorImage")
249  return IMAGE_SPARSE;
250 }
251 
252 inline bool
253 IsVectorImage(const std::string& filename)
254 {
255  return GetImageType(filename)==IMAGE_VECTOR;
256 }
257 
258 inline bool
259 Is3DImage(const std::string& filename)
260 {
261  if (IsVectorImage(filename))
262  return false;
263 
264  typedef itk::Image<float, 4> ImageType;
265  typedef itk::ImageFileReader<ImageType> ReaderType;
266 
267  ReaderType::Pointer reader = ReaderType::New();
268  reader->SetFileName(filename);
269  reader->UpdateOutputInformation();
270  ImageType::Pointer image = reader->GetOutput();
271 
272  typename ImageType::SizeType size = image->GetLargestPossibleRegion().GetSize();
273  return size[3]==1;
274 }
275 
276 template <class ImageType>
277 int
278 GetVectorImageVectorSize(const SmartPointer<ImageType>& image, const int axis=-1)
279 {
280  std::string name = image->GetNameOfClass();
281  if (name=="VectorImage" || name=="SpatiallyDenseSparseVectorImage")
282  {
283  if (axis<0 || axis==ImageType::ImageDimension)
284  return image->GetNumberOfComponentsPerPixel();
285  else
286  {
287  typename ImageType::SizeType size = image->GetLargestPossibleRegion().GetSize();
288  return size[axis];
289  }
290  }
291  else if (name=="Image")
292  {
293  typename ImageType::SizeType size = image->GetLargestPossibleRegion().GetSize();
294  if (axis==ImageType::ImageDimension || (axis<0 && ImageType::ImageDimension<=3))
295  return 1;
296  else if (axis<0 || axis==ImageType::ImageDimension-1)
297  return size[ImageType::ImageDimension-1];
298  else
299  return size[axis];
300  }
301  else
302  {
303  utlGlobalException(true, "not supported type: " + name);
304  return -1;
305  }
306 }
307 
308 template <class ImageType>
309 void
310 SetVectorImageVectorSize(const SmartPointer<ImageType>& image, const int vecsize)
311 {
312  std::string name = image->GetNameOfClass();
313  if (name=="VectorImage" || name=="SpatiallyDenseSparseVectorImage")
314  {
315  image->SetNumberOfComponentsPerPixel(vecsize);
316  }
317  else if (name=="Image")
318  {
319  typename ImageType::RegionType region = image->GetLargestPossibleRegion();
320  typename ImageType::SizeType size = region.GetSize();
321  size[ImageType::ImageDimension-1]=vecsize;
322  region.SetSize(size);
323  image->SetRegions(region);
324  }
325  else
326  utlGlobalException(true, "not supported type: " + name);
327 }
328 
329 template <class ImageType>
330 std::vector<int>
331 GetVectorImage3DVolumeSize(const SmartPointer<ImageType>& image)
332 {
333  std::string name = image->GetNameOfClass();
334  std::vector<int> size(3, 1);
335  typename ImageType::SizeType imagesize = image->GetLargestPossibleRegion().GetSize();
336  if (name=="VectorImage" || name=="SpatiallyDenseSparseVectorImage")
337  {
338  for ( int i = 0; i < ImageType::ImageDimension; ++i )
339  {
340  utlException(i>2 && imagesize[i]!=1, "the image has more than 3D. Image size = " << imagesize);
341  size[i] = imagesize[i];
342  }
343  }
344  else if (name=="Image")
345  {
346  for ( int i = 0; i < utl::min<int>(ImageType::ImageDimension, 3); ++i )
347  size[i] = imagesize[i];
348  for ( int i = 4; i < ImageType::ImageDimension; ++i )
349  utlException(imagesize[i]!=1, "the image has more than 3D. Image size = " << imagesize);
350  }
351  else
352  utlGlobalException(true, "not supported type: " + name);
353  return size;
354 }
355 
356 template <class ImageType>
357 void
358 SetVectorImage3DVolumeSize(SmartPointer<ImageType>& image, const std::vector<int>& size)
359 {
360  std::string name = image->GetNameOfClass();
361  utlException(size.size()!=3, "need to have size 3");
362  typename ImageType::RegionType region = image->GetLargestPossibleRegion();
363  typename ImageType::SizeType imagesize = region.GetSize();
364  for ( int i = 0; i < utl::min<int>(ImageType::ImageDimension, size.size()); ++i )
365  imagesize[i] = size[i];
366  region.SetSize(imagesize);
367  image->SetRegions(region);
368 }
369 
371 template <class ImageType>
372 std::vector<int>
373 GetVectorImageFullSize(const SmartPointer<ImageType>& image)
374 {
375  std::string name = image->GetNameOfClass();
376  std::vector<int> size;
377  typename ImageType::SizeType imagesize = image->GetLargestPossibleRegion().GetSize();
378  const int dim = ImageType::ImageDimension;
379  if (name=="VectorImage" || name=="SpatiallyDenseSparseVectorImage")
380  {
381  size.resize(dim+1);
382  for ( int i = 0; i < dim; ++i )
383  size[i] = imagesize[i];
384  size[dim] = image->GetNumberOfComponentsPerPixel();
385  }
386  else if (name=="Image")
387  {
388  size.resize(dim);
389  for ( int i = 0; i < dim; ++i )
390  size[i] = imagesize[i];
391  }
392  else
393  utlGlobalException(true, "not supported type: " + name);
394  return size;
395 }
396 
398 template <class ImageType>
399 void
400 SetVectorImageFullSize(SmartPointer<ImageType>& image, const std::vector<int>& size)
401 {
402  std::string name = image->GetNameOfClass();
403  typename ImageType::RegionType region = image->GetLargestPossibleRegion();
404  typename ImageType::SizeType imagesize = region.GetSize();
405  const int dim = ImageType::ImageDimension;
406  if (name=="VectorImage" || name=="SpatiallyDenseSparseVectorImage")
407  {
408  utlSAException(size.size()!=dim+1)(size.size())(dim).msg("wrong size");
409  for ( int i = 0; i < dim; ++i )
410  imagesize[i] = size[i];
411  image->SetNumberOfComponentsPerPixel(size[dim]);
412  }
413  else if (name=="Image")
414  {
415  utlSAException(size.size()!=dim)(size.size())(dim).msg("wrong size");
416  for ( int i = 0; i < dim; ++i )
417  imagesize[i] = size[i];
418  }
419  else
420  utlGlobalException(true, "not supported type: " + name);
421  region.SetSize(imagesize);
422  image->SetRegions(region);
423 }
424 
425 inline bool
426 IsSparseImage(const std::string& filename)
427 {
428  std::string fileNoExt, ext;
429  utl::GetFileExtension(filename, ext, fileNoExt);
430  return ext=="spr";
431 }
432 
433 template <class ImageType >
434 bool
435 IsImageEmpty(const SmartPointer<ImageType>& image)
436 {
437  if (!image)
438  return true;
439 
440  typename ImageType::RegionType region = image->GetLargestPossibleRegion();
441  typename ImageType::SizeType size = region.GetSize();
442 
443  for ( int i = 0; i < ImageType::ImageDimension; i += 1 )
444  {
445  if (size[i]>0)
446  return false;
447  }
448  return true;
449 }
450 
452 template <class ImageType>
453 typename ImageType::Pointer
454 GenerateImage(const typename ImageType::SizeType& size, const int vectorLength=1)
455 {
456  typename ImageType::Pointer image = ImageType::New();
457  typename ImageType::RegionType region;
458  region.SetSize(size);
459  image->SetRegions(region);
460  image->SetNumberOfComponentsPerPixel(vectorLength);
461  image->Allocate();
462  return image;
463 }
464 
465 template <class ImageType>
466 typename ImageType::Pointer
467 GenerateImageFromSingleVoxel(const typename ImageType::PixelType& pixel)
468 {
469  typename ImageType::SizeType sizeVoxel;
470  typename ImageType::IndexType indexVoxel;
471  typedef typename ImageType::PixelType PixelType;
472  for ( int i = 0; i < ImageType::ImageDimension; ++i )
473  {
474  sizeVoxel[i] = 1;
475  indexVoxel[i] = 0;
476  }
477  typename ImageType::Pointer image = itk::GenerateImage<ImageType>(sizeVoxel, itk::NumericTraits<PixelType>::GetLength(pixel));
478  image->SetPixel(indexVoxel, pixel);
479  return image;
480 }
481 
482 template <class Image1Type, class Image2Type>
483 void
484 ImageToImage ( const SmartPointer<Image1Type>& image1, SmartPointer<Image2Type>& image2)
485 {
486  utlGlobalException(!image2, "image2 cannot be null");
487  image2->CopyInformation(image1);
488  image2->SetRegions(image1->GetLargestPossibleRegion());
489  image2->Allocate();
490  itk::ImageRegionConstIterator<Image1Type> it1(image1, image1->GetLargestPossibleRegion() );
491  itk::ImageRegionIterator<Image2Type> it2(image2, image2->GetLargestPossibleRegion() );
492 
493  it1.GoToBegin();
494  it2.GoToBegin();
495  while( !it1.IsAtEnd() )
496  {
497  it2.Set(it1.Get());
498  ++it2;
499  ++it1;
500  }
501 }
502 
503 template <class Image1Type, class Image2Type>
504 SmartPointer<Image2Type>
505 ImageToImage ( const SmartPointer<Image1Type>& image1 )
506 {
507  typename Image2Type::Pointer image2 = Image2Type::New();
508  ImageToImage<Image1Type, Image2Type>(image1, image2);
509  return image2;
510 }
511 
512 template <unsigned dimIn, unsigned dimOut>
513 void CopyImageRegion(const ImageRegion<dimIn>& regionIn, ImageRegion<dimOut>& regionOut, const int numberOfComponens=-1)
514 {
515  typename ImageRegion<dimIn>::SizeType sizeIn = regionIn.GetSize();
516  typename ImageRegion<dimIn>::IndexType indexIn = regionIn.GetIndex();
517 
518  typename ImageRegion<dimOut>::SizeType sizeOut = regionOut.GetSize();
519  typename ImageRegion<dimOut>::IndexType indexOut = regionOut.GetIndex();
520 
521  int dimension = utl::min((int)dimIn, (int)dimOut);
522 
523  for ( int i = 0; i < dimension; ++i )
524  {
525  sizeOut[i] = sizeIn[i];
526  indexOut[i] = indexIn[i];
527  }
528 
529  if ((int)dimIn < (int)dimOut)
530  {
531  for ( int i = dimension; i < dimOut; i += 1 )
532  {
533  if (dimension+1==dimOut )
534  {
535  utlSAException(numberOfComponens==-1)(numberOfComponens).msg("need to set numberOfComponens");
536  sizeOut[i] = numberOfComponens;
537  }
538  else
539  sizeOut[i] = 1;
540  indexOut[i] = 0;
541  }
542  }
543 
544  regionOut.SetSize(sizeOut);
545  regionOut.SetIndex(indexOut);
546 }
547 
548 
550 template <class ImageWithInfoType, class ImageType>
551 void
552 CopyImageInformation ( const SmartPointer<ImageWithInfoType>& imageFrom, SmartPointer<ImageType>& imageTo )
553 {
554  // utlException(ImageWithInfoType::ImageDimension < ImageType::ImageDimension, "the dimensions are not compatible");
555  int dimension = utl::min((int)ImageWithInfoType::ImageDimension, (int)ImageType::ImageDimension);
556  // typename ImageWithInfoType::PixelType inputImagePixelValue;
557  // typename ImageWithInfoType::IndexType inputImagePixelIndex;
558  typename ImageWithInfoType::SpacingType inputImageSpacing = imageFrom->GetSpacing();
559  typename ImageWithInfoType::RegionType inRegion = imageFrom->GetLargestPossibleRegion();
560  typename ImageWithInfoType::SizeType inputImageSize = inRegion.GetSize();
561  typename ImageWithInfoType::IndexType inputImageIndex = inRegion.GetIndex();
562  typename ImageWithInfoType::PointType inOrigin = imageFrom->GetOrigin();
563  typename ImageWithInfoType::DirectionType inDirection = imageFrom->GetDirection();
564 
565  typename ImageType::RegionType imageRegion;
566  typename ImageType::SizeType imageSize;
567  typename ImageType::PixelType imagePixelValue;
568  typename ImageType::IndexType imagePixelIndex;
569  typename ImageType::SpacingType imageSpacing;
570  typename ImageType::PointType imageOrigin;
571  typename ImageType::DirectionType imageDirection;
572 
573  for ( int i = 0; i < dimension; i += 1 )
574  {
575  imageSpacing[i] = inputImageSpacing[i];
576  imageSize[i] = inputImageSize[i];
577  imageOrigin[i] = inOrigin[i];
578  imagePixelIndex[i] = inputImageIndex[i];
579  for ( int j = 0; j < dimension; j += 1 )
580  imageDirection(i,j) = inDirection(i,j);
581  }
582 
583  if ((int)ImageWithInfoType::ImageDimension < (int)ImageType::ImageDimension)
584  {
585  for ( int i = dimension; i < ImageType::ImageDimension; i += 1 )
586  {
587  if (dimension+1==ImageType::ImageDimension && std::string(imageFrom->GetNameOfClass())=="VectorImage" && std::string(imageTo->GetNameOfClass())=="Image" )
588  imageSize[i] = imageFrom->GetNumberOfComponentsPerPixel();
589  else
590  imageSize[i] = 1;
591  imageSpacing[i] = 1;
592  imageOrigin[i] = 0;
593  imagePixelIndex[i] = 0;
594  imageDirection(i,i) = 1.0;
595  }
596  }
597 
598  imageRegion.SetSize(imageSize);
599  imageRegion.SetIndex(imagePixelIndex);
600 
601  imageTo->SetSpacing(imageSpacing);
602  imageTo->SetOrigin(imageOrigin);
603  imageTo->SetRegions(imageRegion);
604  imageTo->SetDirection(imageDirection);
605 
606  imageTo->SetNumberOfComponentsPerPixel(GetVectorImageVectorSize(imageFrom));
607 }
608 
610 template <class TPixelType, unsigned int VImageDimension >
611 void
612 PrintVectorImage(const SmartPointer<VectorImage<TPixelType, VImageDimension> >& image, const std::string& mse="", std::ostream& os=std::cout, bool isPrintHeader=false)
613 {
614  typedef VectorImage<TPixelType, VImageDimension> VectorImageType;
615  if (isPrintHeader)
616  image->Print(os<<mse);
617  ImageRegionIteratorWithIndex<VectorImageType> it(image, image->GetLargestPossibleRegion());
618  typename VectorImageType::IndexType index;
619  typename VectorImageType::PixelType pixel;
620  int numberOfComponent = image->GetNumberOfComponentsPerPixel();
621  it.GoToBegin();
622  while(!it.IsAtEnd())
623  {
624  pixel = it.Get();
625  std::vector<double> st = utl::GetContainerStats(pixel.GetDataPointer(), pixel.GetDataPointer()+pixel.GetSize());
626  if (std::fabs(st[0])>1e-8 || std::fabs(st[1])>1e-8)
627  {
628  index = it.GetIndex();
629  os << (mse==""?"VectorImage":mse) <<"(";
630  for ( int i = 0; i < VectorImageType::ImageDimension; i += 1 )
631  {
632  if (i==VectorImageType::ImageDimension-1)
633  os << index[i] << ")";
634  else
635  os << index[i] << ",";
636  }
637  char tmp[1024];
638  sprintf(tmp, " : size = %lu, stat = { %g, %g [%g], %g } : ", (long unsigned)pixel.GetSize(), st[0], st[2], st[3], st[1] );
639  os << std::string(tmp);
640  os << "[ ";
641  for ( int i = 0; i < numberOfComponent; i += 1 )
642  {
643  if (i==numberOfComponent-1)
644  os << pixel[i] << " ];" << std::endl;
645  else
646  os << pixel[i] << ", ";
647  }
648  }
649 
650  ++it;
651  }
652 }
653 
655 template <class TPixelType, unsigned int VImageDimension >
656 void
657 PrintImage3D(const SmartPointer<Image<TPixelType,VImageDimension> >& image, const std::string& mse="", std::ostream& os=std::cout, bool isPrintHeader=false)
658 {
659  utlGlobalException(VImageDimension>3, "dimension should be no more than 3!");
660  typedef Image<TPixelType, VImageDimension> ImageType;
661  if (isPrintHeader)
662  image->Print(os<<mse);
663 
664  ImageRegionIteratorWithIndex<ImageType> it(image, image->GetLargestPossibleRegion());
665  typename ImageType::IndexType index;
666  TPixelType pixel;
667  it.GoToBegin();
668  while(!it.IsAtEnd())
669  {
670  pixel = it.Get();
671  if (std::fabs(pixel)>1e-8)
672  {
673  index = it.GetIndex();
674  os << (mse==""?"Image":mse) <<"(";
675  for ( int i = 0; i < ImageType::ImageDimension; i += 1 )
676  {
677  if (i==ImageType::ImageDimension-1)
678  os << index[i] << ")";
679  else
680  os << index[i] << ",";
681  }
682  os << " : [ ";
683  os << pixel << " ];" << std::endl;
684  }
685  ++it;
686  }
687 }
688 
690 template <class TPixelType, unsigned int VImageDimension>
691 void
692 PrintImage4D(const SmartPointer<Image<TPixelType,VImageDimension> >& image, const std::string& mse="", std::ostream& os=std::cout, bool isPrintHeader=false)
693 {
694  utlGlobalException(VImageDimension!=4, "wrong image dimension");
695  typedef Image<TPixelType, VImageDimension> ImageType;
696  typename ImageType::RegionType region = image->GetLargestPossibleRegion();
697  typename ImageType::SizeType size = region.GetSize();
698 
699  if (isPrintHeader)
700  image->Print(os<<mse);
701 
702  typedef Image<TPixelType, 3> Image3DType;
703  typename Image3DType::Pointer image3D = Image3DType::New();
704  CopyImageInformation<ImageType, Image3DType>(image, image3D);
705 
706  ImageRegionIteratorWithIndex<Image3DType> it(image3D, image3D->GetLargestPossibleRegion());
707  typename Image3DType::IndexType index3D;
708  typename ImageType::IndexType index;
709  it.GoToBegin();
710  while(!it.IsAtEnd())
711  {
712  index3D = it.GetIndex();
713  std::vector<TPixelType> pixel;
714  for ( int i = 0; i < 3; i += 1 )
715  index[i] = index3D[i];
716  for ( int i = 0; i < size[3]; i += 1 )
717  {
718  index[3] = i;
719  pixel.push_back( image->GetPixel(index) );
720  }
721 
722  std::vector<double> st = utl::GetContainerStats(pixel.begin(), pixel.end());
723  if (std::fabs(st[0])>1e-8 || std::fabs(st[1])>1e-8)
724  {
725  os << (mse==""?"Image":mse) <<"(";
726  for ( int i = 0; i < 3; i += 1 )
727  {
728  if (i==2)
729  os << index3D[i] << ")";
730  else
731  os << index3D[i] << ",";
732  }
733  char tmp[1024];
734  sprintf(tmp, " : size = %lu, stat = { %g, %g [%g], %g } : ", (long unsigned)pixel.size(), st[0], st[2], st[3], st[1] );
735  os << std::string(tmp);
736  os << "[ ";
737  for ( int i = 0; i < size[3]; i += 1 )
738  {
739  if (i==size[3]-1)
740  os << pixel[i] << " ];" << std::endl;
741  else
742  os << pixel[i] << ", ";
743  }
744  }
745 
746  ++it;
747  }
748 }
749 
751 template <class TPixelType, unsigned int VImageDimension >
752 void
753 PrintImage(const SmartPointer<Image<TPixelType,VImageDimension> > image, const std::string& mse="", std::ostream& os=std::cout, bool isPrintHeader=false)
754 {
755  if (VImageDimension==4)
756  PrintImage4D<TPixelType, VImageDimension>(image, mse, os, isPrintHeader);
757  else if (VImageDimension<=3)
758  PrintImage3D<TPixelType, VImageDimension>(image, mse, os, isPrintHeader);
759  else
760  utlGlobalException(true, "image dimension is larger than 4");
761 }
762 
764 template <class Image1Type, class Image2Type>
765 bool
766 VerifyImageSize ( const SmartPointer<Image1Type>& image1, const SmartPointer<Image2Type>& image2, const bool isMinimalDimension=false )
767 {
768  int dimension;
769  if (isMinimalDimension)
770  dimension = utl::min((int)Image1Type::ImageDimension, (int)Image2Type::ImageDimension, 3);
771  else
772  {
773  if ((int)Image1Type::ImageDimension!=(int)Image2Type::ImageDimension)
774  return false;
775  dimension = Image1Type::ImageDimension;
776  }
777  typename Image1Type::RegionType region1 = image1->GetLargestPossibleRegion();
778  typename Image1Type::SizeType size1 = region1.GetSize();
779 
780  typename Image2Type::RegionType region2 = image2->GetLargestPossibleRegion();
781  typename Image2Type::SizeType size2 = region2.GetSize();
782 
783  for ( int i = 0; i < dimension; i += 1 )
784  {
785  if ( size1[i] != size2[i] )
786  return false;
787  }
788  return true;
789 }
790 
792 template <class Image1Type>
793 bool
794 VerifyImageSize ( const SmartPointer<Image1Type>& image1, const std::string& file2, const bool isMinimalDimension=false )
795 {
796  if (isMinimalDimension)
797  {
798  static const unsigned int ImageDimension = 4;
799  typedef double PixelType;
800  typedef VectorImage<PixelType, ImageDimension> Image2Type;
801 
802  typedef itk::ImageFileReader<Image2Type> Reader2Type;
803  typename Reader2Type::Pointer reader2 = Reader2Type::New();
804  reader2->SetFileName(file2);
805  reader2->UpdateOutputInformation();
806  typename Image2Type::Pointer image2 = reader2->GetOutput();
807 
808  return VerifyImageSize<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
809  }
810  else
811  {
812  typedef Image1Type Image2Type;
813  typedef itk::ImageFileReader<Image2Type> Reader2Type;
814  typename Reader2Type::Pointer reader2 = Reader2Type::New();
815  reader2->SetFileName(file2);
816  reader2->UpdateOutputInformation();
817  typename Image2Type::Pointer image2 = reader2->GetOutput();
818 
819  return VerifyImageSize<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
820  }
821 }
822 
824 inline bool
825 VerifyImageSize ( const std::string& file1, const std::string& file2, const bool isMinimalDimension=false )
826 {
827  static const unsigned int ImageDimension = 4;
828  typedef double PixelType;
829  typedef VectorImage<PixelType, ImageDimension> Image1Type;
830  typedef VectorImage<PixelType, ImageDimension> Image2Type;
831 
832  typedef itk::ImageFileReader<Image1Type> Reader1Type;
833  Reader1Type::Pointer reader1 = Reader1Type::New();
834  reader1->SetFileName(file1);
835  reader1->UpdateOutputInformation();
836  Image1Type::Pointer image1 = reader1->GetOutput();
837 
838  typedef itk::ImageFileReader<Image2Type> Reader2Type;
839  Reader2Type::Pointer reader2 = Reader2Type::New();
840  reader2->SetFileName(file2);
841  reader2->UpdateOutputInformation();
842  Image2Type::Pointer image2 = reader2->GetOutput();
843 
844  return VerifyImageSize<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
845 }
846 
848 template <class Image1Type, class Image2Type>
849 bool
850 VerifyImageInformation ( const SmartPointer<Image1Type>& image1, const SmartPointer<Image2Type>& image2, const bool isMinimalDimension=false )
851 {
852  int dimension;
853  if (isMinimalDimension)
854  {
855  dimension = utl::min((int)Image1Type::ImageDimension, (int)Image2Type::ImageDimension, 3);
856  }
857  else
858  {
859  if ((int)Image1Type::ImageDimension!=(int)Image2Type::ImageDimension)
860  return false;
861  dimension = Image1Type::ImageDimension;
862  }
863 
864  typename Image1Type::RegionType region1 = image1->GetLargestPossibleRegion();
865  typename Image1Type::SizeType size1 = region1.GetSize();
866  typename Image1Type::SpacingType spacing1 = image1->GetSpacing();
867  typename Image1Type::PointType origin1 = image1->GetOrigin();
868  typename Image1Type::DirectionType direction1 = image1->GetDirection();
869 
870  typename Image2Type::RegionType region2 = image2->GetLargestPossibleRegion();
871  typename Image2Type::SizeType size2 = region2.GetSize();
872  typename Image2Type::SpacingType spacing2 = image2->GetSpacing();
873  typename Image2Type::PointType origin2 = image2->GetOrigin();
874  typename Image2Type::DirectionType direction2 = image2->GetDirection();
875 
876  // image1->Print(std::cout<<"image1");
877  // image2->Print(std::cout<<"image2");
878  for ( int i = 0; i < dimension; i += 1 )
879  {
880  if ( (std::fabs(spacing1[i]-spacing2[i])>1e-6) || (std::fabs(size1[i]-size2[i])>1e-6) || (std::fabs(origin1[i]-origin2[i])>1e-6) )
881  return false;
882  for ( int j = 0; j < dimension; j += 1 )
883  {
884  if ( std::fabs(direction1(i,j) - direction2(i,j))>1e-6 )
885  return false;
886  }
887  }
888  return true;
889 }
890 
892 template <class Image1Type>
893 bool
894 VerifyImageInformation ( const SmartPointer<Image1Type>& image1, const std::string& file2, const bool isMinimalDimension=false )
895 {
896  if (isMinimalDimension)
897  {
898  static const unsigned int ImageDimension = 4;
899  typedef double PixelType;
900  typedef VectorImage<PixelType, ImageDimension> Image2Type;
901 
902  typedef itk::ImageFileReader<Image2Type> Reader2Type;
903  typename Reader2Type::Pointer reader2 = Reader2Type::New();
904  reader2->SetFileName(file2);
905  reader2->UpdateOutputInformation();
906  typename Image2Type::Pointer image2 = reader2->GetOutput();
907 
908  return VerifyImageInformation<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
909  }
910  else
911  {
912  typedef Image1Type Image2Type;
913  typedef itk::ImageFileReader<Image2Type> Reader2Type;
914  typename Reader2Type::Pointer reader2 = Reader2Type::New();
915  reader2->SetFileName(file2);
916  reader2->UpdateOutputInformation();
917  typename Image2Type::Pointer image2 = reader2->GetOutput();
918 
919  return VerifyImageInformation<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
920  }
921 }
922 
924 inline bool
925 VerifyImageInformation ( const std::string& file1, const std::string& file2, const bool isMinimalDimension=false )
926 {
927  static const unsigned int ImageDimension = 4;
928  typedef double PixelType;
929  typedef VectorImage<PixelType, ImageDimension> Image1Type;
930  typedef VectorImage<PixelType, ImageDimension> Image2Type;
931 
932  typedef itk::ImageFileReader<Image1Type> Reader1Type;
933  Reader1Type::Pointer reader1 = Reader1Type::New();
934  reader1->SetFileName(file1);
935  reader1->UpdateOutputInformation();
936  Image1Type::Pointer image1 = reader1->GetOutput();
937 
938  typedef itk::ImageFileReader<Image2Type> Reader2Type;
939  Reader2Type::Pointer reader2 = Reader2Type::New();
940  reader2->SetFileName(file2);
941  reader2->UpdateOutputInformation();
942  Image2Type::Pointer image2 = reader2->GetOutput();
943 
944  return VerifyImageInformation<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
945 }
946 
947 
948 template <class PointsContainer, class VnlValueType>
949 void
950 PointsContainerToVnlMatrix ( const PointsContainer& points, vnl_matrix<VnlValueType>& matrix )
951 {
952  matrix.clear();
953  typedef typename PointsContainer::ConstIterator PointsIterator;
954  typedef typename PointsContainer::Element PointType;
955 
956  const unsigned int pointDimension = PointType::PointDimension;
957  unsigned int numberOfPoints = points.Size();
958  if (numberOfPoints==0)
959  return;
960 
961  matrix.set_size(numberOfPoints, pointDimension);
962  PointsIterator iterator = points.Begin();
963  PointsIterator end = points.End();
964 
965  unsigned int count=0;
966  while( iterator != end )
967  {
968  PointType orientation = iterator.Value();
969  for (unsigned int k=0; k<pointDimension; k++)
970  matrix(count,k) = orientation[k];
971  iterator++;
972  count++;
973  }
974 }
975 
976 template <class VnlValueType, class PointsContainer>
977 void
978 VnlMatrixToPointsContainer ( const vnl_matrix<VnlValueType>& matrix, PointsContainer& points )
979 {
980  points.Initialize();
981  typedef typename PointsContainer::Element PointType;
982 
983  const unsigned int pointDimension = PointType::PointDimension;
984  utlGlobalException(pointDimension!=matrix.columns(), "wrong size of matrix or point dimension. pointDimension="<< pointDimension << ", matrix.columns()="<<matrix.columns());
985 
986  for ( int i = 0; i < matrix.rows(); i += 1 )
987  {
988  PointType point;
989  for ( int j = 0; j < matrix.columns(); j += 1 )
990  {
991  point[j] = matrix(i,j);
992  }
993  points.InsertElement(i, point);
994  }
995 }
996 
999 }
1000 
1001 #endif
1002 
bool Is3DImage(const std::string &filename)
Definition: utlITK.h:259
void CopyImageRegion(const ImageRegion< dimIn > &regionIn, ImageRegion< dimOut > &regionOut, const int numberOfComponens=-1)
Definition: utlITK.h:513
int GetImageType(const std::string &filename)
Definition: utlITK.h:203
bool IsImageEmpty(const SmartPointer< ImageType > &image)
Definition: utlITK.h:435
std::vector< double > GetContainerStats(IteratorType v1, IteratorType v2)
Definition: utlCore.h:921
void SetVectorImageVectorSize(const SmartPointer< ImageType > &image, const int vecsize)
Definition: utlITK.h:310
void PointsContainerToVnlMatrix(const PointsContainer &points, vnl_matrix< VnlValueType > &matrix)
Definition: utlITK.h:950
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
ImageType::Pointer GenerateImage(const typename ImageType::SizeType &size, const int vectorLength=1)
Definition: utlITK.h:454
void SetVectorImageFullSize(SmartPointer< ImageType > &image, const std::vector< int > &size)
Definition: utlITK.h:400
bool SaveImage(const SmartPointer< ImageType > &image, const std::string &filename, const std::string &printInfo="Writing Image:")
Definition: utlITK.h:157
bool IsSparseImage(const std::string &filename)
Definition: utlITK.h:426
bool ReadImageInformation(const std::string &filename, SmartPointer< ImageType > &image)
Definition: utlITK.h:87
void PrintVariableLengthVector(const VariableLengthVector< T >vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
Definition: utlITK.h:45
void PrintImage3D(const SmartPointer< Image< TPixelType, VImageDimension > > &image, const std::string &mse="", std::ostream &os=std::cout, bool isPrintHeader=false)
Definition: utlITK.h:657
const T & min(const T &a, const T &b)
Return the minimum between a and b.
Definition: utlCore.h:257
bool IsInstanceOf(const Object &o)
Definition: utlCore.h:169
bool IsLogNormal(const int level=utl::LogLevel)
Definition: utlCoreMacro.h:209
std::vector< int > GetVectorImageFullSize(const SmartPointer< ImageType > &image)
Definition: utlITK.h:373
bool ReadImage(const std::string &filename, SmartPointer< ImageType > &image, const std::string &printInfo="Reading Image:")
Definition: utlITK.h:110
void CopyImageInformation(const SmartPointer< ImageWithInfoType > &imageFrom, SmartPointer< ImageType > &imageTo)
Definition: utlITK.h:552
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
bool VerifyImageInformation(const SmartPointer< Image1Type > &image1, const SmartPointer< Image2Type > &image2, const bool isMinimalDimension=false)
Definition: utlITK.h:850
ImageType::Pointer GenerateImageFromSingleVoxel(const typename ImageType::PixelType &pixel)
Definition: utlITK.h:467
void PrintImage4D(const SmartPointer< Image< TPixelType, VImageDimension > > &image, const std::string &mse="", std::ostream &os=std::cout, bool isPrintHeader=false)
Definition: utlITK.h:692
void SetVectorImage3DVolumeSize(SmartPointer< ImageType > &image, const std::vector< int > &size)
Definition: utlITK.h:358
std::vector< int > GetVectorImage3DVolumeSize(const SmartPointer< ImageType > &image)
Definition: utlITK.h:331
void ImageToImage(const SmartPointer< Image1Type > &image1, SmartPointer< Image2Type > &image2)
Definition: utlITK.h:484
vnl_vector< T > VariableLengthVectorToVnlVector(const VariableLengthVector< T > &vec)
Definition: utlITK.h:76
#define utlSAException(expr)
Definition: utlCoreMacro.h:543
int GetVectorImageVectorSize(const SmartPointer< ImageType > &image, const int axis=-1)
Definition: utlITK.h:278
Created "06-10-2016.
bool IsEndingWith(const std::string &fullString, const std::string &ending)
Definition: utlCore.h:537
void PrintVectorImage(const SmartPointer< VectorImage< TPixelType, VImageDimension > > &image, const std::string &mse="", std::ostream &os=std::cout, bool isPrintHeader=false)
Definition: utlITK.h:612
bool VerifyImageSize(const SmartPointer< Image1Type > &image1, const SmartPointer< Image2Type > &image2, const bool isMinimalDimension=false)
Definition: utlITK.h:766
void VnlMatrixToPointsContainer(const vnl_matrix< VnlValueType > &matrix, PointsContainer &points)
Definition: utlITK.h:978
bool IsVectorImage(const std::string &filename)
Definition: utlITK.h:253
itk::VectorImage< ScalarType, 3 > VectorImageType
Definition: 4DImageMath.cxx:30
void PrintImage(const SmartPointer< Image< TPixelType, VImageDimension > > image, const std::string &mse="", std::ostream &os=std::cout, bool isPrintHeader=false)
Definition: utlITK.h:753
void GetFileExtension(const std::string &fileNameAbsolute, std::string &ext, std::string &fileNoExt)
Definition: utlCore.h:559
VariableLengthVector< T > VnlVectorToVariableLengthVector(const vnl_vector< T > &vec)
Definition: utlITK.h:66