]> Creatis software - clitk.git/blob - tools/clitkBinarizeImageGenericFilter.cxx
Merge branch 'master' into GammaIndex3D
[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://www.centreleonberard.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   if (mArgsInfo.verbose_given)
67     SetIOVerbose(mArgsInfo.verbose_flag);
68   if (mArgsInfo.imagetypes_given && mArgsInfo.imagetypes_flag)
69     this->PrintAvailableImageTypes();
70
71   if (mArgsInfo.input_given) {
72     SetInputFilename(mArgsInfo.input_arg);
73   }
74   if (mArgsInfo.output_given) {
75     SetOutputFilename(mArgsInfo.output_arg);
76   }
77 }
78 //--------------------------------------------------------------------
79
80
81 //--------------------------------------------------------------------
82 // Update with the number of dimensions and the pixeltype
83 //--------------------------------------------------------------------
84 template<class InputImageType>
85 void
86 BinarizeImageGenericFilter::UpdateWithInputImageType()
87 {
88
89   // Reading input
90   typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
91
92   // Main filter
93   typedef typename InputImageType::PixelType PixelType;
94   typedef itk::Image<uchar, InputImageType::ImageDimension> OutputImageType;
95
96   // Filter
97   typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
98   typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
99   thresholdFilter->SetInput(input);
100   thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
101
102   if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
103   if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
104
105   /* Three modes :
106      - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
107      - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
108      - both -> use FG and BG (real binary image)
109   */
110   if (mArgsInfo.mode_arg == std::string("both")) {
111     thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
112     thresholdFilter->Update();
113     typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
114     this->template SetNextOutput<OutputImageType>(outputImage);
115   } else {
116     typename InputImageType::Pointer outputImage;
117     thresholdFilter->SetOutsideValue(0);
118     if (mArgsInfo.mode_arg == std::string("BG")) {
119       typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
120       typename maskFilterType::Pointer maskFilter = maskFilterType::New();
121       maskFilter->SetInput1(input);
122       maskFilter->SetInput2(thresholdFilter->GetOutput());
123       maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
124       maskFilter->Update();
125       outputImage = maskFilter->GetOutput();
126     } else {
127       typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
128       typename maskFilterType::Pointer maskFilter = maskFilterType::New();
129       maskFilter->SetInput1(input);
130       maskFilter->SetInput2(thresholdFilter->GetOutput());
131       maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
132       maskFilter->Update();
133       outputImage = maskFilter->GetOutput();
134     }
135     // Write/Save results
136     this->template SetNextOutput<InputImageType>(outputImage);
137   }
138 }
139 //--------------------------------------------------------------------
140
141
142 }//end clitk
143
144 #endif  //#define clitkBinarizeImageGenericFilter_cxx