]> Creatis software - clitk.git/blob - filters/clitkBinarizeImageGenericFilter.txx
- remove tiff (do not compile)
[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_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);
55   }
56   //--------------------------------------------------------------------
57   
58
59   //--------------------------------------------------------------------
60   template<class args_info_type>
61   void BinarizeImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
62     mArgsInfo=a;
63     SetIOVerbose(mArgsInfo.verbose_flag);
64     if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
65
66     if (mArgsInfo.input_given) {
67       SetInputFilename(mArgsInfo.input_arg);
68     }
69     if (mArgsInfo.output_given) {
70       SetOutputFilename(mArgsInfo.output_arg);
71     }
72   }
73   //--------------------------------------------------------------------
74
75   //--------------------------------------------------------------------
76   // Update with the number of dimensions and the pixeltype
77   //--------------------------------------------------------------------
78   template<class args_info_type>
79   template<class InputImageType>
80   void 
81   BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
82   {
83
84     // Reading input
85     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
86
87     // Main filter
88     typedef typename InputImageType::PixelType PixelType;
89     typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
90
91     // Filter
92     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
93     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
94     thresholdFilter->SetInput(input);
95     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
96  
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));
99
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);
109
110 // <<<<<<< clitkBinarizeImageGenericFilter.txx
111 //     DD(mArgsInfo.useFG_flag);
112 //     DD(mArgsInfo.useBG_flag);
113
114 //     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
115
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);
119 //     }
120 //     else {
121 //       DD("0");
122 //       thresholdFilter->SetOutsideValue(0);
123 //     }
124
125 //     // thresholdFilter->Update();
126
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) {
130 // =======
131     /* Three modes : 
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)
135     */
136       if (mArgsInfo.mode_arg == std::string("both")) {
137       thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
138       thresholdFilter->Update();
139       //>>>>>>> 1.5
140       typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
141       this->template SetNextOutput<OutputImageType>(outputImage);
142     }
143     else {
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();
154       }
155       else {
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();
163       }
164       // Write/Save results
165       this->template SetNextOutput<InputImageType>(outputImage);
166     }
167   }
168   //--------------------------------------------------------------------
169
170
171 }//end clitk
172  
173 #endif //#define clitkBinarizeImageGenericFilter_txx