1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
19 #ifndef CLITKAUTOCROPFILTER_TXX
20 #define CLITKAUTOCROPFILTER_TXX
23 #include "clitkCommon.h"
26 #include "itkAutoCropLabelMapFilter.h"
27 #include "itkStatisticsLabelObject.h"
28 #include "itkLabelImageToLabelMapFilter.h"
29 #include "itkLabelMapToLabelImageFilter.h"
30 #include "itkRegionOfInterestImageFilter.h"
31 #include "itkExtractImageFilter.h"
35 //--------------------------------------------------------------------
36 template <class ImageType>
37 AutoCropFilter<ImageType>::
38 AutoCropFilter():itk::ImageToImageFilter<ImageType, ImageType>() {
39 this->SetNumberOfRequiredInputs(1);
40 m_BackgroundValue = 0;
43 //--------------------------------------------------------------------
46 //--------------------------------------------------------------------
47 template <class ImageType>
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 ));
54 //--------------------------------------------------------------------
57 //--------------------------------------------------------------------
58 template <class ImageType>
60 AutoCropFilter<ImageType>::
61 SetBackgroundValue(ImagePixelType p) {
62 m_BackgroundValue = p;
64 //--------------------------------------------------------------------
67 //--------------------------------------------------------------------
68 template <class ImageType>
70 AutoCropFilter<ImageType>::
71 GenerateOutputInformation() {
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();
79 ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
82 ImagePointer output = this->GetOutput(0);
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);
95 typedef itk::AutoCropLabelMapFilter<LabelMapType> AutoCropFilterType;
96 typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
97 autoCropFilter->SetInput(imageToLabelFilter->GetOutput());
98 // autoCropFilter->ReleaseDataFlagOff();
100 DD("Crop UseBorder : not correctly implemented do not use (use PadLabelMapFilter)");
101 // http://www.itk.org/Doxygen/html/classitk_1_1AutoCropLabelMapFilter.html#a54f49fdff8d9f2d2313134109d510285
103 typename ImageType::SizeType s;
104 for(uint i=0; i<ImageType::ImageDimension; i++) s[i] = 1;
105 autoCropFilter->SetCropBorder(s);
107 autoCropFilter->ReleaseDataFlagOn();
109 // Convert to LabelImage
110 typedef itk::LabelMapToLabelImageFilter<LabelMapType, ImageType> MapToImageFilterType;
111 typename MapToImageFilterType::Pointer labelToImageFilter = MapToImageFilterType::New();
112 labelToImageFilter->SetInput(autoCropFilter->GetOutput());
115 labelToImageFilter->Update();
116 m_labeImage = labelToImageFilter->GetOutput();
118 // Update the output size
119 m_Region = m_labeImage->GetLargestPossibleRegion();
120 // Sometimes the index is 9223372036854775807 ???
121 if (m_Region.GetIndex()[0] > 99999) {
122 std::cerr << "Warning !! BUG int clitkAutoCropFilter ?" << std::endl;
123 typename ImageType::IndexType index;
125 m_Region.SetIndex(index);
128 // Set the region to output
129 output->SetLargestPossibleRegion(m_Region);
130 output->SetRequestedRegion(m_Region);
131 output->SetBufferedRegion(m_Region);
132 output->SetRegions(m_Region);
134 //--------------------------------------------------------------------
136 //--------------------------------------------------------------------
137 template <class ImageType>
139 AutoCropFilter<ImageType>::
141 // Get input pointers
142 ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
144 // Extract the region with RegionOfInterestImageFilter or ExtractImageFilter ?
145 // The second is when reducing the nb of dimension (index always zero)
146 // The first keep index.
147 // OLD : typedef itk::ExtractImageFilter<ImageType, ImageType> CropFilterType;
148 // OLD : cropFilter->SetExtractionRegion(m_Region);
150 typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> CropFilterType;
151 m_labeImage->SetRequestedRegion(m_labeImage->GetLargestPossibleRegion());
152 typename CropFilterType::Pointer cropFilter = CropFilterType::New();
153 cropFilter->SetInput(m_labeImage);
154 cropFilter->SetReleaseDataFlag(this->GetReleaseDataFlag());
155 cropFilter->SetRegionOfInterest(m_Region);
158 cropFilter->Update();
160 // Get (graft) output (SetNthOutput does not fit here because of Origin).
161 this->GraftOutput(cropFilter->GetOutput());
163 //--------------------------------------------------------------------
167 #endif //#define CLITKAUTOCROPFILTER