]> Creatis software - clitk.git/blob - tools/clitkImageLaplacianGenericFilter.txx
Be sure to have the correct origin in clitkImage2DicomDose output
[clitk.git] / tools / clitkImageLaplacianGenericFilter.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://www.centreleonberard.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 #ifndef clitkImageLaplacianGenericFilter_txx
19 #define clitkImageLaplacianGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkImageLaplacianGenericFilter.txx
23  * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
24  * @date   29 june 2009
25  *
26  * @brief
27  *
28  ===================================================*/
29
30 // itk include
31 #include "itkCastImageFilter.h"
32 #include "itkLaplacianImageFilter.h"
33 #include "itkLaplacianRecursiveGaussianImageFilter.h"
34 #include "itkLabelStatisticsImageFilter.h"
35 #include "itkMaskImageFilter.h"
36 #include "itkMaskNegatedImageFilter.h"
37 #include <clitkCommon.h>
38
39 namespace clitk
40 {
41
42 //--------------------------------------------------------------------
43 template<class args_info_type>
44 ImageLaplacianGenericFilter<args_info_type>::ImageLaplacianGenericFilter():
45   ImageToImageGenericFilter<Self>("ImageLaplacian")
46 {
47   InitializeImageType<2>();
48   InitializeImageType<3>();
49 }
50 //--------------------------------------------------------------------
51
52
53 //--------------------------------------------------------------------
54 template<class args_info_type>
55 template<unsigned int Dim>
56 void ImageLaplacianGenericFilter<args_info_type>::InitializeImageType()
57 {
58   ADD_DEFAULT_IMAGE_TYPES(Dim);
59 }
60 //--------------------------------------------------------------------
61
62
63 //--------------------------------------------------------------------
64 template<class args_info_type>
65 void ImageLaplacianGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a)
66 {
67   mArgsInfo=a;
68   this->SetIOVerbose(mArgsInfo.verbose_flag);
69   if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
70
71   if (mArgsInfo.input_given) {
72     this->SetInputFilename(mArgsInfo.input_arg);
73   }
74   if (mArgsInfo.output_given) {
75     this->SetOutputFilename(mArgsInfo.output_arg);
76   }
77   if (mArgsInfo.mask_given) {
78     this->AddInputFilename(mArgsInfo.mask_arg);
79   }
80 }
81 //--------------------------------------------------------------------
82
83 //--------------------------------------------------------------------
84 // Update with the number of dimensions and the pixeltype
85 //--------------------------------------------------------------------
86 template<class args_info_type>
87 template<class InputImageType>
88 void
89 ImageLaplacianGenericFilter<args_info_type>::UpdateWithInputImageType()
90 {
91     // Main filter
92     typedef typename InputImageType::PixelType InputPixelType;
93     typedef itk::Image<float, InputImageType::ImageDimension> FloatImageType;
94     typedef itk::Image<unsigned char, FloatImageType::ImageDimension> MaskImageType;
95
96     // Reading input
97     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
98
99     //Cast input to float
100     typedef itk::CastImageFilter< InputImageType, FloatImageType > CastFilterType;
101     typename CastFilterType::Pointer castFilter = CastFilterType::New();
102     castFilter->SetInput(input);
103     castFilter->Update();
104
105     typename MaskImageType::Pointer mask = NULL;
106     if(mArgsInfo.mask_given) {
107         mask = this->template GetInput<MaskImageType>(1);
108     }
109     else {
110         mask = MaskImageType::New();
111         mask->SetRegions(input->GetLargestPossibleRegion());
112         mask->SetOrigin(input->GetOrigin());
113         mask->SetSpacing(input->GetSpacing());
114         mask->Allocate();
115         mask->FillBuffer(1);
116     }
117
118
119     // Create output image
120     typename FloatImageType::Pointer outputImage = FloatImageType::New();
121     outputImage->SetRegions(input->GetLargestPossibleRegion());
122     outputImage->SetOrigin(input->GetOrigin());
123     outputImage->SetSpacing(input->GetSpacing());
124     outputImage->Allocate();
125     outputImage->FillBuffer(0.0);
126     // Set output iterator
127     typedef itk::ImageRegionIterator<FloatImageType> IteratorOutputType;
128     IteratorOutputType ito = IteratorOutputType(outputImage, outputImage->GetLargestPossibleRegion());
129
130     // Filter
131     typename FloatImageType::Pointer outputLaplacianFilter;
132     if (mArgsInfo.gaussian_filter_flag == 0) {
133         //std::cout<<"gaussian filter flag == 0"<<std::endl;
134         typedef itk::LaplacianImageFilter<FloatImageType, FloatImageType> LaplacianImageFilterType;
135         typename LaplacianImageFilterType::Pointer laplacianFilter=LaplacianImageFilterType::New();
136         laplacianFilter->SetInput(castFilter->GetOutput());
137         laplacianFilter->Update();
138         outputLaplacianFilter = laplacianFilter->GetOutput();
139     }
140     else {
141         //std::cout<<"gaussian filter flag == 1"<<std::endl;
142         typedef itk::LaplacianRecursiveGaussianImageFilter< FloatImageType, FloatImageType >  LaplacianImageFilterType;
143         typename LaplacianImageFilterType::Pointer laplacianFilter=LaplacianImageFilterType::New();
144         laplacianFilter->SetInput(castFilter->GetOutput());
145         laplacianFilter->Update();
146         //std::cout<<"sigma value="<<laplacianFilter->GetSigma()<<std::endl;
147         outputLaplacianFilter = laplacianFilter->GetOutput();
148     }
149     // Set iterator
150     typedef itk::ImageRegionIterator<FloatImageType> IteratorType;
151     IteratorType it(outputLaplacianFilter, outputLaplacianFilter->GetLargestPossibleRegion());
152
153     // Set mask iterator
154     typedef itk::ImageRegionIterator<MaskImageType> IteratorMaskType;
155     IteratorMaskType itm(mask, mask->GetLargestPossibleRegion());
156
157     //typedef itk::MinimumMaximumImageCalculator <OutputImageType> ImageCalculatorFilterType;
158     //typename ImageCalculatorFilterType::Pointer imageCalculatorFilter = ImageCalculatorFilterType::New();
159     //imageCalculatorFilter->SetImage(gradientFilter->GetOutput());
160     //imageCalculatorFilter->Compute();
161     typedef itk::LabelStatisticsImageFilter< FloatImageType, MaskImageType > LabelStatisticsImageFilterType;
162     typename LabelStatisticsImageFilterType::Pointer labelStatisticsImageFilter = LabelStatisticsImageFilterType::New();
163     labelStatisticsImageFilter->SetLabelInput( mask );
164     labelStatisticsImageFilter->SetInput(outputLaplacianFilter);
165     labelStatisticsImageFilter->Update();
166
167     //std::cout << "Number of labels: " << labelStatisticsImageFilter->GetNumberOfLabels() << std::endl;
168
169     float minImg = labelStatisticsImageFilter->GetMinimum(1);
170     //std::cout << "minImg: " << minImg << std::endl;
171     float maxImg = labelStatisticsImageFilter->GetMaximum(1);
172     //std::cout << "maxImg: " << maxImg << std::endl;
173
174     it.GoToBegin();
175     ito.GoToBegin();
176     itm.GoToBegin();
177
178     while (!ito.IsAtEnd()) {
179         if(mArgsInfo.normalize_flag && itm.Get() == 1) {
180             ito.Set(((float) it.Get() - minImg)/(maxImg-minImg));
181         }
182         if (mArgsInfo.normalize_flag == 0 && itm.Get() == 1) {
183             ito.Set((float) it.Get());
184         }
185         ++it;
186         ++ito;
187         ++itm;
188     }
189
190     //typename OutputImageType::Pointer outputImage = gradientFilter->GetOutput();
191     this->template SetNextOutput<FloatImageType>(outputImage);
192 }
193 //--------------------------------------------------------------------
194
195
196 }//end clitk
197
198 #endif //#define clitkImageLaplacianGenericFilter_txx