]> Creatis software - clitk.git/blob - itk/clitkAutoCropFilter.txx
add seg folder
[clitk.git] / itk / clitkAutoCropFilter.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
19 #ifndef CLITKAUTOCROPFILTER_TXX
20 #define CLITKAUTOCROPFILTER_TXX
21
22 // clitk
23 #include "clitkCommon.h"
24 #include "clitkSegmentationUtils.h"
25
26 // itk
27 #include "itkAutoCropLabelMapFilter.h"
28 #include "itkStatisticsLabelObject.h"
29 #include "itkLabelImageToLabelMapFilter.h"
30 #include "itkLabelMapToLabelImageFilter.h"
31 #include "itkRegionOfInterestImageFilter.h"
32
33 namespace clitk {
34
35   //--------------------------------------------------------------------
36   template <class TImageType>
37   AutoCropFilter<TImageType>::
38   AutoCropFilter():itk::ImageToImageFilter<TImageType, TImageType>() {
39     this->SetNumberOfRequiredInputs(1);
40     m_BackgroundValue  = 0;
41   }
42   //--------------------------------------------------------------------
43
44
45   //--------------------------------------------------------------------
46   template <class TImageType>
47   void 
48   AutoCropFilter<TImageType>::
49   SetInput(const TImageType * image) {
50     // Process object is not const-correct so the const casting is required.
51     this->SetNthInput(0, const_cast<TImageType *>( image ));
52   }
53   //--------------------------------------------------------------------
54   
55
56   //--------------------------------------------------------------------
57   template <class TImageType>
58   void 
59   AutoCropFilter<TImageType>::  
60   SetBackgroundValue(ImagePixelType p) {
61     m_BackgroundValue = p;
62   }
63   //--------------------------------------------------------------------
64
65
66   //--------------------------------------------------------------------
67   template <class TImageType>
68   void 
69   AutoCropFilter<TImageType>::
70   GenerateOutputInformation() {    
71
72     // Superclass
73     // do not call the superclass' implementation of this method since
74     // this filter allows the input the output to be of different dimensions
75     // Superclass::GenerateOutputInformation();
76
77     // Get input pointers
78     ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
79     
80     // Get output pointer
81     ImagePointer output = this->GetOutput(0);
82   
83     // Convert to LabelMap
84     static const unsigned int Dim = ImageType::ImageDimension;
85     //    typedef unsigned long LabelType; // unsigned long needed (!!??)
86     typedef itk::StatisticsLabelObject< LabelType, Dim > LabelObjectType;
87     typedef itk::LabelMap< LabelObjectType > LabelMapType;
88     typedef itk::LabelImageToLabelMapFilter<ImageType, LabelMapType> ImageToMapFilterType;
89     typename ImageToMapFilterType::Pointer imageToLabelFilter = ImageToMapFilterType::New();  
90     imageToLabelFilter->SetBackgroundValue(m_BackgroundValue);
91     imageToLabelFilter->SetInput(input);
92     
93     // AutoCrop
94     typedef itk::AutoCropLabelMapFilter<LabelMapType> AutoCropFilterType;
95     typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
96     autoCropFilter->SetInput(imageToLabelFilter->GetOutput());
97
98     // Convert to LabelImage
99     typedef itk::LabelMapToLabelImageFilter<LabelMapType, ImageType> MapToImageFilterType;
100     typename MapToImageFilterType::Pointer labelToImageFilter = MapToImageFilterType::New();       
101     labelToImageFilter->SetInput(autoCropFilter->GetOutput());
102
103     // Go ! (needed)
104     labelToImageFilter->Update();
105     m_labeImage = labelToImageFilter->GetOutput();
106
107     // Update the output size
108     m_Region = m_labeImage->GetLargestPossibleRegion();
109     output->SetLargestPossibleRegion(m_Region);
110     output->SetRequestedRegion(m_Region);
111     output->SetBufferedRegion(m_Region);
112     output->SetRegions(m_Region);
113   }
114   //--------------------------------------------------------------------
115    
116   //--------------------------------------------------------------------
117   template <class TImageType>
118   void 
119   AutoCropFilter<TImageType>::
120   GenerateData() {
121     // Get input pointers
122     ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
123   
124     // Extract the region
125     typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> CropFilterType;
126     m_labeImage->SetRequestedRegion(m_labeImage->GetLargestPossibleRegion());
127     typename CropFilterType::Pointer cropFilter = CropFilterType::New();
128     cropFilter->SetInput(m_labeImage);
129     cropFilter->SetRegionOfInterest(m_Region);
130
131     // Go ! 
132     cropFilter->Update();
133
134     // Get (graft) output (SetNthOutput does not fit here because of Origin).
135     this->GraftOutput(cropFilter->GetOutput());
136   }
137   //--------------------------------------------------------------------
138    
139 }//end clitk
140  
141 #endif //#define CLITKAUTOCROPFILTER