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> 45 PrintVariableLengthVector(
const VariableLengthVector<T>vec,
const std::string& str=
"",
const char* separate=
" ", std::ostream& os=std::cout)
48 int NSize=vec.GetSize();
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);
54 os << strr <<
" = [ ";
55 for (
int i = 0; i < NSize-1; i += 1 )
56 os << vec[i] << separate;
57 os << vec[NSize-1] <<
" ];\n";
60 os << strr <<
" is empty vector" << std::endl;
65 VariableLengthVector<T>
68 VariableLengthVector<T> v(vec.size());
69 for (
int i = 0; i < vec.size(); i += 1 )
78 vnl_vector<T> v(vec.GetSize());
79 for (
int i = 0; i < vec.GetSize(); i += 1 )
85 template <
class ImageType>
89 typedef itk::ImageFileReader<ImageType> ReaderType;
90 typename ReaderType::Pointer reader = ReaderType::New();
92 reader->SetFileName(filename);
95 reader->UpdateOutputInformation();
97 catch (itk::ExceptionObject & err)
99 std::cout <<
"ExceptionObject caught !" << std::endl;
100 std::cout << err << std::endl;
103 image = reader->GetOutput();
108 template <
class ImageType>
110 ReadImage (
const std::string& filename, SmartPointer<ImageType>& image,
const std::string& printInfo=
"Reading Image:")
112 typedef itk::ImageFileReader<ImageType> ReaderType;
113 typename ReaderType::Pointer reader = ReaderType::New();
115 reader->SetFileName(filename);
119 std::cout << printInfo <<
" " << filename << std::endl;
122 catch (itk::ExceptionObject & err)
124 std::cout <<
"ExceptionObject caught !" << std::endl;
125 std::cout << err << std::endl;
128 image = reader->GetOutput();
132 template <
class ImageType,
class ReaderType>
134 ReadImage (
const std::string& filename, SmartPointer<ImageType>& image,
const std::string& printInfo=
"Reading Image:")
136 typename ReaderType::Pointer reader = ReaderType::New();
138 reader->SetFileName(filename);
142 std::cout << printInfo <<
" " << filename << std::endl;
145 catch (itk::ExceptionObject & err)
147 std::cout <<
"ExceptionObject caught !" << std::endl;
148 std::cout << err << std::endl;
151 image = reader->GetOutput();
155 template <
class ImageType>
157 SaveImage (
const SmartPointer<ImageType>& image,
const std::string& filename,
const std::string& printInfo=
"Writing Image:")
159 typedef itk::ImageFileWriter<ImageType> WriterType;
160 typename WriterType::Pointer writer = WriterType::New();
162 writer->SetFileName(filename);
163 writer->SetInput(image);
167 std::cout << printInfo <<
" " << filename << std::endl;
170 catch (itk::ExceptionObject & err)
172 std::cout <<
"ExceptionObject caught !" << std::endl;
173 std::cout << err << std::endl;
179 template <
class ImageType,
class WriterType>
181 SaveImage (
const SmartPointer<ImageType>& image,
const std::string& filename,
const std::string& printInfo=
"Writing Image:")
183 typename WriterType::Pointer writer = WriterType::New();
185 writer->SetFileName(filename);
186 writer->SetInput(image);
190 std::cout << printInfo <<
" " << filename << std::endl;
193 catch (itk::ExceptionObject & err)
195 std::cout <<
"ExceptionObject caught !" << std::endl;
196 std::cout << err << std::endl;
211 typedef itk::VectorImage<float, 4> MultiVolumeVectorImageType;
212 typedef itk::ImageFileReader<MultiVolumeVectorImageType> ReaderType;
214 ReaderType::Pointer reader = ReaderType::New();
215 reader->SetFileName(filename);
216 reader->UpdateOutputInformation();
217 MultiVolumeVectorImageType::Pointer image = reader->GetOutput();
219 unsigned int numberOfComponentsPerPixel = image->GetNumberOfComponentsPerPixel();
220 if (numberOfComponentsPerPixel > 1)
227 template <
class ImageType>
231 std::string name = image->GetNameOfClass();
232 if (name==
"VectorImage")
234 else if (name==
"Image")
236 typedef typename ImageType::PixelType PType;
237 if (std::is_scalar<PType>::value)
248 else if (name==
"SpatiallyDenseSparseVectorImage")
264 typedef itk::Image<float, 4> ImageType;
265 typedef itk::ImageFileReader<ImageType> ReaderType;
267 ReaderType::Pointer reader = ReaderType::New();
268 reader->SetFileName(filename);
269 reader->UpdateOutputInformation();
270 ImageType::Pointer image = reader->GetOutput();
272 typename ImageType::SizeType size = image->GetLargestPossibleRegion().GetSize();
276 template <
class ImageType>
280 std::string name = image->GetNameOfClass();
281 if (name==
"VectorImage" || name==
"SpatiallyDenseSparseVectorImage")
283 if (axis<0 || axis==ImageType::ImageDimension)
284 return image->GetNumberOfComponentsPerPixel();
287 typename ImageType::SizeType size = image->GetLargestPossibleRegion().GetSize();
291 else if (name==
"Image")
293 typename ImageType::SizeType size = image->GetLargestPossibleRegion().GetSize();
294 if (axis==ImageType::ImageDimension || (axis<0 && ImageType::ImageDimension<=3))
296 else if (axis<0 || axis==ImageType::ImageDimension-1)
297 return size[ImageType::ImageDimension-1];
308 template <
class ImageType>
312 std::string name = image->GetNameOfClass();
313 if (name==
"VectorImage" || name==
"SpatiallyDenseSparseVectorImage")
315 image->SetNumberOfComponentsPerPixel(vecsize);
317 else if (name==
"Image")
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);
329 template <
class ImageType>
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")
338 for (
int i = 0; i < ImageType::ImageDimension; ++i )
340 utlException(i>2 && imagesize[i]!=1,
"the image has more than 3D. Image size = " << imagesize);
341 size[i] = imagesize[i];
344 else if (name==
"Image")
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);
356 template <
class ImageType>
360 std::string name = image->GetNameOfClass();
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);
371 template <
class ImageType>
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")
382 for (
int i = 0; i < dim; ++i )
383 size[i] = imagesize[i];
384 size[dim] = image->GetNumberOfComponentsPerPixel();
386 else if (name==
"Image")
389 for (
int i = 0; i < dim; ++i )
390 size[i] = imagesize[i];
398 template <
class ImageType>
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")
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]);
413 else if (name==
"Image")
415 utlSAException(size.size()!=dim)(size.size())(dim).msg(
"wrong size");
416 for (
int i = 0; i < dim; ++i )
417 imagesize[i] = size[i];
421 region.SetSize(imagesize);
422 image->SetRegions(region);
428 std::string fileNoExt, ext;
433 template <
class ImageType >
440 typename ImageType::RegionType region = image->GetLargestPossibleRegion();
441 typename ImageType::SizeType size = region.GetSize();
443 for (
int i = 0; i < ImageType::ImageDimension; i += 1 )
452 template <
class ImageType>
453 typename ImageType::Pointer
454 GenerateImage(
const typename ImageType::SizeType& size,
const int vectorLength=1)
456 typename ImageType::Pointer image = ImageType::New();
457 typename ImageType::RegionType region;
458 region.SetSize(size);
459 image->SetRegions(region);
460 image->SetNumberOfComponentsPerPixel(vectorLength);
465 template <
class ImageType>
466 typename ImageType::Pointer
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 )
477 typename ImageType::Pointer image = itk::GenerateImage<ImageType>(sizeVoxel, itk::NumericTraits<PixelType>::GetLength(pixel));
478 image->SetPixel(indexVoxel, pixel);
482 template <
class Image1Type,
class Image2Type>
484 ImageToImage (
const SmartPointer<Image1Type>& image1, SmartPointer<Image2Type>& image2)
487 image2->CopyInformation(image1);
488 image2->SetRegions(image1->GetLargestPossibleRegion());
490 itk::ImageRegionConstIterator<Image1Type> it1(image1, image1->GetLargestPossibleRegion() );
491 itk::ImageRegionIterator<Image2Type> it2(image2, image2->GetLargestPossibleRegion() );
495 while( !it1.IsAtEnd() )
503 template <
class Image1Type,
class Image2Type>
504 SmartPointer<Image2Type>
507 typename Image2Type::Pointer image2 = Image2Type::New();
508 ImageToImage<Image1Type, Image2Type>(image1, image2);
512 template <
unsigned dimIn,
unsigned dimOut>
513 void CopyImageRegion(
const ImageRegion<dimIn>& regionIn, ImageRegion<dimOut>& regionOut,
const int numberOfComponens=-1)
515 typename ImageRegion<dimIn>::SizeType sizeIn = regionIn.GetSize();
516 typename ImageRegion<dimIn>::IndexType indexIn = regionIn.GetIndex();
518 typename ImageRegion<dimOut>::SizeType sizeOut = regionOut.GetSize();
519 typename ImageRegion<dimOut>::IndexType indexOut = regionOut.GetIndex();
521 int dimension =
utl::min((
int)dimIn, (
int)dimOut);
523 for (
int i = 0; i < dimension; ++i )
525 sizeOut[i] = sizeIn[i];
526 indexOut[i] = indexIn[i];
529 if ((
int)dimIn < (
int)dimOut)
531 for (
int i = dimension; i < dimOut; i += 1 )
533 if (dimension+1==dimOut )
535 utlSAException(numberOfComponens==-1)(numberOfComponens).msg(
"need to set numberOfComponens");
536 sizeOut[i] = numberOfComponens;
544 regionOut.SetSize(sizeOut);
545 regionOut.SetIndex(indexOut);
550 template <
class ImageWithInfoType,
class ImageType>
555 int dimension =
utl::min((
int)ImageWithInfoType::ImageDimension, (
int)ImageType::ImageDimension);
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();
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;
573 for (
int i = 0; i < dimension; i += 1 )
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);
583 if ((
int)ImageWithInfoType::ImageDimension < (
int)ImageType::ImageDimension)
585 for (
int i = dimension; i < ImageType::ImageDimension; i += 1 )
587 if (dimension+1==ImageType::ImageDimension && std::string(imageFrom->GetNameOfClass())==
"VectorImage" && std::string(imageTo->GetNameOfClass())==
"Image" )
588 imageSize[i] = imageFrom->GetNumberOfComponentsPerPixel();
593 imagePixelIndex[i] = 0;
594 imageDirection(i,i) = 1.0;
598 imageRegion.SetSize(imageSize);
599 imageRegion.SetIndex(imagePixelIndex);
601 imageTo->SetSpacing(imageSpacing);
602 imageTo->SetOrigin(imageOrigin);
603 imageTo->SetRegions(imageRegion);
604 imageTo->SetDirection(imageDirection);
610 template <
class TPixelType,
unsigned int VImageDimension >
612 PrintVectorImage(
const SmartPointer<VectorImage<TPixelType, VImageDimension> >& image,
const std::string& mse=
"", std::ostream& os=std::cout,
bool isPrintHeader=
false)
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();
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)
628 index = it.GetIndex();
629 os << (mse==
""?
"VectorImage":mse) <<
"(";
630 for (
int i = 0; i < VectorImageType::ImageDimension; i += 1 )
632 if (i==VectorImageType::ImageDimension-1)
633 os << index[i] <<
")";
635 os << index[i] <<
",";
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);
641 for (
int i = 0; i < numberOfComponent; i += 1 )
643 if (i==numberOfComponent-1)
644 os << pixel[i] <<
" ];" << std::endl;
646 os << pixel[i] <<
", ";
655 template <
class TPixelType,
unsigned int VImageDimension >
657 PrintImage3D(
const SmartPointer<Image<TPixelType,VImageDimension> >& image,
const std::string& mse=
"", std::ostream& os=std::cout,
bool isPrintHeader=
false)
660 typedef Image<TPixelType, VImageDimension> ImageType;
662 image->Print(os<<mse);
664 ImageRegionIteratorWithIndex<ImageType> it(image, image->GetLargestPossibleRegion());
665 typename ImageType::IndexType index;
671 if (std::fabs(pixel)>1e-8)
673 index = it.GetIndex();
674 os << (mse==
""?
"Image":mse) <<
"(";
675 for (
int i = 0; i < ImageType::ImageDimension; i += 1 )
677 if (i==ImageType::ImageDimension-1)
678 os << index[i] <<
")";
680 os << index[i] <<
",";
683 os << pixel <<
" ];" << std::endl;
690 template <
class TPixelType,
unsigned int VImageDimension>
692 PrintImage4D(
const SmartPointer<Image<TPixelType,VImageDimension> >& image,
const std::string& mse=
"", std::ostream& os=std::cout,
bool isPrintHeader=
false)
695 typedef Image<TPixelType, VImageDimension> ImageType;
696 typename ImageType::RegionType region = image->GetLargestPossibleRegion();
697 typename ImageType::SizeType size = region.GetSize();
700 image->Print(os<<mse);
702 typedef Image<TPixelType, 3> Image3DType;
703 typename Image3DType::Pointer image3D = Image3DType::New();
704 CopyImageInformation<ImageType, Image3DType>(image, image3D);
706 ImageRegionIteratorWithIndex<Image3DType> it(image3D, image3D->GetLargestPossibleRegion());
707 typename Image3DType::IndexType index3D;
708 typename ImageType::IndexType index;
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 )
719 pixel.push_back( image->GetPixel(index) );
723 if (std::fabs(st[0])>1e-8 || std::fabs(st[1])>1e-8)
725 os << (mse==
""?
"Image":mse) <<
"(";
726 for (
int i = 0; i < 3; i += 1 )
729 os << index3D[i] <<
")";
731 os << index3D[i] <<
",";
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);
737 for (
int i = 0; i < size[3]; i += 1 )
740 os << pixel[i] <<
" ];" << std::endl;
742 os << pixel[i] <<
", ";
751 template <
class TPixelType,
unsigned int VImageDimension >
753 PrintImage(
const SmartPointer<Image<TPixelType,VImageDimension> > image,
const std::string& mse=
"", std::ostream& os=std::cout,
bool isPrintHeader=
false)
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);
764 template <
class Image1Type,
class Image2Type>
766 VerifyImageSize (
const SmartPointer<Image1Type>& image1,
const SmartPointer<Image2Type>& image2,
const bool isMinimalDimension=
false )
769 if (isMinimalDimension)
770 dimension =
utl::min((
int)Image1Type::ImageDimension, (
int)Image2Type::ImageDimension, 3);
773 if ((
int)Image1Type::ImageDimension!=(
int)Image2Type::ImageDimension)
775 dimension = Image1Type::ImageDimension;
777 typename Image1Type::RegionType region1 = image1->GetLargestPossibleRegion();
778 typename Image1Type::SizeType size1 = region1.GetSize();
780 typename Image2Type::RegionType region2 = image2->GetLargestPossibleRegion();
781 typename Image2Type::SizeType size2 = region2.GetSize();
783 for (
int i = 0; i < dimension; i += 1 )
785 if ( size1[i] != size2[i] )
792 template <
class Image1Type>
794 VerifyImageSize (
const SmartPointer<Image1Type>& image1,
const std::string& file2,
const bool isMinimalDimension=
false )
796 if (isMinimalDimension)
798 static const unsigned int ImageDimension = 4;
799 typedef double PixelType;
800 typedef VectorImage<PixelType, ImageDimension> Image2Type;
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();
808 return VerifyImageSize<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
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();
819 return VerifyImageSize<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
825 VerifyImageSize (
const std::string& file1,
const std::string& file2,
const bool isMinimalDimension=
false )
827 static const unsigned int ImageDimension = 4;
828 typedef double PixelType;
829 typedef VectorImage<PixelType, ImageDimension> Image1Type;
830 typedef VectorImage<PixelType, ImageDimension> Image2Type;
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();
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();
844 return VerifyImageSize<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
848 template <
class Image1Type,
class Image2Type>
850 VerifyImageInformation (
const SmartPointer<Image1Type>& image1,
const SmartPointer<Image2Type>& image2,
const bool isMinimalDimension=
false )
853 if (isMinimalDimension)
855 dimension =
utl::min((
int)Image1Type::ImageDimension, (
int)Image2Type::ImageDimension, 3);
859 if ((
int)Image1Type::ImageDimension!=(
int)Image2Type::ImageDimension)
861 dimension = Image1Type::ImageDimension;
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();
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();
878 for (
int i = 0; i < dimension; i += 1 )
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) )
882 for (
int j = 0; j < dimension; j += 1 )
884 if ( std::fabs(direction1(i,j) - direction2(i,j))>1e-6 )
892 template <
class Image1Type>
894 VerifyImageInformation (
const SmartPointer<Image1Type>& image1,
const std::string& file2,
const bool isMinimalDimension=
false )
896 if (isMinimalDimension)
898 static const unsigned int ImageDimension = 4;
899 typedef double PixelType;
900 typedef VectorImage<PixelType, ImageDimension> Image2Type;
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();
908 return VerifyImageInformation<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
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();
919 return VerifyImageInformation<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
927 static const unsigned int ImageDimension = 4;
928 typedef double PixelType;
929 typedef VectorImage<PixelType, ImageDimension> Image1Type;
930 typedef VectorImage<PixelType, ImageDimension> Image2Type;
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();
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();
944 return VerifyImageInformation<Image1Type, Image2Type> ( image1, image2, isMinimalDimension);
948 template <
class Po
intsContainer,
class VnlValueType>
953 typedef typename PointsContainer::ConstIterator PointsIterator;
954 typedef typename PointsContainer::Element PointType;
956 const unsigned int pointDimension = PointType::PointDimension;
957 unsigned int numberOfPoints = points.Size();
958 if (numberOfPoints==0)
961 matrix.set_size(numberOfPoints, pointDimension);
962 PointsIterator iterator = points.Begin();
963 PointsIterator end = points.End();
965 unsigned int count=0;
966 while( iterator != end )
968 PointType orientation = iterator.Value();
969 for (
unsigned int k=0; k<pointDimension; k++)
970 matrix(count,k) = orientation[k];
976 template <
class VnlValueType,
class Po
intsContainer>
981 typedef typename PointsContainer::Element PointType;
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());
986 for (
int i = 0; i < matrix.rows(); i += 1 )
989 for (
int j = 0; j < matrix.columns(); j += 1 )
991 point[j] = matrix(i,j);
993 points.InsertElement(i, point);
bool Is3DImage(const std::string &filename)
void CopyImageRegion(const ImageRegion< dimIn > ®ionIn, ImageRegion< dimOut > ®ionOut, const int numberOfComponens=-1)
int GetImageType(const std::string &filename)
bool IsImageEmpty(const SmartPointer< ImageType > &image)
std::vector< double > GetContainerStats(IteratorType v1, IteratorType v2)
void SetVectorImageVectorSize(const SmartPointer< ImageType > &image, const int vecsize)
void PointsContainerToVnlMatrix(const PointsContainer &points, vnl_matrix< VnlValueType > &matrix)
#define utlException(cond, expout)
ImageType::Pointer GenerateImage(const typename ImageType::SizeType &size, const int vectorLength=1)
void SetVectorImageFullSize(SmartPointer< ImageType > &image, const std::vector< int > &size)
bool SaveImage(const SmartPointer< ImageType > &image, const std::string &filename, const std::string &printInfo="Writing Image:")
bool IsSparseImage(const std::string &filename)
bool ReadImageInformation(const std::string &filename, SmartPointer< ImageType > &image)
void PrintVariableLengthVector(const VariableLengthVector< T >vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
void PrintImage3D(const SmartPointer< Image< TPixelType, VImageDimension > > &image, const std::string &mse="", std::ostream &os=std::cout, bool isPrintHeader=false)
const T & min(const T &a, const T &b)
Return the minimum between a and b.
bool IsInstanceOf(const Object &o)
bool IsLogNormal(const int level=utl::LogLevel)
std::vector< int > GetVectorImageFullSize(const SmartPointer< ImageType > &image)
bool ReadImage(const std::string &filename, SmartPointer< ImageType > &image, const std::string &printInfo="Reading Image:")
void CopyImageInformation(const SmartPointer< ImageWithInfoType > &imageFrom, SmartPointer< ImageType > &imageTo)
#define utlGlobalException(cond, expout)
bool VerifyImageInformation(const SmartPointer< Image1Type > &image1, const SmartPointer< Image2Type > &image2, const bool isMinimalDimension=false)
ImageType::Pointer GenerateImageFromSingleVoxel(const typename ImageType::PixelType &pixel)
void PrintImage4D(const SmartPointer< Image< TPixelType, VImageDimension > > &image, const std::string &mse="", std::ostream &os=std::cout, bool isPrintHeader=false)
void SetVectorImage3DVolumeSize(SmartPointer< ImageType > &image, const std::vector< int > &size)
std::vector< int > GetVectorImage3DVolumeSize(const SmartPointer< ImageType > &image)
void ImageToImage(const SmartPointer< Image1Type > &image1, SmartPointer< Image2Type > &image2)
vnl_vector< T > VariableLengthVectorToVnlVector(const VariableLengthVector< T > &vec)
#define utlSAException(expr)
int GetVectorImageVectorSize(const SmartPointer< ImageType > &image, const int axis=-1)
bool IsEndingWith(const std::string &fullString, const std::string &ending)
void PrintVectorImage(const SmartPointer< VectorImage< TPixelType, VImageDimension > > &image, const std::string &mse="", std::ostream &os=std::cout, bool isPrintHeader=false)
bool VerifyImageSize(const SmartPointer< Image1Type > &image1, const SmartPointer< Image2Type > &image2, const bool isMinimalDimension=false)
void VnlMatrixToPointsContainer(const vnl_matrix< VnlValueType > &matrix, PointsContainer &points)
bool IsVectorImage(const std::string &filename)
itk::VectorImage< ScalarType, 3 > VectorImageType
void PrintImage(const SmartPointer< Image< TPixelType, VImageDimension > > image, const std::string &mse="", std::ostream &os=std::cout, bool isPrintHeader=false)
void GetFileExtension(const std::string &fileNameAbsolute, std::string &ext, std::string &fileNoExt)
VariableLengthVector< T > VnlVectorToVariableLengthVector(const vnl_vector< T > &vec)