1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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
21 /* =================================================
22 * @file clitkImageStatisticsGenericFilter.txx
28 ===================================================*/
34 //-------------------------------------------------------------------
35 // Update with the number of dimensions
36 //-------------------------------------------------------------------
37 template<unsigned int Dimension>
39 ImageStatisticsGenericFilter::UpdateWithDim(std::string PixelType)
41 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
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>();
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>();
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>();
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>();
62 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
63 UpdateWithDimAndPixelType<Dimension, float>();
68 //-------------------------------------------------------------------
69 // Update with the number of dimensions and the pixeltype
70 //-------------------------------------------------------------------
71 template <unsigned int Dimension, class PixelType>
73 ImageStatisticsGenericFilter::UpdateWithDimAndPixelType()
77 typedef itk::Image<PixelType, Dimension> InputImageType;
78 typedef itk::Image<unsigned int, Dimension> LabelImageType;
79 typedef itk::Image<PixelType, Dimension> OutputImageType;
82 typedef itk::ImageFileReader<InputImageType> InputReaderType;
83 typename InputReaderType::Pointer reader = InputReaderType::New();
84 reader->SetFileName( m_InputFileName);
86 typename InputImageType::Pointer input= reader->GetOutput();
89 typedef itk::LabelStatisticsImageFilter<InputImageType, LabelImageType> StatisticsImageFilterType;
90 typename StatisticsImageFilterType::Pointer statisticsFilter=StatisticsImageFilterType::New();
91 statisticsFilter->SetInput(input);
94 typename LabelImageType::Pointer labelImage;
95 if (m_ArgsInfo.mask_given)
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();
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]);
112 statisticsFilter->SetLabelInput(labelImage);
115 typename LabelImageType::PixelType label;
116 unsigned int numberOfLabels;
117 if (m_ArgsInfo.label_given)
118 numberOfLabels=m_ArgsInfo.label_given;
122 for (unsigned int k=0; k< numberOfLabels; k++)
124 label=m_ArgsInfo.label_arg[k];
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;
132 if (m_ArgsInfo.histogram_given)
134 statisticsFilter->SetUseHistograms(true);
135 statisticsFilter->SetHistogramParameters(m_ArgsInfo.bins_arg, m_ArgsInfo.lower_arg, m_ArgsInfo.upper_arg);
137 statisticsFilter->Update();
141 if (m_Verbose) std::cout<<"N° of pixels: ";
142 std::cout<<statisticsFilter->GetCount(label)<<std::endl;
144 if (m_Verbose) std::cout<<"Mean: ";
145 std::cout<<statisticsFilter->GetMean(label)<<std::endl;
147 if (m_Verbose) std::cout<<"SD: ";
148 std::cout<<statisticsFilter->GetSigma(label)<<std::endl;
150 if (m_Verbose) std::cout<<"Variance: ";
151 std::cout<<statisticsFilter->GetVariance(label)<<std::endl;
153 if (m_Verbose) std::cout<<"Min: ";
154 std::cout<<statisticsFilter->GetMinimum(label)<<std::endl;
156 if (m_Verbose) std::cout<<"Max: ";
157 std::cout<<statisticsFilter->GetMaximum(label)<<std::endl;
159 if (m_Verbose) std::cout<<"Sum: ";
160 std::cout<<statisticsFilter->GetSum(label)<<std::endl;
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;
169 if (m_ArgsInfo.histogram_given)
171 if (m_Verbose) std::cout<<"Median: ";
172 std::cout<<statisticsFilter->GetMedian(label)<<std::endl;
174 typename StatisticsImageFilterType::HistogramPointer histogram =statisticsFilter->GetHistogram(label);
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;
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;
198 #endif //#define clitkImageStatisticsGenericFilter_txx