]> Creatis software - clitk.git/blob - filters/clitkBinarizeImageGenericFilter.txx
Added Manual Registration Tool with updated Median Filter tool
[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   // Update with the number of dimensions and the pixeltype
71   //--------------------------------------------------------------------
72   template<class args_info_type>
73   template<class InputImageType>
74   void 
75   BinarizeImageGenericFilter<args_info_type>::UpdateWithInputImageType()
76   {
77
78     // Reading input
79     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
80
81     // Main filter
82     typedef typename InputImageType::PixelType PixelType;
83     typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
84
85     // Filter
86     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
87     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
88     thresholdFilter->SetInput(input);
89     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
90  
91     if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
92     if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
93
94     // DD(mArgsInfo.lower_given);
95     // DD(mArgsInfo.upper_given);
96     // DD(mArgsInfo.lower_arg);
97     // DD(mArgsInfo.upper_arg);
98     // DD(mArgsInfo.fg_arg);
99     // DD(mArgsInfo.bg_arg);
100     // DD(mArgsInfo.fg_given);
101     // DD(mArgsInfo.bg_given);
102     // DD(mArgsInfo.mode_arg);
103
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