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);
94 typedef itk::AutoCropLabelMapFilter<LabelMapType> AutoCropFilterType;
95 typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
96 autoCropFilter->SetInput(imageToLabelFilter->GetOutput());
98 // Convert to LabelImage
99 typedef itk::LabelMapToLabelImageFilter<LabelMapType, ImageType> MapToImageFilterType;
100 typename MapToImageFilterType::Pointer labelToImageFilter = MapToImageFilterType::New();
101 labelToImageFilter->SetInput(autoCropFilter->GetOutput());
104 labelToImageFilter->Update();
105 m_labeImage = labelToImageFilter->GetOutput();
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);
114 //--------------------------------------------------------------------
116 //--------------------------------------------------------------------
117 template <class ImageType>
119 AutoCropFilter<ImageType>::
121 DD("AutoCropFilter::GenerateData");
122 // Get input pointers
123 ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
125 // Extract the region
126 typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> CropFilterType;
127 m_labeImage->SetRequestedRegion(m_labeImage->GetLargestPossibleRegion());
128 typename CropFilterType::Pointer cropFilter = CropFilterType::New();
129 cropFilter->SetInput(m_labeImage);
130 cropFilter->SetRegionOfInterest(m_Region);
133 cropFilter->Update();
135 // Get (graft) output (SetNthOutput does not fit here because of Origin).
136 this->GraftOutput(cropFilter->GetOutput());
138 //--------------------------------------------------------------------
142 #endif //#define CLITKAUTOCROPFILTER