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