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