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") {
37 InitializeImageType<2>();
38 InitializeImageType<3>();
39 InitializeImageType<4>();
41 //--------------------------------------------------------------------
44 //--------------------------------------------------------------------
45 template<class args_info_type>
46 template<unsigned int Dim>
47 void BinarizeImageGenericFilter<args_info_type>::InitializeImageType() {
48 ADD_DEFAULT_IMAGE_TYPES(Dim);
50 //--------------------------------------------------------------------
53 //--------------------------------------------------------------------
54 template<class args_info_type>
55 void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
57 SetIOVerbose(mArgsInfo.verbose_flag);
58 if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
60 if (mArgsInfo.input_given) {
61 SetInputFilename(mArgsInfo.input_arg);
63 if (mArgsInfo.output_given) {
64 SetOutputFilename(mArgsInfo.output_arg);
67 //--------------------------------------------------------------------
70 //--------------------------------------------------------------------
71 // Update with the number of dimensions and the pixeltype
72 //--------------------------------------------------------------------
73 template<class args_info_type>
74 template<class InputImageType>
76 BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
80 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
83 typedef typename InputImageType::PixelType PixelType;
84 typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
87 typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
88 typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
89 thresholdFilter->SetInput(input);
90 thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
92 if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
93 if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
95 // DD(mArgsInfo.lower_given);
96 // DD(mArgsInfo.upper_given);
97 // DD(mArgsInfo.lower_arg);
98 // DD(mArgsInfo.upper_arg);
99 // DD(mArgsInfo.fg_arg);
100 // DD(mArgsInfo.bg_arg);
101 // DD(mArgsInfo.fg_given);
102 // DD(mArgsInfo.bg_given);
103 // DD(mArgsInfo.mode_arg);
105 // DD(mArgsInfo.useFG_flag);
106 // DD(mArgsInfo.useBG_flag);
108 // thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
110 // // Keep BG value to 0 if maskFilterType is used after
111 // if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
112 // thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
116 // thresholdFilter->SetOutsideValue(0);
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) {
126 - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
127 - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
128 - both -> use FG and BG (real binary image)
130 if (mArgsInfo.mode_arg == std::string("both")) {
131 thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
132 thresholdFilter->Update();
134 typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
135 this->template SetNextOutput<OutputImageType>(outputImage);
138 typename InputImageType::Pointer outputImage;
139 thresholdFilter->SetOutsideValue(0);
140 if (mArgsInfo.mode_arg == std::string("BG")) {
141 typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
142 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
143 maskFilter->SetInput1(input);
144 maskFilter->SetInput2(thresholdFilter->GetOutput());
145 maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
146 maskFilter->Update();
147 outputImage = maskFilter->GetOutput();
150 typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
151 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
152 maskFilter->SetInput1(input);
153 maskFilter->SetInput2(thresholdFilter->GetOutput());
154 maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
155 maskFilter->Update();
156 outputImage = maskFilter->GetOutput();
158 // Write/Save results
159 this->template SetNextOutput<InputImageType>(outputImage);
162 //--------------------------------------------------------------------
167 #endif //#define clitkBinarizeImageGenericFilter_txx