]> Creatis software - clitk.git/blob - itk/clitkAutoCropFilter.txx
145cc36f8ae24434cd47d72d1414e398a1c3fd64
[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 #include "itkExtractImageFilter.h"
33
34 namespace clitk {
35
36   //--------------------------------------------------------------------
37   template <class ImageType>
38   AutoCropFilter<ImageType>::
39   AutoCropFilter():itk::ImageToImageFilter<ImageType, ImageType>() {
40     this->SetNumberOfRequiredInputs(1);
41     m_BackgroundValue  = 0;
42   }
43   //--------------------------------------------------------------------
44
45
46   //--------------------------------------------------------------------
47   template <class ImageType>
48   void 
49   AutoCropFilter<ImageType>::
50   SetInput(const ImageType * image) {
51     // Process object is not const-correct so the const casting is required.
52     this->SetNthInput(0, const_cast<ImageType *>( image ));
53   }
54   //--------------------------------------------------------------------
55   
56
57   //--------------------------------------------------------------------
58   template <class ImageType>
59   void 
60   AutoCropFilter<ImageType>::  
61   SetBackgroundValue(ImagePixelType p) {
62     m_BackgroundValue = p;
63   }
64   //--------------------------------------------------------------------
65
66
67   //--------------------------------------------------------------------
68   template <class ImageType>
69   void 
70   AutoCropFilter<ImageType>::
71   GenerateOutputInformation() {    
72
73     // Superclass
74     // do not call the superclass' implementation of this method since
75     // this filter allows the input the output to be of different dimensions
76     // Superclass::GenerateOutputInformation();
77
78     // Get input pointers
79     ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
80     
81     // Get output pointer
82     ImagePointer output = this->GetOutput(0);
83   
84     // Convert to LabelMap
85     static const unsigned int Dim = ImageType::ImageDimension;
86     //    typedef unsigned long LabelType; // unsigned long needed (!!??)
87     typedef itk::StatisticsLabelObject< LabelType, Dim > LabelObjectType;
88     typedef itk::LabelMap< LabelObjectType > LabelMapType;
89     typedef itk::LabelImageToLabelMapFilter<ImageType, LabelMapType> ImageToMapFilterType;
90     typename ImageToMapFilterType::Pointer imageToLabelFilter = ImageToMapFilterType::New();  
91     imageToLabelFilter->SetBackgroundValue(m_BackgroundValue);
92     imageToLabelFilter->SetInput(input);
93     
94     // AutoCrop
95     typedef itk::AutoCropLabelMapFilter<LabelMapType> AutoCropFilterType;
96     typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
97     autoCropFilter->SetInput(imageToLabelFilter->GetOutput());
98     autoCropFilter->ReleaseDataFlagOff(); 
99     
100     // Convert to LabelImage
101     typedef itk::LabelMapToLabelImageFilter<LabelMapType, ImageType> MapToImageFilterType;
102     typename MapToImageFilterType::Pointer labelToImageFilter = MapToImageFilterType::New();       
103     labelToImageFilter->SetInput(autoCropFilter->GetOutput());
104
105     // Go ! (needed)
106     labelToImageFilter->Update();
107     m_labeImage = labelToImageFilter->GetOutput();
108
109     // Update the output size
110     m_Region = m_labeImage->GetLargestPossibleRegion();
111     // Sometimes the index is 9223372036854775807 ???
112     if (m_Region.GetIndex()[0] > 99999) {
113       std::cerr << "Warning !! BUG int clitkAutoCropFilter ?" << std::endl;
114       typename ImageType::IndexType index; 
115       index.Fill(0);
116       m_Region.SetIndex(index);
117       DD(m_Region);
118     }
119     output->SetLargestPossibleRegion(m_Region);
120     output->SetRequestedRegion(m_Region);
121     output->SetBufferedRegion(m_Region);
122     output->SetRegions(m_Region);
123   }
124   //--------------------------------------------------------------------
125    
126   //--------------------------------------------------------------------
127   template <class ImageType>
128   void 
129   AutoCropFilter<ImageType>::
130   GenerateData() {
131     // Get input pointers
132     ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
133   
134     // Extract the region with RegionOfInterestImageFilter or ExtractImageFilter ? 
135     // The second is when reducing the nb of dimension (index always zero)
136     // The first keep index
137
138     typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> CropFilterType;
139     //typedef itk::ExtractImageFilter<ImageType, ImageType> CropFilterType;
140     m_labeImage->SetRequestedRegion(m_labeImage->GetLargestPossibleRegion());
141     typename CropFilterType::Pointer cropFilter = CropFilterType::New();
142     cropFilter->SetInput(m_labeImage);
143     cropFilter->SetReleaseDataFlag(this->GetReleaseDataFlag());
144
145     cropFilter->SetRegionOfInterest(m_Region);
146     //cropFilter->SetExtractionRegion(m_Region);
147
148     // Go ! 
149     cropFilter->Update();
150
151     // Get (graft) output (SetNthOutput does not fit here because of Origin).
152     this->GraftOutput(cropFilter->GetOutput());
153   }
154   //--------------------------------------------------------------------
155    
156 }//end clitk
157  
158 #endif //#define CLITKAUTOCROPFILTER