]> Creatis software - clitk.git/blob - filters/clitkBinarizeImageGenericFilter.txx
- correct bug: string pixeltype is different in ITK and VTK
[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, uchar);
38     ADD_IMAGE_TYPE(Dim, short);
39     ADD_IMAGE_TYPE(Dim, int);
40     ADD_IMAGE_TYPE(Dim, float);
41   }
42   //--------------------------------------------------------------------
43   
44
45   //--------------------------------------------------------------------
46   template<class args_info_type>
47   void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
48     mArgsInfo=a;
49     SetIOVerbose(mArgsInfo.verbose_flag);
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     // Main filter
74     typedef typename InputImageType::PixelType PixelType;
75     typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
76
77     // Filter
78     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
79     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
80     thresholdFilter->SetInput(input);
81     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
82  
83     if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
84     if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
85
86     // DD(mArgsInfo.lower_given);
87     // DD(mArgsInfo.upper_given);
88     // DD(mArgsInfo.lower_arg);
89     // DD(mArgsInfo.upper_arg);
90     // DD(mArgsInfo.fg_arg);
91     // DD(mArgsInfo.bg_arg);
92     // DD(mArgsInfo.fg_given);
93     // DD(mArgsInfo.bg_given);
94     // DD(mArgsInfo.mode_arg);
95
96 // <<<<<<< clitkBinarizeImageGenericFilter.txx
97 //     DD(mArgsInfo.useFG_flag);
98 //     DD(mArgsInfo.useBG_flag);
99
100 //     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
101
102 //     // Keep BG value to 0 if maskFilterType is used after
103 //     if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
104 //       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
105 //     }
106 //     else {
107 //       DD("0");
108 //       thresholdFilter->SetOutsideValue(0);
109 //     }
110
111 //     // thresholdFilter->Update();
112
113 //     // If no BG or no FG : new image, copy input with MaskImageFilter
114 //     // If setFG -> FG BG have been changed
115 //     if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
116 // =======
117     /* Three modes : 
118        - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
119        - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
120        - both -> use FG and BG (real binary image)
121     */
122       if (mArgsInfo.mode_arg == std::string("both")) {
123       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
124       thresholdFilter->Update();
125       //>>>>>>> 1.5
126       typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
127       this->template SetNextOutput<OutputImageType>(outputImage);
128     }
129     else {
130       typename InputImageType::Pointer outputImage;
131       thresholdFilter->SetOutsideValue(0);
132       if (mArgsInfo.mode_arg == std::string("BG")) {
133         typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
134         typename maskFilterType::Pointer maskFilter = maskFilterType::New();
135         maskFilter->SetInput1(input);
136         maskFilter->SetInput2(thresholdFilter->GetOutput());
137         maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
138         maskFilter->Update();
139         outputImage = maskFilter->GetOutput();
140       }
141       else {
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