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