1 #ifndef clitkBinarizeImageGenericFilter_txx
2 #define clitkBinarizeImageGenericFilter_txx
4 /* =================================================
5 * @file clitkBinarizeImageGenericFilter.txx
6 * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
11 ===================================================*/
14 #include "itkBinaryThresholdImageFilter.h"
15 #include "itkMaskImageFilter.h"
16 #include "itkMaskNegatedImageFilter.h"
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>();
29 //--------------------------------------------------------------------
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);
41 //--------------------------------------------------------------------
44 //--------------------------------------------------------------------
45 template<class args_info_type>
46 void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
48 SetIOVerbose(mArgsInfo.verbose_flag);
49 if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
51 if (mArgsInfo.input_given) {
52 SetInputFilename(mArgsInfo.input_arg);
54 if (mArgsInfo.output_given) {
55 SetOutputFilename(mArgsInfo.output_arg);
58 //--------------------------------------------------------------------
60 //--------------------------------------------------------------------
61 // Update with the number of dimensions and the pixeltype
62 //--------------------------------------------------------------------
63 template<class args_info_type>
64 template<class InputImageType>
66 BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
70 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
73 typedef typename InputImageType::PixelType PixelType;
74 typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
77 typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
78 typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
79 thresholdFilter->SetInput(input);
80 thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
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));
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);
95 // <<<<<<< clitkBinarizeImageGenericFilter.txx
96 // DD(mArgsInfo.useFG_flag);
97 // DD(mArgsInfo.useBG_flag);
99 // thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
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);
107 // thresholdFilter->SetOutsideValue(0);
110 // // thresholdFilter->Update();
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) {
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)
121 if (mArgsInfo.mode_arg == std::string("both")) {
122 thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
123 thresholdFilter->Update();
125 typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
126 this->template SetNextOutput<OutputImageType>(outputImage);
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();
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();
149 // Write/Save results
150 this->template SetNextOutput<InputImageType>(outputImage);
153 //--------------------------------------------------------------------
158 #endif //#define clitkBinarizeImageGenericFilter_txx