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