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_IMAGE_TYPE(Dim, char);
48 ADD_IMAGE_TYPE(Dim, uchar);
49 ADD_IMAGE_TYPE(Dim, short);
50 ADD_IMAGE_TYPE(Dim, ushort);
51 ADD_IMAGE_TYPE(Dim, int);
52 // ADD_IMAGE_TYPE(Dim, uint16);
53 ADD_IMAGE_TYPE(Dim, float);
54 ADD_IMAGE_TYPE(Dim, double);
56 //--------------------------------------------------------------------
59 //--------------------------------------------------------------------
60 template<class args_info_type>
61 void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
63 SetIOVerbose(mArgsInfo.verbose_flag);
64 if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
66 if (mArgsInfo.input_given) {
67 SetInputFilename(mArgsInfo.input_arg);
69 if (mArgsInfo.output_given) {
70 SetOutputFilename(mArgsInfo.output_arg);
73 //--------------------------------------------------------------------
75 //--------------------------------------------------------------------
76 // Update with the number of dimensions and the pixeltype
77 //--------------------------------------------------------------------
78 template<class args_info_type>
79 template<class InputImageType>
81 BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
85 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
88 typedef typename InputImageType::PixelType PixelType;
89 typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
92 typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
93 typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
94 thresholdFilter->SetInput(input);
95 thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
97 if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
98 if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
100 // DD(mArgsInfo.lower_given);
101 // DD(mArgsInfo.upper_given);
102 // DD(mArgsInfo.lower_arg);
103 // DD(mArgsInfo.upper_arg);
104 // DD(mArgsInfo.fg_arg);
105 // DD(mArgsInfo.bg_arg);
106 // DD(mArgsInfo.fg_given);
107 // DD(mArgsInfo.bg_given);
108 // DD(mArgsInfo.mode_arg);
110 // <<<<<<< clitkBinarizeImageGenericFilter.txx
111 // DD(mArgsInfo.useFG_flag);
112 // DD(mArgsInfo.useBG_flag);
114 // thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
116 // // Keep BG value to 0 if maskFilterType is used after
117 // if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
118 // thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
122 // thresholdFilter->SetOutsideValue(0);
125 // // thresholdFilter->Update();
127 // // If no BG or no FG : new image, copy input with MaskImageFilter
128 // // If setFG -> FG BG have been changed
129 // if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
132 - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
133 - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
134 - both -> use FG and BG (real binary image)
136 if (mArgsInfo.mode_arg == std::string("both")) {
137 thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
138 thresholdFilter->Update();
140 typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
141 this->template SetNextOutput<OutputImageType>(outputImage);
144 typename InputImageType::Pointer outputImage;
145 thresholdFilter->SetOutsideValue(0);
146 if (mArgsInfo.mode_arg == std::string("BG")) {
147 typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
148 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
149 maskFilter->SetInput1(input);
150 maskFilter->SetInput2(thresholdFilter->GetOutput());
151 maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
152 maskFilter->Update();
153 outputImage = maskFilter->GetOutput();
156 typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
157 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
158 maskFilter->SetInput1(input);
159 maskFilter->SetInput2(thresholdFilter->GetOutput());
160 maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
161 maskFilter->Update();
162 outputImage = maskFilter->GetOutput();
164 // Write/Save results
165 this->template SetNextOutput<InputImageType>(outputImage);
168 //--------------------------------------------------------------------
173 #endif //#define clitkBinarizeImageGenericFilter_txx