]> Creatis software - clitk.git/blob - filters/clitkBinarizeImageGenericFilter.txx
a35a7304c802c4e5e63b2e6418af4a74afdbcf5f
[clitk.git] / filters / clitkBinarizeImageGenericFilter.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18
19 #ifndef CLITKBINARIZEIMAGEGENERICFILTER_TXX
20 #define CLITKBINARIZEIMAGEGENERICFILTER_TXX
21
22 // itk include
23 #include "itkBinaryThresholdImageFilter.h"
24 #include "itkMaskImageFilter.h"
25 #include "itkMaskNegatedImageFilter.h"
26 #include <clitkCommon.h>
27 // #include <tiff.h>
28
29 namespace clitk
30 {
31   
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>();
39   }
40   //--------------------------------------------------------------------
41
42
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);
48   }
49   //--------------------------------------------------------------------
50   
51
52   //--------------------------------------------------------------------
53   template<class args_info_type>
54   void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
55     mArgsInfo=a;
56     SetIOVerbose(mArgsInfo.verbose_flag);
57     if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
58
59     if (mArgsInfo.input_given) {
60       SetInputFilename(mArgsInfo.input_arg);
61     }
62     if (mArgsInfo.output_given) {
63       SetOutputFilename(mArgsInfo.output_arg);
64     }
65   }
66   //--------------------------------------------------------------------
67
68   //--------------------------------------------------------------------
69   // Update with the number of dimensions and the pixeltype
70   //--------------------------------------------------------------------
71   template<class args_info_type>
72   template<class InputImageType>
73   void 
74   BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
75   {
76
77     // Reading input
78     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
79
80     // Main filter
81     typedef typename InputImageType::PixelType PixelType;
82     typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
83
84     // Filter
85     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
86     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
87     thresholdFilter->SetInput(input);
88     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
89  
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));
92
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);
102
103 // <<<<<<< clitkBinarizeImageGenericFilter.txx
104 //     DD(mArgsInfo.useFG_flag);
105 //     DD(mArgsInfo.useBG_flag);
106
107 //     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
108
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);
112 //     }
113 //     else {
114 //       DD("0");
115 //       thresholdFilter->SetOutsideValue(0);
116 //     }
117
118 //     // thresholdFilter->Update();
119
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) {
123 // =======
124     /* Three modes : 
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)
128     */
129       if (mArgsInfo.mode_arg == std::string("both")) {
130       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
131       thresholdFilter->Update();
132       //>>>>>>> 1.5
133       typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
134       this->template SetNextOutput<OutputImageType>(outputImage);
135     }
136     else {
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();
147       }
148       else {
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();
156       }
157       // Write/Save results
158       this->template SetNextOutput<InputImageType>(outputImage);
159     }
160   }
161   //--------------------------------------------------------------------
162
163
164 }//end clitk
165  
166 #endif //#define clitkBinarizeImageGenericFilter_txx