]> Creatis software - clitk.git/blob - tools/clitkCropImageGenericFilter.cxx
4adcb9bfadbae4ea0f1b5bc7fa6b9a385990f42e
[clitk.git] / tools / clitkCropImageGenericFilter.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef clitkCropImageGenericFilter_cxx
19 #define clitkCropImageGenericFilter_cxx
20
21 #include "clitkCropImageGenericFilter.h"
22
23 //-----------------------------------------------------------
24 // Constructor
25 //-----------------------------------------------------------
26 clitk::CropImageGenericFilter::CropImageGenericFilter():
27   ImageToImageGenericFilter<Self>("CropImage")
28 {
29   cmdline_parser_clitkCropImage_init(&mArgsInfo);
30   InitializeImageType<2>();
31   InitializeImageType<3>();
32   InitializeImageType<4>();
33 }
34
35 //--------------------------------------------------------------------
36 template<unsigned int Dim>
37 void clitk::CropImageGenericFilter::InitializeImageType()
38 {
39   ADD_DEFAULT_IMAGE_TYPES(Dim);
40   ADD_DEFAULT_VEC_IMAGE_TYPES;
41 }
42 //--------------------------------------------------------------------
43
44 //--------------------------------------------------------------------
45 void clitk::CropImageGenericFilter::SetArgsInfo(const args_info_type& a)
46 {
47   mArgsInfo=a;
48   SetIOVerbose(mArgsInfo.verbose_flag);
49   if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
50   if (mArgsInfo.input_given)   AddInputFilename(mArgsInfo.input_arg);
51   if (mArgsInfo.output_given)  AddOutputFilename(mArgsInfo.output_arg);
52 }
53 //--------------------------------------------------------------------
54
55 //--------------------------------------------------------------------
56 template <class ImageType>
57 typename clitk::CropImageGenericFilter::AutoCrop<ImageType>::ImagePointer
58 clitk::CropImageGenericFilter::AutoCrop<ImageType>::Do(args_info_type &argsInfo, ImagePointer input)
59 {
60   static const unsigned int PixelDimension = itk::PixelTraits<typename ImageType::PixelType>::Dimension;
61   return this->Do(argsInfo, input, static_cast< PixelDimType<PixelDimension> *>(NULL) );
62 }
63 //--------------------------------------------------------------------
64
65 //--------------------------------------------------------------------
66 template <class ImageType>
67 template<unsigned int Dim>
68 typename clitk::CropImageGenericFilter::AutoCrop<ImageType>::ImagePointer
69 clitk::CropImageGenericFilter::AutoCrop<ImageType>::Do(args_info_type &, ImagePointer, PixelDimType<Dim> *)
70 {
71   clitkExceptionMacro("Autocrop is not implemented for vector fields");
72   return ITK_NULLPTR;
73 }
74 //--------------------------------------------------------------------
75
76 //--------------------------------------------------------------------
77 template <class ImageType>
78 typename clitk::CropImageGenericFilter::AutoCrop<ImageType>::ImagePointer
79 clitk::CropImageGenericFilter::AutoCrop<ImageType>::Do(args_info_type &argsInfo, ImagePointer input, PixelDimType<1> *)
80 {
81   if (argsInfo.boundingBox_given)
82     clitkExceptionMacro("Do not use --BG and --boundingBox at the same time");
83   if (argsInfo.lower_given)
84     clitkExceptionMacro("Do not use --BG and --lower at the same time");
85   if (argsInfo.upper_given)
86     clitkExceptionMacro("Do not use --BG and --upper at the same time");
87   typedef clitk::AutoCropFilter<ImageType> FilterType;
88   typename FilterType::Pointer filter = FilterType::New();
89   filter->SetInput(input);
90   filter->SetBackgroundValue(argsInfo.BG_arg);
91   filter->Update();
92   return filter->GetOutput();
93 }
94 //--------------------------------------------------------------------
95
96 //--------------------------------------------------------------------
97 // Update with the number of dimensions and the pixeltype
98 //--------------------------------------------------------------------
99 template<class ImageType>
100 void clitk::CropImageGenericFilter::UpdateWithInputImageType()
101 {
102   // Reading input
103   typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
104   typename ImageType::RegionType input_region = input->GetLargestPossibleRegion();
105
106   // Check options
107   if (mArgsInfo.BG_given && mArgsInfo.like_given)
108     clitkExceptionMacro("Do not use --BG and --like at the same time");
109
110   // Prepare output
111   typename ImageType::Pointer output;
112
113   // ------------------------------------------------
114   if (mArgsInfo.BG_given) { // AutoCrop filter
115     AutoCrop<ImageType> autoCrop;
116     output = autoCrop.Do(mArgsInfo, input);
117   }
118   else {
119     // ------------------------------------------------
120     if (mArgsInfo.like_given) { // CropLike filter
121     if (mArgsInfo.boundingBox_given)
122       clitkExceptionMacro("Do not use --like and --boundingBox at the same time");
123     if (mArgsInfo.lower_given)
124       clitkExceptionMacro("Do not use --like and --lower at the same time");
125     if (mArgsInfo.upper_given)
126       clitkExceptionMacro("Do not use --like and --upper at the same time");
127       typedef clitk::CropLikeImageFilter<ImageType> FilterType;
128       typename FilterType::Pointer filter = FilterType::New();
129       filter->SetInput(input);
130       filter->SetCropLikeFilename(mArgsInfo.like_arg);
131       filter->SetBackgroundValue(mArgsInfo.BGLike_arg);
132       filter->Update();
133       output = filter->GetOutput();
134     }
135     else {
136       // ------------------------------------------------
137       typename ImageType::SizeType lSize;
138       typename ImageType::SizeType uSize;
139       if (mArgsInfo.verbose_flag) std::cout << "input region " << input_region << std::endl;
140       if (mArgsInfo.boundingBox_given) {
141         for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
142           lSize[i] = mArgsInfo.boundingBox_arg[2*i];
143           uSize[i] = input_region.GetSize()[i]-mArgsInfo.boundingBox_arg[2*i+1]-1;
144         }
145       }
146       else {
147         if (mArgsInfo.lower_given) {
148           for(unsigned int i=0; i<ImageType::ImageDimension; i++)
149             lSize[i]=static_cast<unsigned int >(mArgsInfo.lower_arg[i]);
150         }
151         else lSize.Fill(0);
152         if (mArgsInfo.upper_given) {
153           for(unsigned int i=0; i<ImageType::ImageDimension; i++)
154             uSize[i]=static_cast<unsigned int >(mArgsInfo.upper_arg[i]);
155         }
156         else uSize.Fill(0);
157       }
158
159       if (mArgsInfo.verbose_flag) {
160         std::cout << "lower " << lSize << " upper " << uSize << std::endl;
161       }
162
163       typedef  itk::CropImageFilter<ImageType, ImageType> CropImageFilterType;
164       typename CropImageFilterType::Pointer filter=CropImageFilterType::New();
165       filter->SetInput(input);
166       filter->SetLowerBoundaryCropSize(lSize);
167       filter->SetUpperBoundaryCropSize(uSize);
168       filter->Update();
169       output = filter->GetOutput();
170     }
171   }
172
173   // Force origin if needed
174   if (mArgsInfo.origin_flag) {
175     typename ImageType::PointType origin;
176     origin.Fill(itk::NumericTraits<double>::Zero);
177     output->SetOrigin(origin);
178   }
179
180   // adjust image origin and force index to zero
181   typename ImageType::RegionType region = output->GetLargestPossibleRegion();
182   typename ImageType::IndexType index = region.GetIndex();
183   typename ImageType::PointType origin = output->GetOrigin();
184   typename ImageType::SpacingType spacing = output->GetSpacing();
185   if (!mArgsInfo.BG_given) {
186     if (mArgsInfo.verbose_flag) std::cout << "origin before crop " << origin << std::endl;
187     input->TransformIndexToPhysicalPoint(index,origin);
188     if (mArgsInfo.verbose_flag) std::cout << "origin after crop " << origin << std::endl;
189     output->SetOrigin(origin);
190   }
191
192   index.Fill(itk::NumericTraits<double>::Zero);
193   region.SetIndex(index);
194   output->SetRegions(region);
195
196   // Write/Save results
197   this->template SetNextOutput<ImageType>(output);
198 }
199 //--------------------------------------------------------------------
200
201 #endif  //#define clitkCropImageGenericFilter_cxx