]> Creatis software - clitk.git/blob - filters/clitkBinarizeImageGenericFilter.txx
- binarize filter (allow no use of FG/BG)
[clitk.git] / filters / clitkBinarizeImageGenericFilter.txx
1 #ifndef clitkBinarizeImageGenericFilter_txx
2 #define clitkBinarizeImageGenericFilter_txx
3
4 /* =================================================
5  * @file   clitkBinarizeImageGenericFilter.txx
6  * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
7  * @date   29 june 2009
8  * 
9  * @brief 
10  * 
11  ===================================================*/
12
13 // itk include
14 #include "itkBinaryThresholdImageFilter.h"
15 #include "itkMaskImageFilter.h"
16 #include "itkMaskNegatedImageFilter.h"
17
18 namespace clitk
19 {
20   
21   //--------------------------------------------------------------------
22   template<class args_info_type>
23   BinarizeImageGenericFilter<args_info_type>::BinarizeImageGenericFilter():
24     ImageToImageGenericFilter<Self>("Binarize") {
25     //    InitializeImageType<2>();
26     InitializeImageType<3>();
27     //InitializeImageType<4>();
28   }
29   //--------------------------------------------------------------------
30
31
32   //--------------------------------------------------------------------
33   template<class args_info_type>
34   template<unsigned int Dim>
35   void BinarizeImageGenericFilter<args_info_type>::InitializeImageType() {      
36     //ADD_IMAGE_TYPE(Dim, char);
37     ADD_IMAGE_TYPE(Dim, short);
38     //ADD_IMAGE_TYPE(Dim, int);
39     //ADD_IMAGE_TYPE(Dim, float);
40   }
41   //--------------------------------------------------------------------
42   
43
44   //--------------------------------------------------------------------
45   template<class args_info_type>
46   void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
47     mArgsInfo=a;
48     SetIOVerbose(mArgsInfo.verbose_flag);
49
50     if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
51
52     if (mArgsInfo.input_given) {
53       SetInputFilename(mArgsInfo.input_arg);
54     }
55     if (mArgsInfo.output_given) {
56       SetOutputFilename(mArgsInfo.output_arg);
57     }
58   }
59   //--------------------------------------------------------------------
60
61   //--------------------------------------------------------------------
62   // Update with the number of dimensions and the pixeltype
63   //--------------------------------------------------------------------
64   template<class args_info_type>
65   template<class InputImageType>
66   void 
67   BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
68   {
69
70     // Reading input
71     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
72
73     // Check option
74     if (!mArgsInfo.useFG_flag && !mArgsInfo.useBG_flag) {
75       // Do nothing !!
76       std::cerr << "Warning : FG and BG are not used ! Do nothing." << std::endl;
77       this->template SetNextOutput<InputImageType>(input);
78       return;
79     }
80
81     // Main filter
82     typedef typename InputImageType::PixelType PixelType;
83     typedef itk::Image<int, InputImageType::ImageDimension> OutputImageType;
84
85     // Filter
86     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
87     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
88     thresholdFilter->SetInput(input);
89
90     /*    if (mArgsInfo.useFG_flag && !mArgsInfo.useBG_flag) {
91       DD("inverse");
92       // double fg = mArgsInfo.fg_arg;
93       //mArgsInfo.fg_arg = mArgsInfo.bg_arg;
94       // mArgsInfo.bg_arg = fg;
95        //   bool lo = mArgsInfo.lower_given;
96        //mArgsInfo.lower_given = mArgsInfo.upper_given;
97        //mArgsInfo.upper_given = lo;
98        }*/
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
108     DD(mArgsInfo.fg_arg);
109     DD(mArgsInfo.bg_arg);
110     DD(mArgsInfo.fg_given);
111     DD(mArgsInfo.bg_given);
112
113     DD(mArgsInfo.useFG_flag);
114     DD(mArgsInfo.useBG_flag);
115
116     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
117     thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
118
119     thresholdFilter->Update();
120
121     // If no BG or no FG : new image, copy input with MaskImageFilter
122     // If setFG -> FG BG have been changed
123     if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
124       typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
125       // Write/Save results
126       this->template SetNextOutput<OutputImageType>(outputImage);
127     }
128     else {
129       typename InputImageType::Pointer outputImage;
130       if (mArgsInfo.useBG_flag) {
131         DD("use BG");
132         typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
133         typename maskFilterType::Pointer maskFilter = maskFilterType::New();
134         maskFilter->SetInput1(input);
135         maskFilter->SetInput2(thresholdFilter->GetOutput());
136         maskFilter->Update();
137         maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
138         outputImage = maskFilter->GetOutput();
139       }
140       else {
141         DD("use FG");
142         typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
143         typename maskFilterType::Pointer maskFilter = maskFilterType::New();
144         maskFilter->SetInput1(input);
145         maskFilter->SetInput2(thresholdFilter->GetOutput());
146         maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
147         maskFilter->Update();
148         outputImage = maskFilter->GetOutput();
149       }
150       // Write/Save results
151       this->template SetNextOutput<InputImageType>(outputImage);
152     }
153   }
154   //--------------------------------------------------------------------
155
156
157 }//end clitk
158  
159 #endif //#define clitkBinarizeImageGenericFilter_txx