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 /* =================================================
22 * @file clitkCropImageGenericFilter.cxx
28 ===================================================*/
30 #include "clitkCropImageGenericFilter.h"
37 //-----------------------------------------------------------
39 //-----------------------------------------------------------
40 CropImageGenericFilter::CropImageGenericFilter():
41 ImageToImageGenericFilter<Self>("CropImage")
43 cmdline_parser_clitkCropImage_init(&mArgsInfo);
44 InitializeImageType<2>();
45 InitializeImageType<3>();
46 InitializeImageType<4>();
49 //--------------------------------------------------------------------
50 template<unsigned int Dim>
51 void clitk::CropImageGenericFilter::InitializeImageType()
53 ADD_DEFAULT_IMAGE_TYPES(Dim);
54 //ADD_IMAGE_TYPE(Dim, uchar);
55 //ADD_IMAGE_TYPE(Dim, short);
56 // ADD_IMAGE_TYPE(Dim, uint);
57 // ADD_IMAGE_TYPE(Dim, ulong);
58 // ADD_IMAGE_TYPE(Dim, int);
59 // ADD_IMAGE_TYPE(Dim, float);
61 //--------------------------------------------------------------------
63 //--------------------------------------------------------------------
64 void clitk::CropImageGenericFilter::SetArgsInfo(const args_info_type& a)
67 SetIOVerbose(mArgsInfo.verbose_flag);
68 if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
69 if (mArgsInfo.input_given) AddInputFilename(mArgsInfo.input_arg);
70 if (mArgsInfo.output_given) AddOutputFilename(mArgsInfo.output_arg);
72 //--------------------------------------------------------------------
74 //--------------------------------------------------------------------
75 // Update with the number of dimensions and the pixeltype
76 //--------------------------------------------------------------------
77 template<class ImageType>
78 void clitk::CropImageGenericFilter::UpdateWithInputImageType()
81 typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
82 typename ImageType::RegionType input_region = input->GetLargestPossibleRegion();
85 if (mArgsInfo.BG_given && mArgsInfo.like_given)
86 clitkExceptionMacro("Do not use --BG and --like at the same time");
89 typename ImageType::Pointer output;
91 // ------------------------------------------------
92 if (mArgsInfo.BG_given) { // AutoCrop filter
93 if (mArgsInfo.boundingBox_given)
94 clitkExceptionMacro("Do not use --BG and --boundingBox at the same time");
95 if (mArgsInfo.lower_given)
96 clitkExceptionMacro("Do not use --BG and --lower at the same time");
97 if (mArgsInfo.upper_given)
98 clitkExceptionMacro("Do not use --BG and --upper at the same time");
99 typedef clitk::AutoCropFilter<ImageType> FilterType;
100 typename FilterType::Pointer filter = FilterType::New();
101 filter->SetInput(input);
102 filter->SetBackgroundValue(mArgsInfo.BG_arg);
104 output = filter->GetOutput();
107 // ------------------------------------------------
108 if (mArgsInfo.like_given) { // CropLike filter
109 if (mArgsInfo.boundingBox_given)
110 clitkExceptionMacro("Do not use --like and --boundingBox at the same time");
111 if (mArgsInfo.lower_given)
112 clitkExceptionMacro("Do not use --like and --lower at the same time");
113 if (mArgsInfo.upper_given)
114 clitkExceptionMacro("Do not use --like and --upper at the same time");
115 typedef clitk::CropLikeImageFilter<ImageType> FilterType;
116 typename FilterType::Pointer filter = FilterType::New();
117 filter->SetInput(input);
118 filter->SetCropLikeFilename(mArgsInfo.like_arg);
119 filter->SetBackgroundValue(mArgsInfo.BGLike_arg);
121 output = filter->GetOutput();
124 // ------------------------------------------------
125 typename ImageType::SizeType lSize;
126 typename ImageType::SizeType uSize;
127 if (mArgsInfo.verbose_flag) std::cout << "input region " << input_region << std::endl;
128 if (mArgsInfo.boundingBox_given) {
129 for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
130 lSize[i] = mArgsInfo.boundingBox_arg[2*i];
131 uSize[i] = input_region.GetSize()[i]-mArgsInfo.boundingBox_arg[2*i+1]-1;
135 if (mArgsInfo.lower_given) {
136 for(unsigned int i=0; i<ImageType::ImageDimension; i++)
137 lSize[i]=static_cast<unsigned int >(mArgsInfo.lower_arg[i]);
140 if (mArgsInfo.upper_given) {
141 for(unsigned int i=0; i<ImageType::ImageDimension; i++)
142 uSize[i]=static_cast<unsigned int >(mArgsInfo.upper_arg[i]);
147 if (mArgsInfo.verbose_flag) {
148 std::cout << "lower " << lSize << " upper " << uSize << std::endl;
151 typedef itk::CropImageFilter<ImageType, ImageType> CropImageFilterType;
152 typename CropImageFilterType::Pointer filter=CropImageFilterType::New();
153 filter->SetInput(input);
154 filter->SetLowerBoundaryCropSize(lSize);
155 filter->SetUpperBoundaryCropSize(uSize);
157 output = filter->GetOutput();
161 // Force origin if needed
162 if (mArgsInfo.origin_flag) {
163 typename ImageType::PointType origin;
164 origin.Fill(itk::NumericTraits<double>::Zero);
165 output->SetOrigin(origin);
168 // adjust image origin and force index to zero
169 typename ImageType::RegionType region = output->GetLargestPossibleRegion();
170 typename ImageType::IndexType index = region.GetIndex();
171 typename ImageType::PointType origin = output->GetOrigin();
172 typename ImageType::SpacingType spacing = output->GetSpacing();
173 if (mArgsInfo.verbose_flag) std::cout << "origin before crop " << origin << std::endl;
174 for (unsigned int i = 0; i < output->GetImageDimension(); i++)
175 origin[i] += index[i]*spacing[i];
176 if (mArgsInfo.verbose_flag) std::cout << "origin after crop " << origin << std::endl;
177 output->SetOrigin(origin);
179 index.Fill(itk::NumericTraits<double>::Zero);
180 region.SetIndex(index);
181 output->SetRegions(region);
183 // Write/Save results
184 this->template SetNextOutput<ImageType>(output);
186 //--------------------------------------------------------------------
190 #endif //#define clitkCropImageGenericFilter_cxx