]> Creatis software - clitk.git/blob - tools/clitkImageStatisticsGenericFilter.txx
Set mode of regular files to non-executable (644) instead of executable (755)
[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 /* =================================================
22  * @file   clitkImageStatisticsGenericFilter.txx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30
31 namespace clitk
32 {
33
34   //-------------------------------------------------------------------
35   // Update with the number of dimensions
36   //-------------------------------------------------------------------
37   template<unsigned int Dimension>
38   void 
39   ImageStatisticsGenericFilter::UpdateWithDim(std::string PixelType)
40   {
41     if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
42
43     if(PixelType == "short"){  
44       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
45       UpdateWithDimAndPixelType<Dimension, signed short>(); 
46     }
47     //    else if(PixelType == "unsigned_short"){  
48     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
49     //       UpdateWithDimAndPixelType<Dimension, unsigned short>(); 
50     //     }
51     
52     else if (PixelType == "unsigned_char"){ 
53       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
54       UpdateWithDimAndPixelType<Dimension, unsigned char>();
55     }
56     
57     //     else if (PixelType == "char"){ 
58     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
59     //       UpdateWithDimAndPixelType<Dimension, signed char>();
60     //     }
61     else {
62       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
63       UpdateWithDimAndPixelType<Dimension, float>();
64     }
65   }
66
67
68   //-------------------------------------------------------------------
69   // Update with the number of dimensions and the pixeltype
70   //-------------------------------------------------------------------
71   template <unsigned int Dimension, class  PixelType> 
72   void 
73   ImageStatisticsGenericFilter::UpdateWithDimAndPixelType()
74   {
75
76     // ImageTypes
77     typedef itk::Image<PixelType, Dimension> InputImageType;
78     typedef itk::Image<unsigned int, Dimension> LabelImageType;
79     typedef itk::Image<PixelType, Dimension> OutputImageType;
80     
81     // Read the input
82     typedef itk::ImageFileReader<InputImageType> InputReaderType;
83     typename InputReaderType::Pointer reader = InputReaderType::New();
84     reader->SetFileName( m_InputFileName);
85     reader->Update();
86     typename InputImageType::Pointer input= reader->GetOutput();
87
88     // Filter
89     typedef itk::LabelStatisticsImageFilter<InputImageType, LabelImageType> StatisticsImageFilterType;
90     typename StatisticsImageFilterType::Pointer statisticsFilter=StatisticsImageFilterType::New();
91     statisticsFilter->SetInput(input);
92
93     // Label image
94     typename LabelImageType::Pointer labelImage;
95     if (m_ArgsInfo.mask_given)
96       {
97         typedef itk::ImageFileReader<LabelImageType> LabelImageReaderType;
98         typename LabelImageReaderType::Pointer labelImageReader=LabelImageReaderType::New();
99         labelImageReader->SetFileName(m_ArgsInfo.mask_arg);
100         labelImageReader->Update();
101         labelImage= labelImageReader->GetOutput();
102       }
103     else
104       { 
105         labelImage=LabelImageType::New();
106         labelImage->SetRegions(input->GetLargestPossibleRegion());
107         labelImage->SetOrigin(input->GetOrigin());
108         labelImage->SetSpacing(input->GetSpacing());
109         labelImage->Allocate();
110         labelImage->FillBuffer(m_ArgsInfo.label_arg[0]);
111       }
112     statisticsFilter->SetLabelInput(labelImage);
113
114     // For each Label
115     typename LabelImageType::PixelType label;
116     unsigned int numberOfLabels;
117     if (m_ArgsInfo.label_given)
118       numberOfLabels=m_ArgsInfo.label_given;
119     else
120       numberOfLabels=1;
121
122     for (unsigned int k=0; k< numberOfLabels; k++)
123       {
124         label=m_ArgsInfo.label_arg[k];
125
126         std::cout<<std::endl;
127         if (m_Verbose) std::cout<<"-------------"<<std::endl;
128         if (m_Verbose) std::cout<<"| Label: "<<label<<"  |"<<std::endl;
129         if (m_Verbose) std::cout<<"-------------"<<std::endl;
130         
131         // Histograms
132         if (m_ArgsInfo.histogram_given)
133           {
134             statisticsFilter->SetUseHistograms(true);
135             statisticsFilter->SetHistogramParameters(m_ArgsInfo.bins_arg, m_ArgsInfo.lower_arg, m_ArgsInfo.upper_arg);
136           }
137         statisticsFilter->Update();
138
139
140         // Output
141         if (m_Verbose) std::cout<<"N° of pixels: ";
142         std::cout<<statisticsFilter->GetCount(label)<<std::endl;
143
144         if (m_Verbose) std::cout<<"Mean: ";
145         std::cout<<statisticsFilter->GetMean(label)<<std::endl;
146     
147         if (m_Verbose) std::cout<<"SD: ";
148         std::cout<<statisticsFilter->GetSigma(label)<<std::endl;
149
150         if (m_Verbose) std::cout<<"Variance: ";
151         std::cout<<statisticsFilter->GetVariance(label)<<std::endl;
152
153         if (m_Verbose) std::cout<<"Min: ";
154         std::cout<<statisticsFilter->GetMinimum(label)<<std::endl;
155
156         if (m_Verbose) std::cout<<"Max: ";
157         std::cout<<statisticsFilter->GetMaximum(label)<<std::endl;
158
159         if (m_Verbose) std::cout<<"Sum: ";
160         std::cout<<statisticsFilter->GetSum(label)<<std::endl;
161
162         if (m_Verbose) std::cout<<"Bounding box: ";
163         for(unsigned int i =0; i <statisticsFilter->GetBoundingBox(label).size(); i++)
164           std::cout<<statisticsFilter->GetBoundingBox(label)[i]<<" ";
165         std::cout<<std::endl;
166
167
168         // Histogram
169         if (m_ArgsInfo.histogram_given)
170           {
171             if (m_Verbose) std::cout<<"Median: ";
172             std::cout<<statisticsFilter->GetMedian(label)<<std::endl;
173
174             typename StatisticsImageFilterType::HistogramPointer histogram =statisticsFilter->GetHistogram(label);
175             
176             // Screen
177             if (m_Verbose) std::cout<<"Histogram: "<<std::endl;
178             std::cout<<"# MinBin\tMidBin\tMaxBin\tFrequency"<<std::endl;
179             for( int i =0; i <m_ArgsInfo.bins_arg; i++)
180               std::cout<<histogram->GetBinMin(0,i)<<"\t"<<histogram->GetMeasurement(i,0)<<"\t"<<histogram->GetBinMax(0,i)<<"\t"<<histogram->GetFrequency(i)<<std::endl;
181           
182             // Add to the file
183             std::ofstream histogramFile(m_ArgsInfo.histogram_arg);
184             histogramFile<<"#Histogram: "<<std::endl;
185             histogramFile<<"#MinBin\tMidBin\tMaxBin\tFrequency"<<std::endl;
186             for( int i =0; i <m_ArgsInfo.bins_arg; i++)
187               histogramFile<<histogram->GetBinMin(0,i)<<"\t"<<histogram->GetMeasurement(i,0)<<"\t"<<histogram->GetBinMax(0,i)<<"\t"<<histogram->GetFrequency(i)<<std::endl;
188           }
189       }
190     
191     return;
192     
193   }
194   
195   
196 }//end clitk
197
198 #endif //#define clitkImageStatisticsGenericFilter_txx