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