1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #ifndef clitkBinarizeImageGenericFilter_txx
19 #define clitkBinarizeImageGenericFilter_txx
21 /* =================================================
22 * @file clitkBinarizeImageGenericFilter.txx
23 * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
28 ===================================================*/
31 #include "itkBinaryThresholdImageFilter.h"
32 #include "itkMaskImageFilter.h"
33 #include "itkMaskNegatedImageFilter.h"
38 //--------------------------------------------------------------------
39 template<class args_info_type>
40 BinarizeImageGenericFilter<args_info_type>::BinarizeImageGenericFilter():
41 ImageToImageGenericFilter<Self>("Binarize") {
42 InitializeImageType<2>();
43 InitializeImageType<3>();
44 InitializeImageType<4>();
46 //--------------------------------------------------------------------
49 //--------------------------------------------------------------------
50 template<class args_info_type>
51 template<unsigned int Dim>
52 void BinarizeImageGenericFilter<args_info_type>::InitializeImageType() {
53 ADD_IMAGE_TYPE(Dim, char);
54 ADD_IMAGE_TYPE(Dim, uchar);
55 ADD_IMAGE_TYPE(Dim, short);
56 ADD_IMAGE_TYPE(Dim, int);
57 ADD_IMAGE_TYPE(Dim, float);
59 //--------------------------------------------------------------------
62 //--------------------------------------------------------------------
63 template<class args_info_type>
64 void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
66 SetIOVerbose(mArgsInfo.verbose_flag);
67 if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
69 if (mArgsInfo.input_given) {
70 SetInputFilename(mArgsInfo.input_arg);
72 if (mArgsInfo.output_given) {
73 SetOutputFilename(mArgsInfo.output_arg);
76 //--------------------------------------------------------------------
78 //--------------------------------------------------------------------
79 // Update with the number of dimensions and the pixeltype
80 //--------------------------------------------------------------------
81 template<class args_info_type>
82 template<class InputImageType>
84 BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
88 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
91 typedef typename InputImageType::PixelType PixelType;
92 typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
95 typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
96 typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
97 thresholdFilter->SetInput(input);
98 thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
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);
107 // DD(mArgsInfo.fg_arg);
108 // DD(mArgsInfo.bg_arg);
109 // DD(mArgsInfo.fg_given);
110 // DD(mArgsInfo.bg_given);
111 // DD(mArgsInfo.mode_arg);
113 // <<<<<<< clitkBinarizeImageGenericFilter.txx
114 // DD(mArgsInfo.useFG_flag);
115 // DD(mArgsInfo.useBG_flag);
117 // thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
119 // // Keep BG value to 0 if maskFilterType is used after
120 // if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
121 // thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
125 // thresholdFilter->SetOutsideValue(0);
128 // // thresholdFilter->Update();
130 // // If no BG or no FG : new image, copy input with MaskImageFilter
131 // // If setFG -> FG BG have been changed
132 // if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
135 - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
136 - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
137 - both -> use FG and BG (real binary image)
139 if (mArgsInfo.mode_arg == std::string("both")) {
140 thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
141 thresholdFilter->Update();
143 typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
144 this->template SetNextOutput<OutputImageType>(outputImage);
147 typename InputImageType::Pointer outputImage;
148 thresholdFilter->SetOutsideValue(0);
149 if (mArgsInfo.mode_arg == std::string("BG")) {
150 typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
151 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
152 maskFilter->SetInput1(input);
153 maskFilter->SetInput2(thresholdFilter->GetOutput());
154 maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
155 maskFilter->Update();
156 outputImage = maskFilter->GetOutput();
159 typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
160 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
161 maskFilter->SetInput1(input);
162 maskFilter->SetInput2(thresholdFilter->GetOutput());
163 maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
164 maskFilter->Update();
165 outputImage = maskFilter->GetOutput();
167 // Write/Save results
168 this->template SetNextOutput<InputImageType>(outputImage);
171 //--------------------------------------------------------------------
176 #endif //#define clitkBinarizeImageGenericFilter_txx