]> Creatis software - clitk.git/blob - filters/clitkBinarizeImageGenericFilter.txx
26d324c2ed6540db584dd6dbaa3c6b4dde5f51a7
[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
27 #include <clitkCommon.h>
28 // #include <tiff.h>
29
30 namespace clitk
31 {
32   
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>();
40   }
41   //--------------------------------------------------------------------
42
43
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);
49   }
50   //--------------------------------------------------------------------
51   
52
53   //--------------------------------------------------------------------
54   template<class args_info_type>
55   void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
56     mArgsInfo=a;
57     SetIOVerbose(mArgsInfo.verbose_flag);
58     if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
59
60     if (mArgsInfo.input_given) {
61       SetInputFilename(mArgsInfo.input_arg);
62     }
63     if (mArgsInfo.output_given) {
64       SetOutputFilename(mArgsInfo.output_arg);
65     }
66   }
67   //--------------------------------------------------------------------
68
69
70   //--------------------------------------------------------------------
71   // Update with the number of dimensions and the pixeltype
72   //--------------------------------------------------------------------
73   template<class args_info_type>
74   template<class InputImageType>
75   void 
76   BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
77   {
78
79     // Reading input
80     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
81
82     // Main filter
83     typedef typename InputImageType::PixelType PixelType;
84     typedef itk::Image<uchar, InputImageType::ImageDimension> OutputImageType;
85
86     // Filter
87     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
88     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
89     thresholdFilter->SetInput(input);
90     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
91  
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));
94
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);
104
105 //     DD(mArgsInfo.useFG_flag);
106 //     DD(mArgsInfo.useBG_flag);
107
108 //     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
109
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);
113 //     }
114 //     else {
115 //       DD("0");
116 //       thresholdFilter->SetOutsideValue(0);
117 //     }
118
119 //     // thresholdFilter->Update();
120
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) {
124 // =======
125     /* Three modes : 
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)
129     */
130       if (mArgsInfo.mode_arg == std::string("both")) {
131       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
132       thresholdFilter->Update();
133       //>>>>>>> 1.5
134       typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
135       this->template SetNextOutput<OutputImageType>(outputImage);
136     }
137     else {
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();
148       }
149       else {
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();
157       }
158       // Write/Save results
159       this->template SetNextOutput<InputImageType>(outputImage);
160     }
161   }
162   //--------------------------------------------------------------------
163
164
165 }//end clitk
166  
167 #endif //#define clitkBinarizeImageGenericFilter_txx