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);
50 if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
52 if (mArgsInfo.input_given) {
53 SetInputFilename(mArgsInfo.input_arg);
55 if (mArgsInfo.output_given) {
56 SetOutputFilename(mArgsInfo.output_arg);
59 //--------------------------------------------------------------------
61 //--------------------------------------------------------------------
62 // Update with the number of dimensions and the pixeltype
63 //--------------------------------------------------------------------
64 template<class args_info_type>
65 template<class InputImageType>
67 BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
71 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
74 typedef typename InputImageType::PixelType PixelType;
75 typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
78 typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
79 typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
80 thresholdFilter->SetInput(input);
81 thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
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));
86 DD(mArgsInfo.lower_given);
87 DD(mArgsInfo.upper_given);
88 DD(mArgsInfo.lower_arg);
89 DD(mArgsInfo.upper_arg);
92 DD(mArgsInfo.fg_given);
93 DD(mArgsInfo.bg_given);
94 DD(mArgsInfo.mode_arg);
96 // <<<<<<< clitkBinarizeImageGenericFilter.txx
97 // DD(mArgsInfo.useFG_flag);
98 // DD(mArgsInfo.useBG_flag);
100 // thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
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);
108 // thresholdFilter->SetOutsideValue(0);
111 // // thresholdFilter->Update();
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) {
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)
122 if (mArgsInfo.mode_arg == std::string("both")) {
123 thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
124 thresholdFilter->Update();
126 typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
127 this->template SetNextOutput<OutputImageType>(outputImage);
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();
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();
150 // Write/Save results
151 this->template SetNextOutput<InputImageType>(outputImage);
154 //--------------------------------------------------------------------
159 #endif //#define clitkBinarizeImageGenericFilter_txx