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