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