]> Creatis software - clitk.git/blob - segmentation/clitkDecomposeAndReconstructGenericFilter.txx
initial entry
[clitk.git] / segmentation / clitkDecomposeAndReconstructGenericFilter.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 clitkDecomposeAndReconstructGenericFilter_txx
19 #define clitkDecomposeAndReconstructGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkDecomposeAndReconstructGenericFilter.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   DecomposeAndReconstructGenericFilter::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       std::cout  << "WARNING: Working in unsigned pixelType, remember to set the Erosion Padding Value to a suitable value!"<< std::endl;
55       UpdateWithDimAndPixelType<Dimension, unsigned char>();
56   
57     }
58     
59     //     else if (PixelType == "char"){ 
60     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
61     //       UpdateWithDimAndPixelType<Dimension, signed char>();
62     //     }
63     else {
64       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
65       UpdateWithDimAndPixelType<Dimension, float>();
66     }
67   }
68
69
70   //-------------------------------------------------------------------
71   // Update with the number of dimensions and the pixeltype
72   //-------------------------------------------------------------------
73   template <unsigned int Dimension, class  PixelType> 
74   void 
75   DecomposeAndReconstructGenericFilter::UpdateWithDimAndPixelType()
76   {
77
78     // ImageTypes
79     typedef itk::Image<PixelType, Dimension> InputImageType;
80     typedef itk::Image<int, Dimension> InternalImageType;
81     typedef itk::Image<PixelType, Dimension> OutputImageType;
82     
83     // Read the input
84     typedef itk::ImageFileReader<InputImageType> InputReaderType;
85     typename InputReaderType::Pointer reader = InputReaderType::New();
86     reader->SetFileName( m_InputFileName);
87     reader->Update();
88     typename InputImageType::Pointer input= reader->GetOutput();
89
90     // Structuring element radius
91     typename InputImageType::SizeType radius;
92     if (m_ArgsInfo.radius_given==Dimension)
93       for (unsigned int i=0;i<Dimension;i++)
94         {radius[i]=m_ArgsInfo.radius_arg[i];}
95     else 
96       for (unsigned int i=0;i<Dimension;i++)
97         radius[i]=m_ArgsInfo.radius_arg[0];
98
99     // Cast
100     typedef itk::CastImageFilter<InputImageType, InternalImageType> InputCastImageFilterType;
101     typename InputCastImageFilterType::Pointer inputCaster= InputCastImageFilterType::New();
102     inputCaster->SetInput(input);
103     inputCaster->Update();
104
105     // Filter
106     typedef itk::ImageToImageFilter<InternalImageType, InternalImageType> ImageToImageFilterType;
107     typename ImageToImageFilterType::Pointer filter;
108
109     switch (m_ArgsInfo.type_arg)
110       {
111       case 0:
112         {
113           typedef clitk::DecomposeThroughErosionImageFilter<InternalImageType,InternalImageType> FilterType;
114           typename FilterType::Pointer f=FilterType::New();
115           f->SetVerbose(m_Verbose);
116           f->SetFullyConnected(m_ArgsInfo.full_flag);
117           f->SetErosionPaddingValue(m_ArgsInfo.pad_arg);
118           f->SetMinimumNumberOfIterations(m_ArgsInfo.min_arg);
119           f->SetMinimumObjectSize(m_ArgsInfo.minSize_arg);
120           f->SetRadius(radius);
121           f->SetNumberOfNewLabels(m_ArgsInfo.new_arg);
122           if(m_Verbose) std::cout<<"Using the DecomposeThroughErosionImageFilter..."<<std::endl;
123           filter=f;
124           break;
125         }
126    
127       case 1:
128         {
129           typedef clitk::ReconstructThroughDilationImageFilter<InternalImageType,InternalImageType> FilterType;
130           typename FilterType::Pointer f=FilterType::New();
131           f->SetVerbose(m_Verbose);
132           f->SetErosionPaddingValue(m_ArgsInfo.pad_arg);
133           f->SetRadius(radius);
134           f->SetMaximumNumberOfLabels(m_ArgsInfo.max_arg);
135           f->SetBackgroundValue(m_ArgsInfo.bg_arg);
136           f->SetForegroundValue(m_ArgsInfo.fg_arg);
137           if(m_Verbose) std::cout<<"Using the ReconstructThroughDilationImageFilter..."<<std::endl;
138           filter=f;
139           break;
140         }
141       case 2:
142         {
143           typedef clitk::DecomposeAndReconstructImageFilter<InternalImageType,InternalImageType> FilterType;
144           typename FilterType::Pointer f=FilterType::New();
145           f->SetVerbose(m_Verbose);
146           f->SetRadius(radius);
147           f->SetMaximumNumberOfLabels(m_ArgsInfo.max_arg);
148           f->SetMinimumNumberOfIterations(m_ArgsInfo.min_arg);
149           f->SetBackgroundValue(m_ArgsInfo.bg_arg);
150           f->SetForegroundValue(m_ArgsInfo.fg_arg);
151           f->SetFullyConnected(m_ArgsInfo.full_flag);
152           f->SetNumberOfNewLabels(m_ArgsInfo.new_arg);
153           f->SetMinimumObjectSize(m_ArgsInfo.minSize_arg);
154           if(m_Verbose) std::cout<<"Using the DecomposeAndReconstructImageFilter..."<<std::endl;
155           filter=f;
156           break;
157         }
158       }
159     
160     filter->SetInput(inputCaster->GetOutput());
161     filter->Update();
162     typename InternalImageType::Pointer output= filter->GetOutput();
163
164     // Cast
165     typedef itk::CastImageFilter<InternalImageType, OutputImageType> OutputCastImageFilterType;
166     typename OutputCastImageFilterType::Pointer outputCaster= OutputCastImageFilterType::New();
167     outputCaster->SetInput(output);
168     outputCaster->Update();
169
170     // Output
171     typedef itk::ImageFileWriter<OutputImageType> WriterType;
172     typename WriterType::Pointer writer = WriterType::New();
173     writer->SetFileName(m_ArgsInfo.output_arg);
174     writer->SetInput(outputCaster->GetOutput());
175     writer->Update();
176
177   }
178
179
180 }//end clitk
181  
182 #endif //#define clitkDecomposeAndReconstructGenericFilter_txx