]> Creatis software - clitk.git/blob - segmentation/clitkMorphoMathFilter.txx
Remove warnings
[clitk.git] / segmentation / clitkMorphoMathFilter.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
19 //--------------------------------------------------------------------
20 template<class ImageType>
21 clitk::MorphoMathFilter<ImageType>::MorphoMathFilter():
22   clitk::FilterBase(),
23   itk::ImageToImageFilter<ImageType, ImageType>()
24 {
25   this->SetNumberOfRequiredInputs(1);
26   SetBackgroundValue(0);
27   SetForegroundValue(1);
28   SetOperationType(0);
29   SizeType p;
30   p.Fill(1);
31   SetRadius(p);
32   SetBoundaryToForegroundFlag(false);
33   VerboseFlagOff();
34 }
35 //--------------------------------------------------------------------
36
37
38 //--------------------------------------------------------------------
39 template<class ImageType>
40 clitk::MorphoMathFilter<ImageType>::~MorphoMathFilter()
41 {
42   // Nothing
43 }
44 //--------------------------------------------------------------------
45
46
47 //--------------------------------------------------------------------
48 template<class ImageType>
49 void clitk::MorphoMathFilter<ImageType>::
50 SetRadiusInMM(PointType & p)
51 {
52   m_RadiusInMM = p;
53   m_RadiusInMMIsSet = true;
54   m_RadiusIsSet = false;
55 }
56 //--------------------------------------------------------------------
57
58
59 //--------------------------------------------------------------------
60 template<class ImageType>
61 void clitk::MorphoMathFilter<ImageType>::
62 SetRadius(SizeType & p)
63 {
64   m_Radius = p;
65   m_RadiusIsSet = true;
66   m_RadiusInMMIsSet = false;
67 }
68 //--------------------------------------------------------------------
69
70
71 //--------------------------------------------------------------------
72 template<class ImageType>
73 void clitk::MorphoMathFilter<ImageType>::
74 SetRadius(int r)
75 {
76   for(uint i=0; i<ImageType::ImageDimension; i++)
77     m_Radius[i] = r;
78   SetRadius(m_Radius);
79 }
80 //--------------------------------------------------------------------
81
82
83 //--------------------------------------------------------------------
84 template<class ImageType>
85 void clitk::MorphoMathFilter<ImageType>::
86 SetOperationType(int type)
87 {
88   if(type<0 || type>5)
89     clitkExceptionMacro("Operation type must be between 0-5 (0=Erode, 1=Dilate, 2=Close (erode(dilate(x))), 3=Open (dilate(erode(x))), 4=CondErode, 5=CondDilate)");
90   m_OperationType = OperationTypeEnumeration(type);
91 }
92 //--------------------------------------------------------------------
93
94
95 //--------------------------------------------------------------------
96 template <class ImageType>
97 void 
98 clitk::MorphoMathFilter<ImageType>::
99 GenerateInputRequestedRegion() 
100 {
101   // Call default
102   itk::ImageToImageFilter<ImageType, ImageType>::GenerateInputRequestedRegion();
103   // Get input pointers and set requested region to common region
104   ImagePointer input1 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
105   input1->SetRequestedRegion(input1->GetLargestPossibleRegion());
106 }
107 //--------------------------------------------------------------------
108
109
110 //--------------------------------------------------------------------
111 template <class ImageType>
112 void 
113 clitk::MorphoMathFilter<ImageType>::
114 GenerateOutputInformation() 
115 {
116   //---------------------------------
117   // Define the images
118   //---------------------------------
119   ImagePointer m_input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
120   const unsigned int dim = ImageType::ImageDimension;
121
122   //---------------------------------
123   // Cast into internalimagetype
124   //---------------------------------
125   typedef itk::CastImageFilter<ImageType, InternalImageType> InputCastImageFilterType;
126   typename InputCastImageFilterType::Pointer caster = InputCastImageFilterType::New();
127   caster->SetInput(m_input);
128   caster->Update();
129   input =caster->GetOutput();
130   
131   //---------------------------------
132   // Compute the radius in pixel
133   //---------------------------------
134   if (m_RadiusInMMIsSet) {
135     for(uint i=0; i<dim; i++) {
136       m_Radius[i] = (uint)lrint(m_RadiusInMM[i]/input->GetSpacing()[i]);
137     }
138   }
139
140   //---------------------------------
141   // Extend the image if needed
142   //---------------------------------
143   if (GetExtendSupportFlag()) {
144     typedef itk::ConstantPadImageFilter<InternalImageType, InternalImageType> PadFilterType;
145     typename PadFilterType::Pointer padFilter = PadFilterType::New();
146     padFilter->SetInput(input);
147     typename ImageType::SizeType lower;
148     typename ImageType::SizeType upper;
149     for(uint i=0; i<dim; i++) {
150       lower[i] = upper[i] = 2*(m_Radius[i]+1);
151     }
152     padFilter->SetPadLowerBound(lower);
153     padFilter->SetPadUpperBound(upper);
154     padFilter->Update();
155     if (GetVerboseFlag()) std::cout << "Extend the image to 2x " << lower << std::endl;
156     input = padFilter->GetOutput();
157   }
158   
159   // Set output size
160   ImagePointer outputImage = this->GetOutput(0);
161   outputImage->SetRegions(input->GetLargestPossibleRegion());
162 }
163 //--------------------------------------------------------------------
164
165
166 //--------------------------------------------------------------------
167 template <class ImageType>
168 void 
169 clitk::MorphoMathFilter<ImageType>::
170 GenerateData() 
171 {
172   //---------------------------------
173   // Build kernel
174   //---------------------------------
175   typedef itk::BinaryBallStructuringElement<PixelType,ImageDimension> KernelType;
176   KernelType structuringElement;
177   if (GetVerboseFlag()) {
178     std::cout << "Radius in pixel : " << m_Radius << std::endl;
179   }
180   structuringElement.SetRadius(m_Radius);
181   structuringElement.CreateStructuringElement();
182
183   //---------------------------------
184   // Switch according to operation type
185   //---------------------------------
186   typedef itk::ImageToImageFilter<InternalImageType, InternalImageType> ImageFilterType;
187   typename ImageFilterType::Pointer filter; 
188   switch(m_OperationType)
189     {
190     case Erode: {
191       typedef itk::BinaryErodeImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
192       typename FilterType::Pointer m = FilterType::New();
193       m->SetBackgroundValue(this->GetBackgroundValue());
194       m->SetForegroundValue(this->GetForegroundValue());
195       m->SetBoundaryToForeground(GetBoundaryToForegroundFlag());
196       m->SetKernel(structuringElement);
197       
198       filter=m;
199       if (GetVerboseFlag()) std::cout<<"Using the erode filter..."<<std::endl;
200       break;
201     }
202
203     case Dilate:
204       {
205         typedef itk::BinaryDilateImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
206         typename FilterType::Pointer m = FilterType::New();
207         m->SetBackgroundValue(this->GetBackgroundValue());
208         m->SetForegroundValue(this->GetForegroundValue());
209         m->SetBoundaryToForeground(GetBoundaryToForegroundFlag());
210         m->SetKernel(structuringElement);
211
212         filter=m;
213         if (GetVerboseFlag()) std::cout<<"Using the dilate filter..."<<std::endl;
214         break;
215       }
216
217     case Close:
218       {
219         typedef itk::BinaryMorphologicalClosingImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
220         typename FilterType::Pointer m = FilterType::New();
221         m->SetForegroundValue(this->GetForegroundValue());
222         m->SetSafeBorder(GetBoundaryToForegroundFlag());
223         m->SetKernel(structuringElement);
224
225         filter=m;
226         if (GetVerboseFlag()) std::cout<<"Using the closing filter..."<<std::endl;
227         break;
228       }
229
230     case Open:
231       {
232         typedef itk::BinaryMorphologicalOpeningImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
233         typename FilterType::Pointer m = FilterType::New();
234         m->SetBackgroundValue(this->GetBackgroundValue());
235         m->SetForegroundValue(this->GetForegroundValue());
236         m->SetKernel(structuringElement);
237
238         filter=m;
239         if (GetVerboseFlag()) std::cout<<"Using the opening filter..."<<std::endl;
240         break;
241       }
242
243     case CondErode:
244       {
245         typedef clitk::ConditionalBinaryErodeImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
246         typename FilterType::Pointer m = FilterType::New();
247         m->SetBackgroundValue(this->GetBackgroundValue());
248         m->SetForegroundValue(this->GetForegroundValue());
249         m->SetBoundaryToForeground(GetBoundaryToForegroundFlag());
250         m->SetKernel(structuringElement);
251           
252         filter=m;
253         if (GetVerboseFlag()) std::cout<<"Using the conditional erode filter..."<<std::endl;
254         break;
255       }
256
257     case CondDilate:
258       {
259         typedef clitk::ConditionalBinaryDilateImageFilter<InternalImageType, InternalImageType , KernelType> FilterType;
260         typename FilterType::Pointer m = FilterType::New();
261         m->SetBackgroundValue(this->GetBackgroundValue());
262         m->SetForegroundValue(this->GetForegroundValue());
263         m->SetBoundaryToForeground(GetBoundaryToForegroundFlag());
264         m->SetKernel(structuringElement);
265           
266         filter=m;
267         if (GetVerboseFlag()) std::cout<<"Using the conditional dilate filter..."<<std::endl;
268         break;
269       }
270
271     }
272   
273
274   //---------------------------------
275   // Execute the filter
276   //---------------------------------
277   filter->SetInput(input);
278   filter->Update();
279
280   //---------------------------------
281   // Write the output
282   //---------------------------------
283   typedef itk::CastImageFilter< InternalImageType, ImageType > OutputCastImageFilterType;
284   typename OutputCastImageFilterType::Pointer oCaster = OutputCastImageFilterType::New();
285   oCaster->SetInput(filter->GetOutput());
286   oCaster->Update();
287
288   this->SetNthOutput(0, oCaster->GetOutput());
289   //this->GraftOutput(oCaster->GetOutput()); // NO
290 }
291 //--------------------------------------------------------------------
292
293