]> Creatis software - clitk.git/blob - filters/clitkBinarizeImageGenericFilter.txx
- autotool
[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     if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
50
51     if (mArgsInfo.input_given) {
52       SetInputFilename(mArgsInfo.input_arg);
53     }
54     if (mArgsInfo.output_given) {
55       SetOutputFilename(mArgsInfo.output_arg);
56     }
57   }
58   //--------------------------------------------------------------------
59
60   //--------------------------------------------------------------------
61   // Update with the number of dimensions and the pixeltype
62   //--------------------------------------------------------------------
63   template<class args_info_type>
64   template<class InputImageType>
65   void 
66   BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
67   {
68
69     // Reading input
70     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
71
72     // Main filter
73     typedef typename InputImageType::PixelType PixelType;
74     typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
75
76     // Filter
77     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
78     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
79     thresholdFilter->SetInput(input);
80     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
81  
82     if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
83     if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
84
85     // DD(mArgsInfo.lower_given);
86     // DD(mArgsInfo.upper_given);
87     // DD(mArgsInfo.lower_arg);
88     // DD(mArgsInfo.upper_arg);
89     // DD(mArgsInfo.fg_arg);
90     // DD(mArgsInfo.bg_arg);
91     // DD(mArgsInfo.fg_given);
92     // DD(mArgsInfo.bg_given);
93     // DD(mArgsInfo.mode_arg);
94
95 // <<<<<<< clitkBinarizeImageGenericFilter.txx
96 //     DD(mArgsInfo.useFG_flag);
97 //     DD(mArgsInfo.useBG_flag);
98
99 //     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
100
101 //     // Keep BG value to 0 if maskFilterType is used after
102 //     if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
103 //       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
104 //     }
105 //     else {
106 //       DD("0");
107 //       thresholdFilter->SetOutsideValue(0);
108 //     }
109
110 //     // thresholdFilter->Update();
111
112 //     // If no BG or no FG : new image, copy input with MaskImageFilter
113 //     // If setFG -> FG BG have been changed
114 //     if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
115 // =======
116     /* Three modes : 
117        - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
118        - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
119        - both -> use FG and BG (real binary image)
120     */
121       if (mArgsInfo.mode_arg == std::string("both")) {
122       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
123       thresholdFilter->Update();
124       //>>>>>>> 1.5
125       typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
126       this->template SetNextOutput<OutputImageType>(outputImage);
127     }
128     else {
129       typename InputImageType::Pointer outputImage;
130       thresholdFilter->SetOutsideValue(0);
131       if (mArgsInfo.mode_arg == std::string("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->SetOutsideValue(mArgsInfo.bg_arg);
137         maskFilter->Update();
138         outputImage = maskFilter->GetOutput();
139       }
140       else {
141         typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
142         typename maskFilterType::Pointer maskFilter = maskFilterType::New();
143         maskFilter->SetInput1(input);
144         maskFilter->SetInput2(thresholdFilter->GetOutput());
145         maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
146         maskFilter->Update();
147         outputImage = maskFilter->GetOutput();
148       }
149       // Write/Save results
150       this->template SetNextOutput<InputImageType>(outputImage);
151     }
152   }
153   //--------------------------------------------------------------------
154
155
156 }//end clitk
157  
158 #endif //#define clitkBinarizeImageGenericFilter_txx