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 if (!mArgsInfo.useFG_flag && !mArgsInfo.useBG_flag) {
76 std::cerr << "Warning : FG and BG are not used ! Do nothing." << std::endl;
77 this->template SetNextOutput<InputImageType>(input);
82 typedef typename InputImageType::PixelType PixelType;
83 typedef itk::Image<int, InputImageType::ImageDimension> OutputImageType;
86 typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
87 typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
88 thresholdFilter->SetInput(input);
90 /* if (mArgsInfo.useFG_flag && !mArgsInfo.useBG_flag) {
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;
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));
103 DD(mArgsInfo.lower_given);
104 DD(mArgsInfo.upper_given);
105 DD(mArgsInfo.lower_arg);
106 DD(mArgsInfo.upper_arg);
108 DD(mArgsInfo.fg_arg);
109 DD(mArgsInfo.bg_arg);
110 DD(mArgsInfo.fg_given);
111 DD(mArgsInfo.bg_given);
113 DD(mArgsInfo.useFG_flag);
114 DD(mArgsInfo.useBG_flag);
116 thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
117 thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
119 thresholdFilter->Update();
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);
129 typename InputImageType::Pointer outputImage;
130 if (mArgsInfo.useBG_flag) {
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();
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