]> Creatis software - clitk.git/blob - filters/clitkBinarizeImageGenericFilter.txx
2076e71e4204527e347aae4b268ca36b6ebaac4b
[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 #ifndef clitkBinarizeImageGenericFilter_txx
19 #define clitkBinarizeImageGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkBinarizeImageGenericFilter.txx
23  * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
24  * @date   29 june 2009
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30 // itk include
31 #include "itkBinaryThresholdImageFilter.h"
32 #include "itkMaskImageFilter.h"
33 #include "itkMaskNegatedImageFilter.h"
34 #include <clitkCommon.h>
35 #include <tiff.h>
36
37 namespace clitk
38 {
39   
40   //--------------------------------------------------------------------
41   template<class args_info_type>
42   BinarizeImageGenericFilter<args_info_type>::BinarizeImageGenericFilter():
43     ImageToImageGenericFilter<Self>("Binarize") {
44     InitializeImageType<2>();
45     InitializeImageType<3>();
46     InitializeImageType<4>();
47   }
48   //--------------------------------------------------------------------
49
50
51   //--------------------------------------------------------------------
52   template<class args_info_type>
53   template<unsigned int Dim>
54   void BinarizeImageGenericFilter<args_info_type>::InitializeImageType() {      
55     ADD_IMAGE_TYPE(Dim, char);
56     ADD_IMAGE_TYPE(Dim, uchar);
57     ADD_IMAGE_TYPE(Dim, short);
58     ADD_IMAGE_TYPE(Dim, ushort);
59     ADD_IMAGE_TYPE(Dim, int);
60     ADD_IMAGE_TYPE(Dim, uint16);
61     ADD_IMAGE_TYPE(Dim, float);
62     ADD_IMAGE_TYPE(Dim, double);
63   }
64   //--------------------------------------------------------------------
65   
66
67   //--------------------------------------------------------------------
68   template<class args_info_type>
69   void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
70     mArgsInfo=a;
71     SetIOVerbose(mArgsInfo.verbose_flag);
72     if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
73
74     if (mArgsInfo.input_given) {
75       SetInputFilename(mArgsInfo.input_arg);
76     }
77     if (mArgsInfo.output_given) {
78       SetOutputFilename(mArgsInfo.output_arg);
79     }
80   }
81   //--------------------------------------------------------------------
82
83   //--------------------------------------------------------------------
84   // Update with the number of dimensions and the pixeltype
85   //--------------------------------------------------------------------
86   template<class args_info_type>
87   template<class InputImageType>
88   void 
89   BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
90   {
91
92     // Reading input
93     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
94
95     // Main filter
96     typedef typename InputImageType::PixelType PixelType;
97     typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
98
99     // Filter
100     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
101     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
102     thresholdFilter->SetInput(input);
103     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
104  
105     if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
106     if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
107
108     // DD(mArgsInfo.lower_given);
109     // DD(mArgsInfo.upper_given);
110     // DD(mArgsInfo.lower_arg);
111     // DD(mArgsInfo.upper_arg);
112     // DD(mArgsInfo.fg_arg);
113     // DD(mArgsInfo.bg_arg);
114     // DD(mArgsInfo.fg_given);
115     // DD(mArgsInfo.bg_given);
116     // DD(mArgsInfo.mode_arg);
117
118 // <<<<<<< clitkBinarizeImageGenericFilter.txx
119 //     DD(mArgsInfo.useFG_flag);
120 //     DD(mArgsInfo.useBG_flag);
121
122 //     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
123
124 //     // Keep BG value to 0 if maskFilterType is used after
125 //     if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
126 //       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
127 //     }
128 //     else {
129 //       DD("0");
130 //       thresholdFilter->SetOutsideValue(0);
131 //     }
132
133 //     // thresholdFilter->Update();
134
135 //     // If no BG or no FG : new image, copy input with MaskImageFilter
136 //     // If setFG -> FG BG have been changed
137 //     if (mArgsInfo.useBG_flag && mArgsInfo.useFG_flag) {
138 // =======
139     /* Three modes : 
140        - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
141        - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
142        - both -> use FG and BG (real binary image)
143     */
144       if (mArgsInfo.mode_arg == std::string("both")) {
145       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
146       thresholdFilter->Update();
147       //>>>>>>> 1.5
148       typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
149       this->template SetNextOutput<OutputImageType>(outputImage);
150     }
151     else {
152       typename InputImageType::Pointer outputImage;
153       thresholdFilter->SetOutsideValue(0);
154       if (mArgsInfo.mode_arg == std::string("BG")) {
155         typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
156         typename maskFilterType::Pointer maskFilter = maskFilterType::New();
157         maskFilter->SetInput1(input);
158         maskFilter->SetInput2(thresholdFilter->GetOutput());
159         maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
160         maskFilter->Update();
161         outputImage = maskFilter->GetOutput();
162       }
163       else {
164         typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
165         typename maskFilterType::Pointer maskFilter = maskFilterType::New();
166         maskFilter->SetInput1(input);
167         maskFilter->SetInput2(thresholdFilter->GetOutput());
168         maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
169         maskFilter->Update();
170         outputImage = maskFilter->GetOutput();
171       }
172       // Write/Save results
173       this->template SetNextOutput<InputImageType>(outputImage);
174     }
175   }
176   //--------------------------------------------------------------------
177
178
179 }//end clitk
180  
181 #endif //#define clitkBinarizeImageGenericFilter_txx