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 ======================================================================-====*/
19 #ifndef CLITKBINARIZEIMAGEGENERICFILTER_TXX
20 #define CLITKBINARIZEIMAGEGENERICFILTER_TXX
23 #include "itkBinaryThresholdImageFilter.h"
24 #include "itkMaskImageFilter.h"
25 #include "itkMaskNegatedImageFilter.h"
27 #include <clitkCommon.h>
33 //--------------------------------------------------------------------
34 template<class args_info_type>
35 BinarizeImageGenericFilter<args_info_type>::BinarizeImageGenericFilter():
36 ImageToImageGenericFilter<Self>("Binarize")
38 InitializeImageType<2>();
39 InitializeImageType<3>();
40 InitializeImageType<4>();
42 //--------------------------------------------------------------------
45 //--------------------------------------------------------------------
46 template<class args_info_type>
47 template<unsigned int Dim>
48 void BinarizeImageGenericFilter<args_info_type>::InitializeImageType()
50 ADD_DEFAULT_IMAGE_TYPES(Dim);
52 //--------------------------------------------------------------------
55 //--------------------------------------------------------------------
56 template<class args_info_type>
57 void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a)
60 SetIOVerbose(mArgsInfo.verbose_flag);
61 if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
63 if (mArgsInfo.input_given) {
64 SetInputFilename(mArgsInfo.input_arg);
66 if (mArgsInfo.output_given) {
67 SetOutputFilename(mArgsInfo.output_arg);
70 //--------------------------------------------------------------------
73 //--------------------------------------------------------------------
74 // Update with the number of dimensions and the pixeltype
75 //--------------------------------------------------------------------
76 template<class args_info_type>
77 template<class InputImageType>
79 BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
83 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
86 typedef typename InputImageType::PixelType PixelType;
87 typedef itk::Image<uchar, InputImageType::ImageDimension> OutputImageType;
90 typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
91 typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
92 thresholdFilter->SetInput(input);
93 thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
95 if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
96 if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
98 // DD(mArgsInfo.lower_given);
99 // DD(mArgsInfo.upper_given);
100 // DD(mArgsInfo.lower_arg);
101 // DD(mArgsInfo.upper_arg);
102 // DD(mArgsInfo.fg_arg);
103 // DD(mArgsInfo.bg_arg);
104 // DD(mArgsInfo.fg_given);
105 // DD(mArgsInfo.bg_given);
106 // DD(mArgsInfo.mode_arg);
108 // DD(mArgsInfo.useFG_flag);
109 // DD(mArgsInfo.useBG_flag);
111 // thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
113 // // Keep BG value to 0 if maskFilterType is used after
114 // if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
115 // thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
119 // thresholdFilter->SetOutsideValue(0);
122 // // thresholdFilter->Update();
124 // // If no BG or no FG : new image, copy input with MaskImageFilter
125 // // If setFG -> FG BG have been changed
126 // if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
129 - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
130 - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
131 - both -> use FG and BG (real binary image)
133 if (mArgsInfo.mode_arg == std::string("both")) {
134 thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
135 thresholdFilter->Update();
137 typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
138 this->template SetNextOutput<OutputImageType>(outputImage);
140 typename InputImageType::Pointer outputImage;
141 thresholdFilter->SetOutsideValue(0);
142 if (mArgsInfo.mode_arg == std::string("BG")) {
143 typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
144 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
145 maskFilter->SetInput1(input);
146 maskFilter->SetInput2(thresholdFilter->GetOutput());
147 maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
148 maskFilter->Update();
149 outputImage = maskFilter->GetOutput();
151 typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
152 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
153 maskFilter->SetInput1(input);
154 maskFilter->SetInput2(thresholdFilter->GetOutput());
155 maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
156 maskFilter->Update();
157 outputImage = maskFilter->GetOutput();
159 // Write/Save results
160 this->template SetNextOutput<InputImageType>(outputImage);
163 //--------------------------------------------------------------------
168 #endif //#define clitkBinarizeImageGenericFilter_txx