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://oncora1.lyon.fnclcc.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 clitkMorphoMathGenericFilter_txx
19 #define clitkMorphoMathGenericFilter_txx
21 =================================================
22 * @file clitkMorphoMathGenericFilter.txx
23 * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
28 =================================================*/
35 //==============================================================================
36 // Update with the number of dimensions
37 //==============================================================================
38 template<unsigned int Dimension>
40 MorphoMathGenericFilter::UpdateWithDim(std::string PixelType)
42 if(PixelType == "short"){
43 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
44 UpdateWithDimAndPixelType<Dimension, signed short>();
46 // else if(PixelType == "unsigned_short"){
47 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
48 // UpdateWithDimAndPixelType<Dimension, unsigned short>();
51 else if (PixelType == "unsigned_char"){
52 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
53 UpdateWithDimAndPixelType<Dimension, unsigned char>();
56 // else if (PixelType == "char"){
57 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
58 // UpdateWithDimAndPixelType<Dimension, signed char>();
61 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
62 UpdateWithDimAndPixelType<Dimension, float>();
67 //==============================================================================
68 // Update with the pixel type
69 //==============================================================================
70 template <unsigned int Dimension, class InputPixelType>
72 MorphoMathGenericFilter::UpdateWithDimAndPixelType()
75 //---------------------------------
77 //---------------------------------
78 typedef float InternalPixelType;
79 typedef itk::Image<InputPixelType, Dimension> InputImageType;
80 typedef itk::Image<InternalPixelType, Dimension> InternalImageType;
81 typedef itk::Image<InputPixelType, Dimension> OutputImageType;
82 typedef itk::ImageFileReader<InputImageType> FileReaderType;
83 typename FileReaderType::Pointer fileReader=FileReaderType::New();
84 fileReader->SetFileName(m_InputFileName);
85 typedef itk::CastImageFilter<InputImageType, InternalImageType> InputCastImageFilterType;
86 typename InputCastImageFilterType::Pointer caster = InputCastImageFilterType::New();
87 caster->SetInput(fileReader->GetOutput());
89 typename InternalImageType::Pointer input =caster->GetOutput();
92 //---------------------------------
93 // Find the type of action
94 //---------------------------------
95 typedef itk::ImageToImageFilter<InternalImageType, InternalImageType> ImageFilterType;
96 typename ImageFilterType::Pointer filter;
98 typedef itk::BinaryBallStructuringElement<InputPixelType,Dimension > KernelType;
99 KernelType structuringElement;
100 typename InternalImageType::SizeType radius;
101 if (m_ArgsInfo.radius_given==Dimension)
102 for (unsigned int i=0;i<Dimension;i++)
103 {radius[i]=m_ArgsInfo.radius_arg[i];}
105 for (unsigned int i=0;i<Dimension;i++)
106 radius[i]=m_ArgsInfo.radius_arg[0];
108 structuringElement.SetRadius(radius);
109 structuringElement.CreateStructuringElement();
111 switch(m_ArgsInfo.type_arg)
116 typedef itk::BinaryErodeImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
117 typename FilterType::Pointer m = FilterType::New();
118 m->SetBackgroundValue(m_ArgsInfo.bg_arg);
119 m->SetForegroundValue(m_ArgsInfo.fg_arg);
120 m->SetBoundaryToForeground(m_ArgsInfo.bound_flag);
121 m->SetKernel(structuringElement);
124 if(m_Verbose) std::cout<<"Using the erode filter..."<<std::endl;
130 typedef itk::BinaryDilateImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
131 typename FilterType::Pointer m = FilterType::New();
132 m->SetBackgroundValue(m_ArgsInfo.bg_arg);
133 m->SetForegroundValue(m_ArgsInfo.fg_arg);
134 m->SetBoundaryToForeground(m_ArgsInfo.bound_flag);
135 m->SetKernel(structuringElement);
138 if(m_Verbose) std::cout<<"Using the dilate filter..."<<std::endl;
144 typedef itk::BinaryMorphologicalClosingImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
145 typename FilterType::Pointer m = FilterType::New();
146 m->SetForegroundValue(m_ArgsInfo.fg_arg);
147 m->SetSafeBorder(m_ArgsInfo.bound_flag);
148 m->SetKernel(structuringElement);
151 if(m_Verbose) std::cout<<"Using the closing filter..."<<std::endl;
157 typedef itk::BinaryMorphologicalOpeningImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
158 typename FilterType::Pointer m = FilterType::New();
159 m->SetBackgroundValue(m_ArgsInfo.bg_arg);
160 m->SetForegroundValue(m_ArgsInfo.fg_arg);
161 m->SetKernel(structuringElement);
164 if(m_Verbose) std::cout<<"Using the opening filter..."<<std::endl;
170 typedef clitk::ConditionalBinaryErodeImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
171 typename FilterType::Pointer m = FilterType::New();
172 m->SetBackgroundValue(m_ArgsInfo.bg_arg);
173 m->SetForegroundValue(m_ArgsInfo.fg_arg);
174 m->SetBoundaryToForeground(m_ArgsInfo.bound_flag);
175 m->SetKernel(structuringElement);
178 if(m_Verbose) std::cout<<"Using the conditional erode filter..."<<std::endl;
184 typedef clitk::ConditionalBinaryDilateImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
185 typename FilterType::Pointer m = FilterType::New();
186 m->SetBackgroundValue(m_ArgsInfo.bg_arg);
187 m->SetForegroundValue(m_ArgsInfo.fg_arg);
188 m->SetBoundaryToForeground(m_ArgsInfo.bound_flag);
189 m->SetKernel(structuringElement);
192 if(m_Verbose) std::cout<<"Using the conditional dilate filter..."<<std::endl;
199 //---------------------------------
200 // Execute the filter
201 //---------------------------------
202 filter->SetInput(input);
208 catch( itk::ExceptionObject & err )
210 std::cerr << "ExceptionObject caught executing the filter!" << std::endl;
211 std::cerr << err << std::endl;
216 //---------------------------------
218 //---------------------------------
219 typedef itk::CastImageFilter< InternalImageType, OutputImageType > OutputCastImageFilterType;
220 typename OutputCastImageFilterType::Pointer oCaster = OutputCastImageFilterType::New();
221 oCaster->SetInput(filter->GetOutput());
222 typedef itk::ImageFileWriter<OutputImageType> FileWriterType;
223 typename FileWriterType::Pointer writer=FileWriterType::New();
224 writer->SetInput(oCaster->GetOutput());
225 writer->SetFileName(m_ArgsInfo.output_arg);
232 #endif //#define clitkMorphoMathGenericFilter_txx