]> Creatis software - clitk.git/blob - tools/clitkCropImageGenericFilter.cxx
GateAsciiImageIO is now cross-platform using itksys::RegularExpression
[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 /* =================================================
22  * @file   clitkCropImageGenericFilter.cxx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30 #include "clitkCropImageGenericFilter.h"
31
32
33 namespace clitk
34 {
35
36
37   //-----------------------------------------------------------
38   // Constructor
39   //-----------------------------------------------------------
40   CropImageGenericFilter::CropImageGenericFilter():
41     ImageToImageGenericFilter<Self>("CropImage")
42   {
43     cmdline_parser_clitkCropImage_init(&mArgsInfo);
44     InitializeImageType<2>();
45     InitializeImageType<3>();
46     InitializeImageType<4>();
47   }
48
49   //--------------------------------------------------------------------
50   template<unsigned int Dim>
51   void clitk::CropImageGenericFilter::InitializeImageType()
52   {
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);
60   }
61   //--------------------------------------------------------------------
62
63   //--------------------------------------------------------------------
64   void clitk::CropImageGenericFilter::SetArgsInfo(const args_info_type& a) 
65   {
66     mArgsInfo=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);
71   }
72   //--------------------------------------------------------------------
73
74   //--------------------------------------------------------------------
75   // Update with the number of dimensions and the pixeltype
76   //--------------------------------------------------------------------
77   template<class ImageType>
78   void clitk::CropImageGenericFilter::UpdateWithInputImageType() 
79   { 
80     // Reading input
81     typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
82     typename ImageType::RegionType input_region = input->GetLargestPossibleRegion();
83
84     // Check options
85     if (mArgsInfo.BG_given && mArgsInfo.like_given)
86       clitkExceptionMacro("Do not use --BG and --like at the same time");    
87
88     // Prepare output
89     typename ImageType::Pointer output;
90     
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);
103       filter->Update();
104       output = filter->GetOutput();
105     }
106     else {
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);
120         filter->Update();
121         output = filter->GetOutput();
122       }
123       else {
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;
132           }
133         }
134         else {
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]);
138           }
139           else lSize.Fill(0);
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]);
143           }
144           else uSize.Fill(0);
145         }
146         
147         if (mArgsInfo.verbose_flag) {
148           std::cout << "lower " << lSize << " upper " << uSize << std::endl;
149         }
150         
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);
156         filter->Update();
157         output = filter->GetOutput();    
158       }
159     }
160
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);
166     }
167     
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);
178
179     index.Fill(itk::NumericTraits<double>::Zero);
180     region.SetIndex(index);
181     output->SetRegions(region);
182     
183     // Write/Save results
184     this->template SetNextOutput<ImageType>(output); 
185   }
186   //--------------------------------------------------------------------
187
188 } //end clitk
189
190 #endif  //#define clitkCropImageGenericFilter_cxx