]> Creatis software - clitk.git/commitdiff
Adapted crop for vector fields with partial specialization to generate
authorSimon Rit <simon.rit@creatis.insa-lyon.fr>
Wed, 4 Jan 2012 15:48:10 +0000 (16:48 +0100)
committerSimon Rit <simon.rit@creatis.insa-lyon.fr>
Wed, 4 Jan 2012 15:48:10 +0000 (16:48 +0100)
exceptions for AutoCrop of VF

itk/clitkCropLikeImageFilter.txx
tools/clitkCropImageGenericFilter.cxx
tools/clitkCropImageGenericFilter.h

index 93644a57c57f9a4a664de713aaab5524b603b06d..519c5fd4e6a8f3015112e081c152dc4c9844f595 100644 (file)
@@ -30,7 +30,7 @@ CropLikeImageFilter():itk::ImageToImageFilter<ImageType, ImageType>() {
   this->SetNumberOfRequiredInputs(1);
   m_LikeImage = NULL;
   m_LikeFilenameIsGiven = false;
-  this->SetBackgroundValue(0);
+  this->SetBackgroundValue(typename PixelTraits<typename ImageType::PixelType>::ValueType(0));
   m_CropAlongThisDimension.resize(ImageType::ImageDimension);
   for(uint i=0; i<ImageType::ImageDimension; i++)
     m_CropAlongThisDimension[i] = true;
index aeae1f988806c100c6198f407f59baefc5134222..d46f20e461a02f8a90c4e07784049b03b1ad6225 100644 (file)
 #include "clitkCropImageGenericFilter.h"
 
 
-namespace clitk
+//-----------------------------------------------------------
+// Constructor
+//-----------------------------------------------------------
+clitk::CropImageGenericFilter::CropImageGenericFilter():
+  ImageToImageGenericFilter<Self>("CropImage")
 {
+  cmdline_parser_clitkCropImage_init(&mArgsInfo);
+  InitializeImageType<2>();
+  InitializeImageType<3>();
+  InitializeImageType<4>();
+}
+
+//--------------------------------------------------------------------
+template<unsigned int Dim>
+void clitk::CropImageGenericFilter::InitializeImageType()
+{
+  ADD_DEFAULT_IMAGE_TYPES(Dim);
+  ADD_DEFAULT_VEC_IMAGE_TYPES;
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+void clitk::CropImageGenericFilter::SetArgsInfo(const args_info_type& a)
+{
+  mArgsInfo=a;
+  SetIOVerbose(mArgsInfo.verbose_flag);
+  if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
+  if (mArgsInfo.input_given)   AddInputFilename(mArgsInfo.input_arg);
+  if (mArgsInfo.output_given)  AddOutputFilename(mArgsInfo.output_arg);
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+template <class ImageType>
+typename clitk::CropImageGenericFilter::AutoCrop<ImageType>::ImagePointer
+clitk::CropImageGenericFilter::AutoCrop<ImageType>::Do(args_info_type &argsInfo, ImagePointer input)
+{
+  static const unsigned int PixelDimension = itk::PixelTraits<typename ImageType::PixelType>::Dimension;
+  return this->Do(argsInfo, input, static_cast< PixelDimType<PixelDimension> *>(NULL) );
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+template <class ImageType>
+template<unsigned int Dim>
+typename clitk::CropImageGenericFilter::AutoCrop<ImageType>::ImagePointer
+clitk::CropImageGenericFilter::AutoCrop<ImageType>::Do(args_info_type &, ImagePointer, PixelDimType<Dim> *)
+{
+  clitkExceptionMacro("Autocrop is not implemented for vector fields");
+  return NULL;
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+template <class ImageType>
+typename clitk::CropImageGenericFilter::AutoCrop<ImageType>::ImagePointer
+clitk::CropImageGenericFilter::AutoCrop<ImageType>::Do(args_info_type &argsInfo, ImagePointer input, PixelDimType<1> *)
+{
+  if (argsInfo.boundingBox_given)
+    clitkExceptionMacro("Do not use --BG and --boundingBox at the same time");
+  if (argsInfo.lower_given)
+    clitkExceptionMacro("Do not use --BG and --lower at the same time");
+  if (argsInfo.upper_given)
+    clitkExceptionMacro("Do not use --BG and --upper at the same time");
+  typedef clitk::AutoCropFilter<ImageType> FilterType;
+  typename FilterType::Pointer filter = FilterType::New();
+  filter->SetInput(input);
+  filter->SetBackgroundValue(argsInfo.BG_arg);
+  filter->Update();
+  return filter->GetOutput();
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+// Update with the number of dimensions and the pixeltype
+//--------------------------------------------------------------------
+template<class ImageType>
+void clitk::CropImageGenericFilter::UpdateWithInputImageType()
+{
+  // Reading input
+  typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
+  typename ImageType::RegionType input_region = input->GetLargestPossibleRegion();
 
+  // Check options
+  if (mArgsInfo.BG_given && mArgsInfo.like_given)
+    clitkExceptionMacro("Do not use --BG and --like at the same time");
 
-  //-----------------------------------------------------------
-  // Constructor
-  //-----------------------------------------------------------
-  CropImageGenericFilter::CropImageGenericFilter():
-    ImageToImageGenericFilter<Self>("CropImage")
-  {
-    cmdline_parser_clitkCropImage_init(&mArgsInfo);
-    InitializeImageType<2>();
-    InitializeImageType<3>();
-    InitializeImageType<4>();
-  }
+  // Prepare output
+  typename ImageType::Pointer output;
 
-  //--------------------------------------------------------------------
-  template<unsigned int Dim>
-  void clitk::CropImageGenericFilter::InitializeImageType()
-  {
-    ADD_DEFAULT_IMAGE_TYPES(Dim);
-    //ADD_IMAGE_TYPE(Dim, uchar);
-    //ADD_IMAGE_TYPE(Dim, short);
-    // ADD_IMAGE_TYPE(Dim, uint);
-    //  ADD_IMAGE_TYPE(Dim, ulong);
-    // ADD_IMAGE_TYPE(Dim, int);
-    // ADD_IMAGE_TYPE(Dim, float);
+  // ------------------------------------------------
+  if (mArgsInfo.BG_given) { // AutoCrop filter
+    AutoCrop<ImageType> autoCrop;
+    output = autoCrop.Do(mArgsInfo, input);
   }
-  //--------------------------------------------------------------------
-
-  //--------------------------------------------------------------------
-  void clitk::CropImageGenericFilter::SetArgsInfo(const args_info_type& a) 
-  {
-    mArgsInfo=a;
-    SetIOVerbose(mArgsInfo.verbose_flag);
-    if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
-    if (mArgsInfo.input_given)   AddInputFilename(mArgsInfo.input_arg);
-    if (mArgsInfo.output_given)  AddOutputFilename(mArgsInfo.output_arg);
-  }
-  //--------------------------------------------------------------------
-
-  //--------------------------------------------------------------------
-  // Update with the number of dimensions and the pixeltype
-  //--------------------------------------------------------------------
-  template<class ImageType>
-  void clitk::CropImageGenericFilter::UpdateWithInputImageType() 
-  { 
-    // Reading input
-    typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
-    typename ImageType::RegionType input_region = input->GetLargestPossibleRegion();
-
-    // Check options
-    if (mArgsInfo.BG_given && mArgsInfo.like_given)
-      clitkExceptionMacro("Do not use --BG and --like at the same time");    
-
-    // Prepare output
-    typename ImageType::Pointer output;
-    
+  else {
     // ------------------------------------------------
-    if (mArgsInfo.BG_given) { // AutoCrop filter
-      if (mArgsInfo.boundingBox_given) 
-        clitkExceptionMacro("Do not use --BG and --boundingBox at the same time");    
-      if (mArgsInfo.lower_given) 
-        clitkExceptionMacro("Do not use --BG and --lower at the same time");    
-      if (mArgsInfo.upper_given) 
-        clitkExceptionMacro("Do not use --BG and --upper at the same time");    
-      typedef clitk::AutoCropFilter<ImageType> FilterType;
+    if (mArgsInfo.like_given) { // CropLike filter
+    if (mArgsInfo.boundingBox_given)
+      clitkExceptionMacro("Do not use --like and --boundingBox at the same time");
+    if (mArgsInfo.lower_given)
+      clitkExceptionMacro("Do not use --like and --lower at the same time");
+    if (mArgsInfo.upper_given)
+      clitkExceptionMacro("Do not use --like and --upper at the same time");
+      typedef clitk::CropLikeImageFilter<ImageType> FilterType;
       typename FilterType::Pointer filter = FilterType::New();
       filter->SetInput(input);
-      filter->SetBackgroundValue(mArgsInfo.BG_arg);
+      filter->SetCropLikeFilename(mArgsInfo.like_arg);
+      filter->SetBackgroundValue(mArgsInfo.BGLike_arg);
       filter->Update();
       output = filter->GetOutput();
     }
     else {
       // ------------------------------------------------
-      if (mArgsInfo.like_given) { // CropLike filter
-      if (mArgsInfo.boundingBox_given) 
-        clitkExceptionMacro("Do not use --like and --boundingBox at the same time");    
-      if (mArgsInfo.lower_given) 
-        clitkExceptionMacro("Do not use --like and --lower at the same time");    
-      if (mArgsInfo.upper_given) 
-        clitkExceptionMacro("Do not use --like and --upper at the same time");    
-        typedef clitk::CropLikeImageFilter<ImageType> FilterType;
-        typename FilterType::Pointer filter = FilterType::New();
-        filter->SetInput(input);
-        filter->SetCropLikeFilename(mArgsInfo.like_arg);
-        filter->SetBackgroundValue(mArgsInfo.BGLike_arg);
-        filter->Update();
-        output = filter->GetOutput();
+      typename ImageType::SizeType lSize;
+      typename ImageType::SizeType uSize;
+      if (mArgsInfo.verbose_flag) std::cout << "input region " << input_region << std::endl;
+      if (mArgsInfo.boundingBox_given) {
+        for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
+          lSize[i] = mArgsInfo.boundingBox_arg[2*i];
+          uSize[i] = input_region.GetSize()[i]-mArgsInfo.boundingBox_arg[2*i+1]-1;
+        }
       }
       else {
-        // ------------------------------------------------
-        typename ImageType::SizeType lSize;
-        typename ImageType::SizeType uSize;
-        if (mArgsInfo.verbose_flag) std::cout << "input region " << input_region << std::endl;
-        if (mArgsInfo.boundingBox_given) {
-          for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
-            lSize[i] = mArgsInfo.boundingBox_arg[2*i];
-            uSize[i] = input_region.GetSize()[i]-mArgsInfo.boundingBox_arg[2*i+1]-1;
-          }
+        if (mArgsInfo.lower_given) {
+          for(unsigned int i=0; i<ImageType::ImageDimension; i++)
+            lSize[i]=static_cast<unsigned int >(mArgsInfo.lower_arg[i]);
         }
-        else {
-          if (mArgsInfo.lower_given) {
-            for(unsigned int i=0; i<ImageType::ImageDimension; i++) 
-              lSize[i]=static_cast<unsigned int >(mArgsInfo.lower_arg[i]);
-          }
-          else lSize.Fill(0);
-          if (mArgsInfo.upper_given) {
-            for(unsigned int i=0; i<ImageType::ImageDimension; i++)
-              uSize[i]=static_cast<unsigned int >(mArgsInfo.upper_arg[i]);
-          }
-          else uSize.Fill(0);
+        else lSize.Fill(0);
+        if (mArgsInfo.upper_given) {
+          for(unsigned int i=0; i<ImageType::ImageDimension; i++)
+            uSize[i]=static_cast<unsigned int >(mArgsInfo.upper_arg[i]);
         }
-        
-        if (mArgsInfo.verbose_flag) {
-          std::cout << "lower " << lSize << " upper " << uSize << std::endl;
-        }
-        
-        typedef  itk::CropImageFilter<ImageType, ImageType> CropImageFilterType;
-        typename CropImageFilterType::Pointer filter=CropImageFilterType::New();
-        filter->SetInput(input);
-        filter->SetLowerBoundaryCropSize(lSize);
-        filter->SetUpperBoundaryCropSize(uSize);
-        filter->Update();
-        output = filter->GetOutput();    
+        else uSize.Fill(0);
       }
-    }
 
-    // Force origin if needed
-    if (mArgsInfo.origin_flag) {
-      typename ImageType::PointType origin;
-      origin.Fill(itk::NumericTraits<double>::Zero);
-      output->SetOrigin(origin);
+      if (mArgsInfo.verbose_flag) {
+        std::cout << "lower " << lSize << " upper " << uSize << std::endl;
+      }
+
+      typedef  itk::CropImageFilter<ImageType, ImageType> CropImageFilterType;
+      typename CropImageFilterType::Pointer filter=CropImageFilterType::New();
+      filter->SetInput(input);
+      filter->SetLowerBoundaryCropSize(lSize);
+      filter->SetUpperBoundaryCropSize(uSize);
+      filter->Update();
+      output = filter->GetOutput();
     }
-    
-    // adjust image origin and force index to zero 
-    typename ImageType::RegionType region = output->GetLargestPossibleRegion();
-    typename ImageType::IndexType index = region.GetIndex();
-    typename ImageType::PointType origin = output->GetOrigin();
-    typename ImageType::SpacingType spacing = output->GetSpacing();
-    if (mArgsInfo.verbose_flag) std::cout << "origin before crop " << origin << std::endl;
-    for (unsigned int i = 0; i < output->GetImageDimension(); i++)
-      origin[i] += index[i]*spacing[i];
-    if (mArgsInfo.verbose_flag) std::cout << "origin after crop " << origin << std::endl;
-    output->SetOrigin(origin);
+  }
 
-    index.Fill(itk::NumericTraits<double>::Zero);
-    region.SetIndex(index);
-    output->SetRegions(region);
-    
-    // Write/Save results
-    this->template SetNextOutput<ImageType>(output); 
+  // Force origin if needed
+  if (mArgsInfo.origin_flag) {
+    typename ImageType::PointType origin;
+    origin.Fill(itk::NumericTraits<double>::Zero);
+    output->SetOrigin(origin);
   }
-  //--------------------------------------------------------------------
 
-} //end clitk
+  // adjust image origin and force index to zero
+  typename ImageType::RegionType region = output->GetLargestPossibleRegion();
+  typename ImageType::IndexType index = region.GetIndex();
+  typename ImageType::PointType origin = output->GetOrigin();
+  typename ImageType::SpacingType spacing = output->GetSpacing();
+  if (mArgsInfo.verbose_flag) std::cout << "origin before crop " << origin << std::endl;
+  for (unsigned int i = 0; i < output->GetImageDimension(); i++)
+    origin[i] += index[i]*spacing[i];
+  if (mArgsInfo.verbose_flag) std::cout << "origin after crop " << origin << std::endl;
+  output->SetOrigin(origin);
+
+  index.Fill(itk::NumericTraits<double>::Zero);
+  region.SetIndex(index);
+  output->SetRegions(region);
+
+  // Write/Save results
+  this->template SetNextOutput<ImageType>(output);
+}
+//--------------------------------------------------------------------
 
 #endif  //#define clitkCropImageGenericFilter_cxx
index a1d668e1ca92e646935ca81eddb63a78aa019227..fef610340a03dc7a5fe5985816bd48816f88ffd0 100644 (file)
@@ -60,6 +60,18 @@ namespace clitk
     void UpdateWithInputImageType();
 
   protected:
+    template <class ImageType>
+    class AutoCrop
+    {
+    public:
+      typedef typename ImageType::Pointer ImagePointer;
+      ImagePointer Do(args_info_type &, ImagePointer);
+    private:
+      template<unsigned int> struct PixelDimType {};
+      template<unsigned int Dim> ImagePointer Do(args_info_type &,ImagePointer, PixelDimType<Dim> *);
+      ImagePointer Do(args_info_type &, ImagePointer, PixelDimType<1> *);
+    };
+
     template<unsigned int Dim> void InitializeImageType();
     args_info_type mArgsInfo;