]> Creatis software - clitk.git/blob - tools/clitkImageStatisticsGenericFilter.txx
81f93be1105ff3b162aee89c57530e0f1895744b
[clitk.git] / tools / clitkImageStatisticsGenericFilter.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 clitkImageStatisticsGenericFilter_txx
19 #define clitkImageStatisticsGenericFilter_txx
20
21 #include "itkNthElementImageAdaptor.h"
22 #include "itkJoinSeriesImageFilter.h"
23
24 #include "clitkImageStatisticsGenericFilter.h"
25 #include "clitkCropLikeImageFilter.h"
26 #include "clitkResampleImageWithOptionsFilter.h"
27
28 namespace clitk
29 {
30
31   //-------------------------------------------------------------------
32   // Update with the number of dimensions
33   //-------------------------------------------------------------------
34   template<unsigned int Dimension, unsigned int Components>
35   void 
36   ImageStatisticsGenericFilter::UpdateWithDim(std::string PixelType)
37   {
38     if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<" with " << Components << " channel(s)..."<<std::endl;
39
40     if(PixelType == "short"){  
41       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
42       UpdateWithDimAndPixelType<Dimension, signed short, Components>(); 
43     }
44     //    else if(PixelType == "unsigned_short"){  
45     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
46     //       UpdateWithDimAndPixelType<Dimension, unsigned short>(); 
47     //     }
48     
49     else if (PixelType == "unsigned_char"){ 
50       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
51       UpdateWithDimAndPixelType<Dimension, unsigned char, Components>();
52     }
53     
54     //     else if (PixelType == "char"){ 
55     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
56     //       UpdateWithDimAndPixelType<Dimension, signed char>();
57     //     }
58     else {
59       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
60       UpdateWithDimAndPixelType<Dimension, float, Components>();
61     }
62   }
63
64
65   //-------------------------------------------------------------------
66   // Update with the number of dimensions and the pixeltype
67   //-------------------------------------------------------------------
68   template <unsigned int Dimension, class  PixelType, unsigned int Components> 
69   void 
70   ImageStatisticsGenericFilter::UpdateWithDimAndPixelType()
71   {
72
73     // ImageTypes
74     typedef unsigned char LabelPixelType;
75     typedef itk::Image<itk::Vector<PixelType, Components>, Dimension> InputImageType;
76     typedef itk::Image<LabelPixelType, Dimension> LabelImageType;
77     
78     // Read the input
79     typedef itk::ImageFileReader<InputImageType> InputReaderType;
80     typename InputReaderType::Pointer reader = InputReaderType::New();
81     reader->SetFileName( m_InputFileName);
82     reader->Update();
83     typename InputImageType::Pointer input= reader->GetOutput();
84     
85     typedef itk::NthElementImageAdaptor<InputImageType, PixelType> InputImageAdaptorType;
86     typedef itk::Image<PixelType, Dimension> OutputImageType;
87
88     typename InputImageAdaptorType::Pointer input_adaptor = InputImageAdaptorType::New();
89     input_adaptor->SetImage(input);
90     
91     // Filter
92     typedef itk::LabelStatisticsImageFilter<InputImageAdaptorType, LabelImageType> StatisticsImageFilterType;
93     typename StatisticsImageFilterType::Pointer statisticsFilter=StatisticsImageFilterType::New();
94     statisticsFilter->SetInput(input_adaptor);
95
96     // Label image
97     typename LabelImageType::Pointer labelImage;
98     if (m_ArgsInfo.mask_given) {
99       int maskDimension, maskComponents;
100       std::string maskPixelType;
101       ReadImageDimensionAndPixelType(m_ArgsInfo.mask_arg, maskDimension, maskPixelType, maskComponents);
102
103       if (maskDimension == Dimension - 1) {
104         // Due to a limitation of filter itk::LabelStatisticsImageFilter, InputImageType and LabelImageType
105         // must have the same image dimension. However, we want to support label images with Dl = Di - 1,
106         // so we need to replicate the label image as many times as the size along dimension Di.
107         if (m_Verbose) 
108           std::cout << "Replicating label image to match input image's dimension... " << std::endl;
109         
110         typedef itk::Image<LabelPixelType, Dimension - 1> ReducedLabelImageType;
111         typedef itk::ImageFileReader<ReducedLabelImageType> LabelImageReaderType;
112         typedef itk::JoinSeriesImageFilter<ReducedLabelImageType, LabelImageType> JoinImageFilterType;
113         
114         typename LabelImageReaderType::Pointer labelImageReader=LabelImageReaderType::New();
115         labelImageReader->SetFileName(m_ArgsInfo.mask_arg);
116         labelImageReader->Update();
117
118         typename JoinImageFilterType::Pointer joinFilter = JoinImageFilterType::New();
119         typename InputImageType::SizeType size = input->GetLargestPossibleRegion().GetSize();
120         for (unsigned int i = 0; i < size[Dimension - 1]; i++)
121           joinFilter->PushBackInput(labelImageReader->GetOutput());
122         
123         joinFilter->Update();
124         labelImage = joinFilter->GetOutput();
125       }
126       else {
127         typedef itk::ImageFileReader<LabelImageType> LabelImageReaderType;
128         typename LabelImageReaderType::Pointer labelImageReader=LabelImageReaderType::New();
129         labelImageReader->SetFileName(m_ArgsInfo.mask_arg);
130         labelImageReader->Update();
131         labelImage= labelImageReader->GetOutput();
132
133         // Check mask sampling/size
134         if (!HaveSameSizeAndSpacing<LabelImageType, InputImageType>(labelImage, input)) {
135           if (m_ArgsInfo.allow_resize_flag) {
136             if (m_ArgsInfo.verbose_flag) {
137               std::cout << "Resize mask image like input" << std::endl;
138             }
139             typedef clitk::ResampleImageWithOptionsFilter<LabelImageType> ResamplerType;
140             typename ResamplerType::Pointer resampler = ResamplerType::New();
141             resampler->SetInput(labelImage);
142             resampler->SetOutputSpacing(input->GetSpacing());
143             resampler->Update();
144             labelImage = resampler->GetOutput();
145
146             typename itk::ImageBase<LabelImageType::ImageDimension>::RegionType reg 
147               = input->GetLargestPossibleRegion();
148             labelImage = ResizeImageLike<LabelImageType>(labelImage, &reg, 0);
149           }
150           else {
151             std::cerr << "Mask image has a different size/spacing than input. Abort" << std::endl;
152             exit(-1);
153           }
154         }
155
156       }
157
158     }
159     else { 
160       labelImage=LabelImageType::New();
161       labelImage->SetRegions(input->GetLargestPossibleRegion());
162       labelImage->SetOrigin(input->GetOrigin());
163       labelImage->SetSpacing(input->GetSpacing());
164       labelImage->Allocate();
165       labelImage->FillBuffer(m_ArgsInfo.label_arg[0]);
166     }
167     statisticsFilter->SetLabelInput(labelImage);
168
169     // For each Label
170     typename LabelImageType::PixelType label;
171     unsigned int numberOfLabels;
172     if (m_ArgsInfo.label_given)
173       numberOfLabels=m_ArgsInfo.label_given;
174     else
175       numberOfLabels=1;
176
177     unsigned int firstComponent = 0, lastComponent = 0;
178     if (m_ArgsInfo.channel_arg == -1) {
179       firstComponent = 0; 
180       lastComponent = Components - 1;
181     }
182     else {
183       firstComponent = m_ArgsInfo.channel_arg;
184       lastComponent = m_ArgsInfo.channel_arg;
185     }
186     
187     for (unsigned int c=firstComponent; c<=lastComponent; c++) {
188       if (m_Verbose) std::cout << std::endl << "Processing channel " << c << std::endl;
189       
190       input_adaptor->SelectNthElement(c);
191       input_adaptor->Update();
192       
193       for (unsigned int k=0; k< numberOfLabels; k++) {
194         label=m_ArgsInfo.label_arg[k];
195
196         std::cout<<std::endl;
197         if (m_Verbose) std::cout<<"-------------"<<std::endl;
198         if (m_Verbose) std::cout<<"| Label: "<<label<<"  |"<<std::endl;
199         if (m_Verbose) std::cout<<"-------------"<<std::endl;
200
201         // Histograms
202         if (m_ArgsInfo.histogram_given) {
203           statisticsFilter->SetUseHistograms(true);
204           statisticsFilter->SetHistogramParameters(m_ArgsInfo.bins_arg, m_ArgsInfo.lower_arg, m_ArgsInfo.upper_arg);
205         }
206         statisticsFilter->Update();
207
208         // Output
209         if (m_Verbose) std::cout<<"N° of pixels: ";
210           std::cout<<statisticsFilter->GetCount(label)<<std::endl;
211
212         if (m_Verbose) std::cout<<"Mean: ";
213           std::cout<<statisticsFilter->GetMean(label)<<std::endl;
214
215         if (m_Verbose) std::cout<<"SD: ";
216           std::cout<<statisticsFilter->GetSigma(label)<<std::endl;
217
218         if (m_Verbose) std::cout<<"Variance: ";
219           std::cout<<statisticsFilter->GetVariance(label)<<std::endl;
220
221         if (m_Verbose) std::cout<<"Min: ";
222           std::cout<<statisticsFilter->GetMinimum(label)<<std::endl;
223
224         if (m_Verbose) std::cout<<"Max: ";
225           std::cout<<statisticsFilter->GetMaximum(label)<<std::endl;
226
227         if (m_Verbose) std::cout<<"Sum: ";
228           std::cout<<statisticsFilter->GetSum(label)<<std::endl;
229
230         if (m_Verbose) std::cout<<"Bounding box: ";
231         
232         for(unsigned int i =0; i <statisticsFilter->GetBoundingBox(label).size(); i++)
233           std::cout<<statisticsFilter->GetBoundingBox(label)[i]<<" ";
234         std::cout<<std::endl;
235
236         // Histogram
237         if (m_ArgsInfo.histogram_given)
238         {
239           if (m_Verbose) std::cout<<"Median: ";
240           std::cout<<statisticsFilter->GetMedian(label)<<std::endl;
241
242           typename StatisticsImageFilterType::HistogramPointer histogram =statisticsFilter->GetHistogram(label);
243
244           // Screen
245           if (m_Verbose) std::cout<<"Histogram: "<<std::endl;
246             std::cout<<"# MinBin\tMidBin\tMaxBin\tFrequency"<<std::endl;
247           for( int i =0; i <m_ArgsInfo.bins_arg; i++)
248             std::cout<<histogram->GetBinMin(0,i)<<"\t"<<histogram->GetMeasurement(i,0)<<"\t"<<histogram->GetBinMax(0,i)<<"\t"<<histogram->GetFrequency(i)<<std::endl;
249
250           // Add to the file
251           std::ofstream histogramFile(m_ArgsInfo.histogram_arg);
252           histogramFile<<"#Histogram: "<<std::endl;
253           histogramFile<<"#MinBin\tMidBin\tMaxBin\tFrequency"<<std::endl;
254           for( int i =0; i <m_ArgsInfo.bins_arg; i++)
255             histogramFile<<histogram->GetBinMin(0,i)<<"\t"<<histogram->GetMeasurement(i,0)<<"\t"<<histogram->GetBinMax(0,i)<<"\t"<<histogram->GetFrequency(i)<<std::endl;
256         }
257       }
258     }
259
260     return;
261
262   }
263   
264   
265 }//end clitk
266
267 #endif //#define clitkImageStatisticsGenericFilter_txx