]> Creatis software - clitk.git/blob - filters/clitkBinarizeImageGenericFilter.txx
added the new headers
[clitk.git] / filters / clitkBinarizeImageGenericFilter.txx
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://oncora1.lyon.fnclcc.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 clitkBinarizeImageGenericFilter_txx
19 #define clitkBinarizeImageGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkBinarizeImageGenericFilter.txx
23  * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
24  * @date   29 june 2009
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30 // itk include
31 #include "itkBinaryThresholdImageFilter.h"
32 #include "itkMaskImageFilter.h"
33 #include "itkMaskNegatedImageFilter.h"
34
35 namespace clitk
36 {
37   
38   //--------------------------------------------------------------------
39   template<class args_info_type>
40   BinarizeImageGenericFilter<args_info_type>::BinarizeImageGenericFilter():
41     ImageToImageGenericFilter<Self>("Binarize") {
42     InitializeImageType<2>();
43     InitializeImageType<3>();
44     InitializeImageType<4>();
45   }
46   //--------------------------------------------------------------------
47
48
49   //--------------------------------------------------------------------
50   template<class args_info_type>
51   template<unsigned int Dim>
52   void BinarizeImageGenericFilter<args_info_type>::InitializeImageType() {      
53     ADD_IMAGE_TYPE(Dim, char);
54     ADD_IMAGE_TYPE(Dim, uchar);
55     ADD_IMAGE_TYPE(Dim, short);
56     ADD_IMAGE_TYPE(Dim, int);
57     ADD_IMAGE_TYPE(Dim, float);
58   }
59   //--------------------------------------------------------------------
60   
61
62   //--------------------------------------------------------------------
63   template<class args_info_type>
64   void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
65     mArgsInfo=a;
66     SetIOVerbose(mArgsInfo.verbose_flag);
67     if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
68
69     if (mArgsInfo.input_given) {
70       SetInputFilename(mArgsInfo.input_arg);
71     }
72     if (mArgsInfo.output_given) {
73       SetOutputFilename(mArgsInfo.output_arg);
74     }
75   }
76   //--------------------------------------------------------------------
77
78   //--------------------------------------------------------------------
79   // Update with the number of dimensions and the pixeltype
80   //--------------------------------------------------------------------
81   template<class args_info_type>
82   template<class InputImageType>
83   void 
84   BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
85   {
86
87     // Reading input
88     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
89
90     // Main filter
91     typedef typename InputImageType::PixelType PixelType;
92     typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
93
94     // Filter
95     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
96     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
97     thresholdFilter->SetInput(input);
98     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
99  
100     if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
101     if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
102
103     // DD(mArgsInfo.lower_given);
104     // DD(mArgsInfo.upper_given);
105     // DD(mArgsInfo.lower_arg);
106     // DD(mArgsInfo.upper_arg);
107     // DD(mArgsInfo.fg_arg);
108     // DD(mArgsInfo.bg_arg);
109     // DD(mArgsInfo.fg_given);
110     // DD(mArgsInfo.bg_given);
111     // DD(mArgsInfo.mode_arg);
112
113 // <<<<<<< clitkBinarizeImageGenericFilter.txx
114 //     DD(mArgsInfo.useFG_flag);
115 //     DD(mArgsInfo.useBG_flag);
116
117 //     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
118
119 //     // Keep BG value to 0 if maskFilterType is used after
120 //     if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
121 //       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
122 //     }
123 //     else {
124 //       DD("0");
125 //       thresholdFilter->SetOutsideValue(0);
126 //     }
127
128 //     // thresholdFilter->Update();
129
130 //     // If no BG or no FG : new image, copy input with MaskImageFilter
131 //     // If setFG -> FG BG have been changed
132 //     if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
133 // =======
134     /* Three modes : 
135        - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
136        - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
137        - both -> use FG and BG (real binary image)
138     */
139       if (mArgsInfo.mode_arg == std::string("both")) {
140       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
141       thresholdFilter->Update();
142       //>>>>>>> 1.5
143       typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
144       this->template SetNextOutput<OutputImageType>(outputImage);
145     }
146     else {
147       typename InputImageType::Pointer outputImage;
148       thresholdFilter->SetOutsideValue(0);
149       if (mArgsInfo.mode_arg == std::string("BG")) {
150         typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
151         typename maskFilterType::Pointer maskFilter = maskFilterType::New();
152         maskFilter->SetInput1(input);
153         maskFilter->SetInput2(thresholdFilter->GetOutput());
154         maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
155         maskFilter->Update();
156         outputImage = maskFilter->GetOutput();
157       }
158       else {
159         typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
160         typename maskFilterType::Pointer maskFilter = maskFilterType::New();
161         maskFilter->SetInput1(input);
162         maskFilter->SetInput2(thresholdFilter->GetOutput());
163         maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
164         maskFilter->Update();
165         outputImage = maskFilter->GetOutput();
166       }
167       // Write/Save results
168       this->template SetNextOutput<InputImageType>(outputImage);
169     }
170   }
171   //--------------------------------------------------------------------
172
173
174 }//end clitk
175  
176 #endif //#define clitkBinarizeImageGenericFilter_txx