1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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
21 #include "clitkCropImageGenericFilter.h"
23 //-----------------------------------------------------------
25 //-----------------------------------------------------------
26 clitk::CropImageGenericFilter::CropImageGenericFilter():
27 ImageToImageGenericFilter<Self>("CropImage")
29 cmdline_parser_clitkCropImage_init(&mArgsInfo);
30 InitializeImageType<2>();
31 InitializeImageType<3>();
32 InitializeImageType<4>();
35 //--------------------------------------------------------------------
36 template<unsigned int Dim>
37 void clitk::CropImageGenericFilter::InitializeImageType()
39 ADD_DEFAULT_IMAGE_TYPES(Dim);
40 ADD_DEFAULT_VEC_IMAGE_TYPES;
42 //--------------------------------------------------------------------
44 //--------------------------------------------------------------------
45 void clitk::CropImageGenericFilter::SetArgsInfo(const args_info_type& 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);
53 //--------------------------------------------------------------------
55 //--------------------------------------------------------------------
56 template <class ImageType>
57 typename clitk::CropImageGenericFilter::AutoCrop<ImageType>::ImagePointer
58 clitk::CropImageGenericFilter::AutoCrop<ImageType>::Do(args_info_type &argsInfo, ImagePointer input)
60 static const unsigned int PixelDimension = itk::PixelTraits<typename ImageType::PixelType>::Dimension;
61 return this->Do(argsInfo, input, static_cast< PixelDimType<PixelDimension> *>(NULL) );
63 //--------------------------------------------------------------------
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> *)
71 clitkExceptionMacro("Autocrop is not implemented for vector fields");
74 //--------------------------------------------------------------------
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> *)
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);
92 return filter->GetOutput();
94 //--------------------------------------------------------------------
96 //--------------------------------------------------------------------
97 // Update with the number of dimensions and the pixeltype
98 //--------------------------------------------------------------------
99 template<class ImageType>
100 void clitk::CropImageGenericFilter::UpdateWithInputImageType()
103 typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
104 typename ImageType::RegionType input_region = input->GetLargestPossibleRegion();
107 if (mArgsInfo.BG_given && mArgsInfo.like_given)
108 clitkExceptionMacro("Do not use --BG and --like at the same time");
111 typename ImageType::Pointer output;
113 // ------------------------------------------------
114 if (mArgsInfo.BG_given) { // AutoCrop filter
115 AutoCrop<ImageType> autoCrop;
116 output = autoCrop.Do(mArgsInfo, input);
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);
133 output = filter->GetOutput();
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;
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]);
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]);
159 if (mArgsInfo.verbose_flag) {
160 std::cout << "lower " << lSize << " upper " << uSize << std::endl;
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);
169 output = filter->GetOutput();
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);
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);
192 index.Fill(itk::NumericTraits<double>::Zero);
193 region.SetIndex(index);
194 output->SetRegions(region);
196 // Write/Save results
197 this->template SetNextOutput<ImageType>(output);
199 //--------------------------------------------------------------------
201 #endif //#define clitkCropImageGenericFilter_cxx