Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkGIFFirstOrderStatistics.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 // MITK
20 #include <mitkITKImageImport.h>
21 #include <mitkImageCast.h>
22 #include <mitkImageAccessByItk.h>
23 
24 // ITK
25 #include <itkLabelStatisticsImageFilter.h>
26 #include <itkMinimumMaximumImageCalculator.h>
27 
28 // STL
29 #include <sstream>
30 
31 template<typename TPixel, unsigned int VImageDimension>
32 void
34 {
35  typedef itk::Image<TPixel, VImageDimension> ImageType;
36  typedef itk::Image<int, VImageDimension> MaskType;
37  typedef itk::LabelStatisticsImageFilter<ImageType, MaskType> FilterType;
38  typedef typename FilterType::HistogramType HistogramType;
39  typedef typename HistogramType::IndexType HIndexType;
40  typedef itk::MinimumMaximumImageCalculator<ImageType> MinMaxComputerType;
41 
42  typename MaskType::Pointer maskImage = MaskType::New();
43  mitk::CastToItkImage(mask, maskImage);
44 
45  typename MinMaxComputerType::Pointer minMaxComputer = MinMaxComputerType::New();
46  minMaxComputer->SetImage(itkImage);
47  minMaxComputer->Compute();
48  double imageRange = minMaxComputer->GetMaximum() - minMaxComputer->GetMinimum();
49 
50  typename FilterType::Pointer labelStatisticsImageFilter = FilterType::New();
51  labelStatisticsImageFilter->SetInput( itkImage );
52  labelStatisticsImageFilter->SetLabelInput(maskImage);
53  labelStatisticsImageFilter->SetUseHistograms(true);
54  if (params.m_UseCtRange)
55  {
56  labelStatisticsImageFilter->SetHistogramParameters(1024.5+3096.5, -1024.5,3096.5);
57  } else {
58  labelStatisticsImageFilter->SetHistogramParameters(params.m_HistogramSize, minMaxComputer->GetMinimum(),minMaxComputer->GetMaximum());
59  }
60  labelStatisticsImageFilter->Update();
61 
62  // --------------- Range --------------------
63  double range = labelStatisticsImageFilter->GetMaximum(1) - labelStatisticsImageFilter->GetMinimum(1);
64  // --------------- Uniformity, Entropy --------------------
65  double count = labelStatisticsImageFilter->GetCount(1);
66  //double std_dev = labelStatisticsImageFilter->GetSigma(1);
67  double uncorrected_std_dev = std::sqrt((count - 1) / count * labelStatisticsImageFilter->GetVariance(1));
68  double mean = labelStatisticsImageFilter->GetMean(1);
69  auto histogram = labelStatisticsImageFilter->GetHistogram(1);
70  HIndexType index;
71  index.SetSize(1);
72  double binWidth = histogram->GetBinMax(0, 0) - histogram->GetBinMin(0, 0);
73 
74  double uniformity = 0;
75  double entropy = 0;
76  double squared_sum = 0;
77  double kurtosis = 0;
78  double mean_absolut_deviation = 0;
79  double skewness = 0;
80  double sum_prob = 0;
81 
82  double Log2=log(2);
83  for (int i = 0; i < (int)(histogram->GetSize(0)); ++i)
84  {
85  index[0] = i;
86  double prob = histogram->GetFrequency(index);
87 
88  if (prob < 0.1)
89  continue;
90 
91  double voxelValue = histogram->GetBinMin(0, i) +binWidth * 0.5;
92 
93  sum_prob += prob;
94  squared_sum += prob * voxelValue*voxelValue;
95 
96  prob /= count;
97  mean_absolut_deviation += prob* std::abs(voxelValue - mean);
98 
99  kurtosis +=prob* (voxelValue - mean) * (voxelValue - mean) * (voxelValue - mean) * (voxelValue - mean);
100  skewness += prob* (voxelValue - mean) * (voxelValue - mean) * (voxelValue - mean);
101 
102  uniformity += prob*prob;
103  if (prob > 0)
104  {
105  entropy += prob * std::log(prob) / Log2;
106  }
107  }
108 
109  double rms = std::sqrt(squared_sum / count);
110  kurtosis = kurtosis / (uncorrected_std_dev*uncorrected_std_dev * uncorrected_std_dev*uncorrected_std_dev);
111  skewness = skewness / (uncorrected_std_dev*uncorrected_std_dev * uncorrected_std_dev);
112  //mean_absolut_deviation = mean_absolut_deviation;
113  double coveredGrayValueRange = range / imageRange;
114 
115  featureList.push_back(std::make_pair("FirstOrder Range",range));
116  featureList.push_back(std::make_pair("FirstOrder Uniformity",uniformity));
117  featureList.push_back(std::make_pair("FirstOrder Entropy",entropy));
118  featureList.push_back(std::make_pair("FirstOrder Energy",squared_sum));
119  featureList.push_back(std::make_pair("FirstOrder RMS",rms));
120  featureList.push_back(std::make_pair("FirstOrder Kurtosis",kurtosis));
121  featureList.push_back(std::make_pair("FirstOrder Skewness",skewness));
122  featureList.push_back(std::make_pair("FirstOrder Mean absolute deviation",mean_absolut_deviation));
123  featureList.push_back(std::make_pair("FirstOrder Covered Image Intensity Range",coveredGrayValueRange));
124 
125  featureList.push_back(std::make_pair("FirstOrder Minimum",labelStatisticsImageFilter->GetMinimum(1)));
126  featureList.push_back(std::make_pair("FirstOrder Maximum",labelStatisticsImageFilter->GetMaximum(1)));
127  featureList.push_back(std::make_pair("FirstOrder Mean",labelStatisticsImageFilter->GetMean(1)));
128  featureList.push_back(std::make_pair("FirstOrder Variance",labelStatisticsImageFilter->GetVariance(1)));
129  featureList.push_back(std::make_pair("FirstOrder Sum",labelStatisticsImageFilter->GetSum(1)));
130  featureList.push_back(std::make_pair("FirstOrder Median",labelStatisticsImageFilter->GetMedian(1)));
131  featureList.push_back(std::make_pair("FirstOrder Standard deviation",labelStatisticsImageFilter->GetSigma(1)));
132  featureList.push_back(std::make_pair("FirstOrder No. of Voxel",labelStatisticsImageFilter->GetCount(1)));
133 }
134 
136  m_HistogramSize(256), m_UseCtRange(false)
137 {
138 }
139 
141 {
142  FeatureListType featureList;
143 
144  ParameterStruct params;
145  params.m_HistogramSize = this->m_HistogramSize;
146  params.m_UseCtRange = this->m_UseCtRange;
147 
148  AccessByItk_3(image, CalculateFirstOrderStatistics, mask, featureList, params);
149 
150  return featureList;
151 }
152 
154 {
155  FeatureNameListType featureList;
156  featureList.push_back("FirstOrder Minimum");
157  featureList.push_back("FirstOrder Maximum");
158  featureList.push_back("FirstOrder Mean");
159  featureList.push_back("FirstOrder Variance");
160  featureList.push_back("FirstOrder Sum");
161  featureList.push_back("FirstOrder Median");
162  featureList.push_back("FirstOrder Standard deviation");
163  featureList.push_back("FirstOrder No. of Voxel");
164  return featureList;
165 }
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
itk::SmartPointer< Self > Pointer
itk::Image< unsigned char, 3 > ImageType
std::vector< std::pair< std::string, double > > FeatureListType
virtual FeatureNameListType GetFeatureNames() override
Returns a list of the names of all features that are calculated from this class.
void CalculateFirstOrderStatistics(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer mask, mitk::GIFFirstOrderStatistics::FeatureListType &featureList, mitk::GIFFirstOrderStatistics::ParameterStruct params)
virtual FeatureListType CalculateFeatures(const Image::Pointer &image, const Image::Pointer &feature) override
Calculates the Cooccurence-Matrix based features for this class.
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.