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://oncora1.lyon.fnclcc.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"
24 #include "clitkSegmentationUtils.h"
27 #include "itkAutoCropLabelMapFilter.h"
28 #include "itkStatisticsLabelObject.h"
29 #include "itkLabelImageToLabelMapFilter.h"
30 #include "itkLabelMapToLabelImageFilter.h"
31 #include "itkRegionOfInterestImageFilter.h"
35 //--------------------------------------------------------------------
36 template <class ImageType>
37 AutoCropFilter<ImageType>::
38 AutoCropFilter():itk::ImageToImageFilter<ImageType, ImageType>() {
39 this->SetNumberOfRequiredInputs(1);
40 m_BackgroundValue = 0;
42 //--------------------------------------------------------------------
45 //--------------------------------------------------------------------
46 template <class ImageType>
48 AutoCropFilter<ImageType>::
49 SetInput(const ImageType * image) {
50 // Process object is not const-correct so the const casting is required.
51 this->SetNthInput(0, const_cast<ImageType *>( image ));
53 //--------------------------------------------------------------------
56 //--------------------------------------------------------------------
57 template <class ImageType>
59 AutoCropFilter<ImageType>::
60 SetBackgroundValue(ImagePixelType p) {
61 m_BackgroundValue = p;
63 //--------------------------------------------------------------------
66 //--------------------------------------------------------------------
67 template <class ImageType>
69 AutoCropFilter<ImageType>::
70 GenerateOutputInformation() {
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();
78 ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
81 ImagePointer output = this->GetOutput(0);
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 DD(input->GetLargestPossibleRegion());
95 typedef itk::AutoCropLabelMapFilter<LabelMapType> AutoCropFilterType;
96 typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
97 autoCropFilter->SetInput(imageToLabelFilter->GetOutput());
99 // Convert to LabelImage
100 typedef itk::LabelMapToLabelImageFilter<LabelMapType, ImageType> MapToImageFilterType;
101 typename MapToImageFilterType::Pointer labelToImageFilter = MapToImageFilterType::New();
102 labelToImageFilter->SetInput(autoCropFilter->GetOutput());
105 labelToImageFilter->Update();
106 DD("CHECK AUTOCROP IF NB LABEL == 0 !!!");
107 m_labeImage = labelToImageFilter->GetOutput();
109 // Update the output size
110 m_Region = m_labeImage->GetLargestPossibleRegion();
112 // Sometimes the index is 9223372036854775807 ???
113 if (m_Region.GetIndex()[0] > 99999) {
114 typename ImageType::IndexType index;
116 m_Region.SetIndex(index);
118 output->SetLargestPossibleRegion(m_Region);
119 output->SetRequestedRegion(m_Region);
120 output->SetBufferedRegion(m_Region);
121 output->SetRegions(m_Region);
123 //--------------------------------------------------------------------
125 //--------------------------------------------------------------------
126 template <class ImageType>
128 AutoCropFilter<ImageType>::
130 // Get input pointers
131 ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
133 // Extract the region
134 typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> CropFilterType;
135 m_labeImage->SetRequestedRegion(m_labeImage->GetLargestPossibleRegion());
136 typename CropFilterType::Pointer cropFilter = CropFilterType::New();
137 cropFilter->SetInput(m_labeImage);
138 cropFilter->SetRegionOfInterest(m_Region);
141 cropFilter->Update();
143 // Get (graft) output (SetNthOutput does not fit here because of Origin).
144 this->GraftOutput(cropFilter->GetOutput());
146 //--------------------------------------------------------------------
150 #endif //#define CLITKAUTOCROPFILTER