]> Creatis software - clitk.git/blob - tools/clitkBinarizeImageGenericFilter.cxx
new gengetopt versions don't seem to like capital letters
[clitk.git] / tools / clitkBinarizeImageGenericFilter.cxx
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_cxx
19 #define clitkBinarizeImageGenericFilter_cxx
20
21 /* =================================================
22  * @file   clitkBinarizeImageGenericFilter.cxx
23  * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
24  * @date   29 june 2009
25  *
26  * @brief
27  *
28  ===================================================*/
29
30 #include "clitkBinarizeImageGenericFilter.h"
31
32 // itk include
33 #include "itkBinaryThresholdImageFilter.h"
34 #include "itkMaskImageFilter.h"
35 #include "itkMaskNegatedImageFilter.h"
36
37 #include <clitkCommon.h>
38
39 namespace clitk
40 {
41
42 //--------------------------------------------------------------------
43 BinarizeImageGenericFilter::BinarizeImageGenericFilter():
44   ImageToImageGenericFilter<Self>("BinarizeImage")
45 {
46   InitializeImageType<2>();
47   InitializeImageType<3>();
48   InitializeImageType<4>();
49 }
50 //--------------------------------------------------------------------
51
52
53 //--------------------------------------------------------------------
54 template<unsigned int Dim>
55 void BinarizeImageGenericFilter::InitializeImageType()
56 {
57   ADD_DEFAULT_IMAGE_TYPES(Dim);
58 }
59 //--------------------------------------------------------------------
60
61
62 //--------------------------------------------------------------------
63 void BinarizeImageGenericFilter::SetArgsInfo(const args_info_type & a)
64 {
65   mArgsInfo=a;
66   SetIOVerbose(mArgsInfo.verbose_flag);
67   if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
68
69   if (mArgsInfo.input_given) {
70     SetInputFilename(mArgsInfo.input_arg);
71   }
72   if (mArgsInfo.output_given) {
73     SetOutputFilename(mArgsInfo.output_arg);
74   }
75 }
76 //--------------------------------------------------------------------
77
78
79 //--------------------------------------------------------------------
80 // Update with the number of dimensions and the pixeltype
81 //--------------------------------------------------------------------
82 template<class InputImageType>
83 void
84 BinarizeImageGenericFilter::UpdateWithInputImageType()
85 {
86
87   // Reading input
88   typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
89
90   // Main filter
91   typedef typename InputImageType::PixelType PixelType;
92   typedef itk::Image<uchar, InputImageType::ImageDimension> OutputImageType;
93
94   // Filter
95   typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
96   typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
97   thresholdFilter->SetInput(input);
98   thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
99
100   if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
101   if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
102
103   /* Three modes :
104      - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
105      - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
106      - both -> use FG and BG (real binary image)
107   */
108   if (mArgsInfo.mode_arg == std::string("both")) {
109     thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
110     thresholdFilter->Update();
111     typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
112     this->template SetNextOutput<OutputImageType>(outputImage);
113   } else {
114     typename InputImageType::Pointer outputImage;
115     thresholdFilter->SetOutsideValue(0);
116     if (mArgsInfo.mode_arg == std::string("BG")) {
117       typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
118       typename maskFilterType::Pointer maskFilter = maskFilterType::New();
119       maskFilter->SetInput1(input);
120       maskFilter->SetInput2(thresholdFilter->GetOutput());
121       maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
122       maskFilter->Update();
123       outputImage = maskFilter->GetOutput();
124     } else {
125       typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
126       typename maskFilterType::Pointer maskFilter = maskFilterType::New();
127       maskFilter->SetInput1(input);
128       maskFilter->SetInput2(thresholdFilter->GetOutput());
129       maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
130       maskFilter->Update();
131       outputImage = maskFilter->GetOutput();
132     }
133     // Write/Save results
134     this->template SetNextOutput<InputImageType>(outputImage);
135   }
136 }
137 //--------------------------------------------------------------------
138
139
140 }//end clitk
141
142 #endif  //#define clitkBinarizeImageGenericFilter_cxx