1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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
21 /* =================================================
22 * @file clitkBinarizeImageGenericFilter.cxx
23 * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
28 ===================================================*/
30 #include "clitkBinarizeImageGenericFilter.h"
33 #include "itkBinaryThresholdImageFilter.h"
34 #include "itkMaskImageFilter.h"
35 #include "itkMaskNegatedImageFilter.h"
37 #include <clitkCommon.h>
42 //--------------------------------------------------------------------
43 BinarizeImageGenericFilter::BinarizeImageGenericFilter():
44 ImageToImageGenericFilter<Self>("BinarizeImage")
46 InitializeImageType<2>();
47 InitializeImageType<3>();
48 InitializeImageType<4>();
50 //--------------------------------------------------------------------
53 //--------------------------------------------------------------------
54 template<unsigned int Dim>
55 void BinarizeImageGenericFilter::InitializeImageType()
57 ADD_DEFAULT_IMAGE_TYPES(Dim);
59 //--------------------------------------------------------------------
62 //--------------------------------------------------------------------
63 void BinarizeImageGenericFilter::SetArgsInfo(const args_info_type & a)
66 if (mArgsInfo.verbose_given)
67 SetIOVerbose(mArgsInfo.verbose_flag);
68 if (mArgsInfo.imagetypes_given && mArgsInfo.imagetypes_flag)
69 this->PrintAvailableImageTypes();
71 if (mArgsInfo.input_given) {
72 SetInputFilename(mArgsInfo.input_arg);
74 if (mArgsInfo.output_given) {
75 SetOutputFilename(mArgsInfo.output_arg);
78 //--------------------------------------------------------------------
81 //--------------------------------------------------------------------
82 // Update with the number of dimensions and the pixeltype
83 //--------------------------------------------------------------------
84 template<class InputImageType>
86 BinarizeImageGenericFilter::UpdateWithInputImageType()
90 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
93 typedef typename InputImageType::PixelType PixelType;
94 typedef itk::Image<uchar, InputImageType::ImageDimension> OutputImageType;
97 typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
98 typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
99 thresholdFilter->SetInput(input);
100 thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
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));
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)
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);
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();
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();
135 // Write/Save results
136 this->template SetNextOutput<InputImageType>(outputImage);
139 //--------------------------------------------------------------------
144 #endif //#define clitkBinarizeImageGenericFilter_cxx