]> Creatis software - clitk.git/blob - segmentation/clitkMorphoMathGenericFilter.txx
initial entry
[clitk.git] / segmentation / clitkMorphoMathGenericFilter.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 #ifndef clitkMorphoMathGenericFilter_txx
19 #define clitkMorphoMathGenericFilter_txx
20 /**
21    =================================================
22    * @file   clitkMorphoMathGenericFilter.txx
23    * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
24    * @date   5 May 2009
25    * 
26    * @brief 
27    * 
28    =================================================*/
29
30
31 namespace clitk
32 {
33
34
35   //==============================================================================
36   // Update with the number of dimensions
37   //==============================================================================
38   template<unsigned int Dimension>
39   void 
40   MorphoMathGenericFilter::UpdateWithDim(std::string PixelType)
41   {
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>(); 
45     }
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>(); 
49     //    }
50     
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>();
54     }
55     
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>();
59     //     }
60     else {
61       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
62       UpdateWithDimAndPixelType<Dimension, float>();
63     }
64   }
65
66
67   //==============================================================================
68   // Update with the pixel type
69   //==============================================================================
70   template <unsigned int Dimension, class  InputPixelType> 
71   void 
72   MorphoMathGenericFilter::UpdateWithDimAndPixelType()
73   {
74    
75     //---------------------------------
76     // Define the images
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());
88     caster->Update();
89     typename InternalImageType::Pointer input =caster->GetOutput();
90
91
92     //---------------------------------
93     // Find the type of action
94     //---------------------------------
95     typedef itk::ImageToImageFilter<InternalImageType, InternalImageType> ImageFilterType;
96     typename ImageFilterType::Pointer filter; 
97
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];}
104     else 
105       for (unsigned int i=0;i<Dimension;i++)
106         radius[i]=m_ArgsInfo.radius_arg[0];
107
108     structuringElement.SetRadius(radius);
109     structuringElement.CreateStructuringElement();
110
111     switch(m_ArgsInfo.type_arg)
112       {
113
114       case 0:
115         {
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);
122           
123           filter=m;
124           if(m_Verbose) std::cout<<"Using the erode filter..."<<std::endl;
125           break;
126         }
127
128       case 1:
129         {
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);
136
137           filter=m;
138           if(m_Verbose) std::cout<<"Using the dilate filter..."<<std::endl;
139           break;
140         }
141
142       case 2:
143         {
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);
149
150           filter=m;
151           if(m_Verbose) std::cout<<"Using the closing filter..."<<std::endl;
152           break;
153         }
154
155       case 3:
156         {
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);
162
163           filter=m;
164           if(m_Verbose) std::cout<<"Using the opening filter..."<<std::endl;
165           break;
166         }
167
168       case 4:
169         {
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);
176           
177           filter=m;
178           if(m_Verbose) std::cout<<"Using the conditional erode filter..."<<std::endl;
179           break;
180         }
181
182       case 5:
183         {
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);
190           
191           filter=m;
192           if(m_Verbose) std::cout<<"Using the conditional dilate filter..."<<std::endl;
193           break;
194         }
195
196       }
197   
198
199     //---------------------------------
200     // Execute the filter
201     //---------------------------------
202     filter->SetInput(input);
203     
204     try 
205       {
206         filter->Update();
207       }
208     catch( itk::ExceptionObject & err ) 
209       { 
210         std::cerr << "ExceptionObject caught executing the filter!" << std::endl; 
211         std::cerr << err << std::endl; 
212         return;
213       } 
214     
215
216     //---------------------------------
217     // Write the output
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);
226     writer->Update();
227    
228   }
229
230 }//end namespace
231  
232 #endif //#define clitkMorphoMathGenericFilter_txx