]> Creatis software - clitk.git/blob - segmentation/clitkMorphoReconstructionGenericFilter.txx
685840680c976fc672193027ff727fd325ab46e0
[clitk.git] / segmentation / clitkMorphoReconstructionGenericFilter.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://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 clitkMorphoReconstructionGenericFilter_txx
19 #define clitkMorphoReconstructionGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkMorphoReconstructionGenericFilter.txx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30
31 namespace clitk
32 {
33
34   //-------------------------------------------------------------------
35   // Update with the number of dimensions
36   //-------------------------------------------------------------------
37   template<unsigned int Dimension>
38   void 
39   MorphoReconstructionGenericFilter::UpdateWithDim(std::string PixelType)
40   {
41     if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
42
43     if(PixelType == "short"){  
44       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
45       UpdateWithDimAndPixelType<Dimension, signed short>(); 
46     }
47     //    else if(PixelType == "unsigned_short"){  
48     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
49     //       UpdateWithDimAndPixelType<Dimension, unsigned short>(); 
50     //     }
51     
52     else if (PixelType == "unsigned_char"){ 
53       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
54       UpdateWithDimAndPixelType<Dimension, unsigned char>();
55     }
56     
57     //     else if (PixelType == "char"){ 
58     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
59     //       UpdateWithDimAndPixelType<Dimension, signed char>();
60     //     }
61     else {
62       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
63       UpdateWithDimAndPixelType<Dimension, float>();
64     }
65   }
66
67
68   //-------------------------------------------------------------------
69   // Update with the number of dimensions and the pixeltype
70   //-------------------------------------------------------------------
71   template <unsigned int Dimension, class  PixelType> 
72   void 
73   MorphoReconstructionGenericFilter::UpdateWithDimAndPixelType()
74   {
75
76     // ImageTypes
77     typedef itk::Image<PixelType, Dimension> InputImageType;
78     typedef itk::Image<PixelType, Dimension> OutputImageType;
79     
80     // Read the input
81     typedef itk::ImageFileReader<InputImageType> InputReaderType;
82     typename InputReaderType::Pointer reader = InputReaderType::New();
83     reader->SetFileName( m_InputFileName);
84     reader->Update();
85     typename InputImageType::Pointer input= reader->GetOutput();
86
87     // Read the mask
88     typename InputImageType::Pointer mask;
89     if (m_ArgsInfo.mask_given)
90       {
91         typename InputReaderType::Pointer maskReader=InputReaderType::New();
92         maskReader->SetFileName(m_ArgsInfo.mask_arg);
93         maskReader->Update();
94         mask=maskReader->GetOutput();
95       }
96
97
98     //---------------------------------
99     // Find the type of action
100     //---------------------------------
101     typedef itk::ImageToImageFilter<InputImageType, InputImageType> ImageFilterType;
102     typename ImageFilterType::Pointer filter; 
103
104     typedef itk::BinaryBallStructuringElement<PixelType, Dimension > KernelType;
105     KernelType structuringElement;
106     structuringElement.SetRadius(m_ArgsInfo.radius_arg);
107     structuringElement.CreateStructuringElement();
108
109
110     switch(m_ArgsInfo.type_arg)
111       {
112
113       case 0:
114         {
115           typedef itk::ReconstructionByErosionImageFilter<InputImageType, OutputImageType> FilterType;
116           typename FilterType::Pointer m = FilterType::New();
117           m->SetMarkerImage(input);
118           m->SetMaskImage(mask);
119
120           filter=m;
121           if(m_Verbose) std::cout<<"Using the reconstruction by erosion filter..."<<std::endl;
122           break;
123         }
124
125       case 1:
126         {
127           typedef itk::ReconstructionByDilationImageFilter<InputImageType, OutputImageType> FilterType;
128           typename FilterType::Pointer m = FilterType::New();
129           m->SetMarkerImage(input);
130           m->SetMaskImage(mask);
131
132           filter=m;
133           if(m_Verbose) std::cout<<"Using the reconstruction by dilation filter..."<<std::endl;
134           break;
135           break;
136         }
137
138       case 2:
139         {
140           typedef itk::ClosingByReconstructionImageFilter<InputImageType, OutputImageType, KernelType> FilterType;
141           typename FilterType::Pointer m = FilterType::New();
142           m->SetInput(input);
143           m->SetKernel(structuringElement);
144           m->SetFullyConnected(m_ArgsInfo.full_flag);
145           m->SetPreserveIntensities(m_ArgsInfo.int_flag);
146
147           filter=m;
148           if(m_Verbose) std::cout<<"Using the closing by reconstruction filter..."<<std::endl;
149           break;
150          
151         }
152
153       case 3:
154         {
155           typedef itk::OpeningByReconstructionImageFilter<InputImageType, OutputImageType, KernelType> FilterType;
156           typename FilterType::Pointer m = FilterType::New();
157           m->SetInput(input);
158           m->SetKernel(structuringElement);
159           m->SetFullyConnected(m_ArgsInfo.full_flag);
160           m->SetPreserveIntensities(m_ArgsInfo.int_flag);
161
162           filter=m;
163           if(m_Verbose) std::cout<<"Using the opening by reconstruction filter..."<<std::endl;
164           break;
165         }
166       }
167   
168
169     //---------------------------------
170     // Execute the filter
171     //---------------------------------
172     filter->SetInput(input);
173     try 
174       {
175         filter->Update();
176       }
177     catch( itk::ExceptionObject & err ) 
178       { 
179         std::cerr << "ExceptionObject caught executing the filter!" << std::endl; 
180         std::cerr << err << std::endl; 
181         return;
182       } 
183     typename OutputImageType::Pointer output=filter->GetOutput();
184  
185
186     
187     // Output
188     typedef itk::ImageFileWriter<OutputImageType> WriterType;
189     typename WriterType::Pointer writer = WriterType::New();
190     writer->SetFileName(m_ArgsInfo.output_arg);
191     writer->SetInput(output);
192     writer->Update();
193
194   }
195
196
197 }//end clitk
198  
199 #endif //#define clitkMorphoReconstructionGenericFilter_txx