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"
26 #include <clitkCommon.h>
32 //--------------------------------------------------------------------
33 template<class args_info_type>
34 BinarizeImageGenericFilter<args_info_type>::BinarizeImageGenericFilter():
35 ImageToImageGenericFilter<Self>("Binarize") {
36 InitializeImageType<2>();
37 InitializeImageType<3>();
38 InitializeImageType<4>();
40 //--------------------------------------------------------------------
43 //--------------------------------------------------------------------
44 template<class args_info_type>
45 template<unsigned int Dim>
46 void BinarizeImageGenericFilter<args_info_type>::InitializeImageType() {
47 ADD_DEFAULT_IMAGE_TYPES(Dim);
49 //--------------------------------------------------------------------
52 //--------------------------------------------------------------------
53 template<class args_info_type>
54 void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
56 SetIOVerbose(mArgsInfo.verbose_flag);
57 if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
59 if (mArgsInfo.input_given) {
60 SetInputFilename(mArgsInfo.input_arg);
62 if (mArgsInfo.output_given) {
63 SetOutputFilename(mArgsInfo.output_arg);
66 //--------------------------------------------------------------------
68 //--------------------------------------------------------------------
69 // Update with the number of dimensions and the pixeltype
70 //--------------------------------------------------------------------
71 template<class args_info_type>
72 template<class InputImageType>
74 BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
78 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
81 typedef typename InputImageType::PixelType PixelType;
82 typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
85 typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
86 typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
87 thresholdFilter->SetInput(input);
88 thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
90 if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
91 if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
93 // DD(mArgsInfo.lower_given);
94 // DD(mArgsInfo.upper_given);
95 // DD(mArgsInfo.lower_arg);
96 // DD(mArgsInfo.upper_arg);
97 // DD(mArgsInfo.fg_arg);
98 // DD(mArgsInfo.bg_arg);
99 // DD(mArgsInfo.fg_given);
100 // DD(mArgsInfo.bg_given);
101 // DD(mArgsInfo.mode_arg);
103 // <<<<<<< clitkBinarizeImageGenericFilter.txx
104 // DD(mArgsInfo.useFG_flag);
105 // DD(mArgsInfo.useBG_flag);
107 // thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
109 // // Keep BG value to 0 if maskFilterType is used after
110 // if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
111 // thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
115 // thresholdFilter->SetOutsideValue(0);
118 // // thresholdFilter->Update();
120 // // If no BG or no FG : new image, copy input with MaskImageFilter
121 // // If setFG -> FG BG have been changed
122 // if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
125 - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
126 - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
127 - both -> use FG and BG (real binary image)
129 if (mArgsInfo.mode_arg == std::string("both")) {
130 thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
131 thresholdFilter->Update();
133 typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
134 this->template SetNextOutput<OutputImageType>(outputImage);
137 typename InputImageType::Pointer outputImage;
138 thresholdFilter->SetOutsideValue(0);
139 if (mArgsInfo.mode_arg == std::string("BG")) {
140 typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
141 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
142 maskFilter->SetInput1(input);
143 maskFilter->SetInput2(thresholdFilter->GetOutput());
144 maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
145 maskFilter->Update();
146 outputImage = maskFilter->GetOutput();
149 typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
150 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
151 maskFilter->SetInput1(input);
152 maskFilter->SetInput2(thresholdFilter->GetOutput());
153 maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
154 maskFilter->Update();
155 outputImage = maskFilter->GetOutput();
157 // Write/Save results
158 this->template SetNextOutput<InputImageType>(outputImage);
161 //--------------------------------------------------------------------
166 #endif //#define clitkBinarizeImageGenericFilter_txx