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 #include "itkNthElementImageAdaptor.h"
22 #include "itkJoinSeriesImageFilter.h"
24 /* =================================================
25 * @file clitkImageStatisticsGenericFilter.txx
31 ===================================================*/
32 #include "clitkImageStatisticsGenericFilter.h"
38 //-------------------------------------------------------------------
39 // Update with the number of dimensions
40 //-------------------------------------------------------------------
41 template<unsigned int Dimension, unsigned int Components>
43 ImageStatisticsGenericFilter::UpdateWithDim(std::string PixelType)
45 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<" with " << Components << " channel(s)..."<<std::endl;
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>();
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>();
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>();
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>();
66 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
67 UpdateWithDimAndPixelType<Dimension, float, Components>();
72 //-------------------------------------------------------------------
73 // Update with the number of dimensions and the pixeltype
74 //-------------------------------------------------------------------
75 template <unsigned int Dimension, class PixelType, unsigned int Components>
77 ImageStatisticsGenericFilter::UpdateWithDimAndPixelType()
81 typedef unsigned char LabelPixelType;
82 typedef itk::Image<itk::Vector<PixelType, Components>, Dimension> InputImageType;
83 typedef itk::Image<LabelPixelType, Dimension> LabelImageType;
86 typedef itk::ImageFileReader<InputImageType> InputReaderType;
87 typename InputReaderType::Pointer reader = InputReaderType::New();
88 reader->SetFileName( m_InputFileName);
90 typename InputImageType::Pointer input= reader->GetOutput();
92 typedef itk::NthElementImageAdaptor<InputImageType, PixelType> InputImageAdaptorType;
93 typedef itk::Image<PixelType, Dimension> OutputImageType;
95 typename InputImageAdaptorType::Pointer input_adaptor = InputImageAdaptorType::New();
96 input_adaptor->SetImage(input);
99 typedef itk::LabelStatisticsImageFilter<InputImageAdaptorType, LabelImageType> StatisticsImageFilterType;
100 typename StatisticsImageFilterType::Pointer statisticsFilter=StatisticsImageFilterType::New();
101 statisticsFilter->SetInput(input_adaptor);
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.
114 std::cout << "Replicating label image to match input image's dimension... " << std::endl;
116 typedef itk::Image<LabelPixelType, Dimension - 1> ReducedLabelImageType;
117 typedef itk::ImageFileReader<ReducedLabelImageType> LabelImageReaderType;
118 typedef itk::JoinSeriesImageFilter<ReducedLabelImageType, LabelImageType> JoinImageFilterType;
120 typename LabelImageReaderType::Pointer labelImageReader=LabelImageReaderType::New();
121 labelImageReader->SetFileName(m_ArgsInfo.mask_arg);
122 labelImageReader->Update();
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());
129 joinFilter->Update();
130 labelImage = joinFilter->GetOutput();
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();
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]);
149 statisticsFilter->SetLabelInput(labelImage);
152 typename LabelImageType::PixelType label;
153 unsigned int numberOfLabels;
154 if (m_ArgsInfo.label_given)
155 numberOfLabels=m_ArgsInfo.label_given;
159 unsigned int firstComponent = 0, lastComponent = 0;
160 if (m_ArgsInfo.channel_arg == -1) {
162 lastComponent = Components - 1;
165 firstComponent = m_ArgsInfo.channel_arg;
166 lastComponent = m_ArgsInfo.channel_arg;
169 for (unsigned int c=firstComponent; c<=lastComponent; c++) {
170 if (m_Verbose) std::cout << std::endl << "Processing channel " << c << std::endl;
172 input_adaptor->SelectNthElement(c);
173 input_adaptor->Update();
175 for (unsigned int k=0; k< numberOfLabels; k++) {
176 label=m_ArgsInfo.label_arg[k];
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;
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);
188 statisticsFilter->Update();
191 if (m_Verbose) std::cout<<"N° of pixels: ";
192 std::cout<<statisticsFilter->GetCount(label)<<std::endl;
194 if (m_Verbose) std::cout<<"Mean: ";
195 std::cout<<statisticsFilter->GetMean(label)<<std::endl;
197 if (m_Verbose) std::cout<<"SD: ";
198 std::cout<<statisticsFilter->GetSigma(label)<<std::endl;
200 if (m_Verbose) std::cout<<"Variance: ";
201 std::cout<<statisticsFilter->GetVariance(label)<<std::endl;
203 if (m_Verbose) std::cout<<"Min: ";
204 std::cout<<statisticsFilter->GetMinimum(label)<<std::endl;
206 if (m_Verbose) std::cout<<"Max: ";
207 std::cout<<statisticsFilter->GetMaximum(label)<<std::endl;
209 if (m_Verbose) std::cout<<"Sum: ";
210 std::cout<<statisticsFilter->GetSum(label)<<std::endl;
212 if (m_Verbose) std::cout<<"Bounding box: ";
214 for(unsigned int i =0; i <statisticsFilter->GetBoundingBox(label).size(); i++)
215 std::cout<<statisticsFilter->GetBoundingBox(label)[i]<<" ";
216 std::cout<<std::endl;
219 if (m_ArgsInfo.histogram_given)
221 if (m_Verbose) std::cout<<"Median: ";
222 std::cout<<statisticsFilter->GetMedian(label)<<std::endl;
224 typename StatisticsImageFilterType::HistogramPointer histogram =statisticsFilter->GetHistogram(label);
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;
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;
249 #endif //#define clitkImageStatisticsGenericFilter_txx