Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkGIFGreyLevelRunLength.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 // MITK
16 #include <mitkITKImageImport.h>
17 #include <mitkImageCast.h>
18 #include <mitkImageAccessByItk.h>
19 
20 // ITK
22 
23 // STL
24 #include <sstream>
25 
26 template<typename TPixel, unsigned int VImageDimension>
27 void
29 {
30  typedef itk::Image<TPixel, VImageDimension> ImageType;
31  typedef itk::Image<TPixel, VImageDimension> MaskType;
33  typedef typename FilterType::RunLengthFeaturesFilterType TextureFilterType;
34 
35  typename MaskType::Pointer maskImage = MaskType::New();
36  mitk::CastToItkImage(mask, maskImage);
37 
38  typename FilterType::Pointer filter = FilterType::New();
39  typename FilterType::Pointer filter2 = FilterType::New();
40 
41  typename FilterType::OffsetVector::Pointer newOffset = FilterType::OffsetVector::New();
42  auto oldOffsets = filter->GetOffsets();
43  auto oldOffsetsIterator = oldOffsets->Begin();
44  while (oldOffsetsIterator != oldOffsets->End())
45  {
46  bool continueOuterLoop = false;
47  typename FilterType::OffsetType offset = oldOffsetsIterator->Value();
48  for (unsigned int i = 0; i < VImageDimension; ++i)
49  {
50  if (params.m_Direction == i + 2 && offset[i] != 0)
51  {
52  continueOuterLoop = true;
53  }
54  }
55  if (params.m_Direction == 1)
56  {
57  offset[0] = 0;
58  offset[1] = 0;
59  offset[2] = 1;
60  newOffset->push_back(offset);
61  break;
62  }
63 
64  oldOffsetsIterator++;
65  if (continueOuterLoop)
66  continue;
67  newOffset->push_back(offset);
68  }
69  filter->SetOffsets(newOffset);
70  filter2->SetOffsets(newOffset);
71 
72 
73  // All features are required
74  typename FilterType::FeatureNameVectorPointer requestedFeatures = FilterType::FeatureNameVector::New();
75  requestedFeatures->push_back(TextureFilterType::ShortRunEmphasis);
76  requestedFeatures->push_back(TextureFilterType::LongRunEmphasis);
77  requestedFeatures->push_back(TextureFilterType::GreyLevelNonuniformity);
78  requestedFeatures->push_back(TextureFilterType::GreyLevelNonuniformityNormalized);
79  requestedFeatures->push_back(TextureFilterType::RunLengthNonuniformity);
80  requestedFeatures->push_back(TextureFilterType::RunLengthNonuniformityNormalized);
81  requestedFeatures->push_back(TextureFilterType::LowGreyLevelRunEmphasis);
82  requestedFeatures->push_back(TextureFilterType::HighGreyLevelRunEmphasis);
83  requestedFeatures->push_back(TextureFilterType::ShortRunLowGreyLevelEmphasis);
84  requestedFeatures->push_back(TextureFilterType::ShortRunHighGreyLevelEmphasis);
85  requestedFeatures->push_back(TextureFilterType::LongRunLowGreyLevelEmphasis);
86  requestedFeatures->push_back(TextureFilterType::LongRunHighGreyLevelEmphasis);
87  requestedFeatures->push_back(TextureFilterType::RunPercentage);
88  requestedFeatures->push_back(TextureFilterType::NumberOfRuns);
89  requestedFeatures->push_back(TextureFilterType::GreyLevelVariance);
90  requestedFeatures->push_back(TextureFilterType::RunLengthVariance);
91  requestedFeatures->push_back(TextureFilterType::RunEntropy);
92 
93  filter->SetInput(itkImage);
94  filter->SetMaskImage(maskImage);
95  filter->SetRequestedFeatures(requestedFeatures);
96  filter2->SetInput(itkImage);
97  filter2->SetMaskImage(maskImage);
98  filter2->SetRequestedFeatures(requestedFeatures);
99  int numberOfBins = params.Bins;
100  if (numberOfBins < 2)
101  numberOfBins = 256;
102 
103  double minRange = params.MinimumIntensity;
104  double maxRange = params.MaximumIntensity;
105 
106  filter->SetPixelValueMinMax(minRange, maxRange);
107  filter->SetNumberOfBinsPerAxis(numberOfBins);
108  filter2->SetPixelValueMinMax(minRange, maxRange);
109  filter2->SetNumberOfBinsPerAxis(numberOfBins);
110 
111  filter->SetDistanceValueMinMax(0, numberOfBins);
112  filter2->SetDistanceValueMinMax(0, numberOfBins);
113 
114  filter2->CombinedFeatureCalculationOn();
115 
116  filter->Update();
117  filter2->Update();
118 
119  auto featureMeans = filter->GetFeatureMeans ();
120  auto featureStd = filter->GetFeatureStandardDeviations();
121  auto featureCombined = filter2->GetFeatureMeans();
122 
123  for (std::size_t i = 0; i < featureMeans->size(); ++i)
124  {
125  switch (i)
126  {
127  case TextureFilterType::ShortRunEmphasis :
128  featureList.push_back(std::make_pair(params.featurePrefix + "Short run emphasis Means",featureMeans->ElementAt(i)));
129  featureList.push_back(std::make_pair(params.featurePrefix + "Short run emphasis Std.", featureStd->ElementAt(i)));
130  featureList.push_back(std::make_pair(params.featurePrefix + "Short run emphasis Comb.", featureCombined->ElementAt(i)));
131  break;
132  case TextureFilterType::LongRunEmphasis :
133  featureList.push_back(std::make_pair(params.featurePrefix + "Long run emphasis Means", featureMeans->ElementAt(i)));
134  featureList.push_back(std::make_pair(params.featurePrefix + "Long run emphasis Std.", featureStd->ElementAt(i)));
135  featureList.push_back(std::make_pair(params.featurePrefix + "Long run emphasis Comb.", featureCombined->ElementAt(i)));
136  break;
137  case TextureFilterType::GreyLevelNonuniformity :
138  featureList.push_back(std::make_pair(params.featurePrefix + "Grey level nonuniformity Means", featureMeans->ElementAt(i)));
139  featureList.push_back(std::make_pair(params.featurePrefix + "Grey level nonuniformity Std.", featureStd->ElementAt(i)));
140  featureList.push_back(std::make_pair(params.featurePrefix + "Grey level nonuniformity Comb.", featureCombined->ElementAt(i)));
141  break;
142  case TextureFilterType::GreyLevelNonuniformityNormalized :
143  featureList.push_back(std::make_pair(params.featurePrefix + "Grey level nonuniformity normalized Means", featureMeans->ElementAt(i)));
144  featureList.push_back(std::make_pair(params.featurePrefix + "Grey level nonuniformity normalized Std.", featureStd->ElementAt(i)));
145  featureList.push_back(std::make_pair(params.featurePrefix + "Grey level nonuniformity normalized Comb.", featureCombined->ElementAt(i)));
146  break;
147  case TextureFilterType::RunLengthNonuniformity :
148  featureList.push_back(std::make_pair(params.featurePrefix + "Run length nonuniformity Means", featureMeans->ElementAt(i)));
149  featureList.push_back(std::make_pair(params.featurePrefix + "Run length nonuniformity Std.", featureStd->ElementAt(i)));
150  featureList.push_back(std::make_pair(params.featurePrefix + "Run length nonuniformity Comb.", featureCombined->ElementAt(i)));
151  break;
152  case TextureFilterType::RunLengthNonuniformityNormalized :
153  featureList.push_back(std::make_pair(params.featurePrefix + "Run length nonuniformity normalized Means", featureMeans->ElementAt(i)));
154  featureList.push_back(std::make_pair(params.featurePrefix + "Run length nonuniformity normalized Std.", featureStd->ElementAt(i)));
155  featureList.push_back(std::make_pair(params.featurePrefix + "Run length nonuniformity normalized Comb.", featureCombined->ElementAt(i)));
156  break;
157  case TextureFilterType::LowGreyLevelRunEmphasis :
158  featureList.push_back(std::make_pair(params.featurePrefix + "Low grey level run emphasis Means", featureMeans->ElementAt(i)));
159  featureList.push_back(std::make_pair(params.featurePrefix + "Low grey level run emphasis Std.", featureStd->ElementAt(i)));
160  featureList.push_back(std::make_pair(params.featurePrefix + "Low grey level run emphasis Comb.", featureCombined->ElementAt(i)));
161  break;
162  case TextureFilterType::HighGreyLevelRunEmphasis :
163  featureList.push_back(std::make_pair(params.featurePrefix + "High grey level run emphasis Means", featureMeans->ElementAt(i)));
164  featureList.push_back(std::make_pair(params.featurePrefix + "High grey level run emphasis Std.", featureStd->ElementAt(i)));
165  featureList.push_back(std::make_pair(params.featurePrefix + "High grey level run emphasis Comb.", featureCombined->ElementAt(i)));
166  break;
167  case TextureFilterType::ShortRunLowGreyLevelEmphasis :
168  featureList.push_back(std::make_pair(params.featurePrefix + "Short run low grey level emphasis Means", featureMeans->ElementAt(i)));
169  featureList.push_back(std::make_pair(params.featurePrefix + "Short run low grey level emphasis Std.", featureStd->ElementAt(i)));
170  featureList.push_back(std::make_pair(params.featurePrefix + "Short run low grey level emphasis Comb.", featureCombined->ElementAt(i)));
171  break;
172  case TextureFilterType::ShortRunHighGreyLevelEmphasis :
173  featureList.push_back(std::make_pair(params.featurePrefix + "Short run high grey level emphasis Means", featureMeans->ElementAt(i)));
174  featureList.push_back(std::make_pair(params.featurePrefix + "Short run high grey level emphasis Std.", featureStd->ElementAt(i)));
175  featureList.push_back(std::make_pair(params.featurePrefix + "Short run high grey level emphasis Comb.", featureCombined->ElementAt(i)));
176  break;
177  case TextureFilterType::LongRunLowGreyLevelEmphasis :
178  featureList.push_back(std::make_pair(params.featurePrefix + "Long run low grey level emphasis Means", featureMeans->ElementAt(i)));
179  featureList.push_back(std::make_pair(params.featurePrefix + "Long run low grey level emphasis Std.", featureStd->ElementAt(i)));
180  featureList.push_back(std::make_pair(params.featurePrefix + "Long run low grey level emphasis Comb.", featureCombined->ElementAt(i)));
181  break;
182  case TextureFilterType::LongRunHighGreyLevelEmphasis :
183  featureList.push_back(std::make_pair(params.featurePrefix + "Long run high grey level emphasis Means", featureMeans->ElementAt(i)));
184  featureList.push_back(std::make_pair(params.featurePrefix + "Long run high grey level emphasis Std.", featureStd->ElementAt(i)));
185  featureList.push_back(std::make_pair(params.featurePrefix + "Long run high grey level emphasis Comb.", featureCombined->ElementAt(i)));
186  break;
187  case TextureFilterType::RunPercentage :
188  featureList.push_back(std::make_pair(params.featurePrefix + "Run percentage Means", featureMeans->ElementAt(i)));
189  featureList.push_back(std::make_pair(params.featurePrefix + "Run percentage Std.", featureStd->ElementAt(i)));
190  featureList.push_back(std::make_pair(params.featurePrefix + "Run percentage Comb.", featureCombined->ElementAt(i) / newOffset->size()));
191  break;
192  case TextureFilterType::NumberOfRuns :
193  featureList.push_back(std::make_pair(params.featurePrefix + "Number of runs Means", featureMeans->ElementAt(i)));
194  featureList.push_back(std::make_pair(params.featurePrefix + "Number of runs Std.", featureStd->ElementAt(i)));
195  featureList.push_back(std::make_pair(params.featurePrefix + "Number of runs Comb.", featureCombined->ElementAt(i)));
196  break;
197  case TextureFilterType::GreyLevelVariance :
198  featureList.push_back(std::make_pair(params.featurePrefix + "Grey level variance Means", featureMeans->ElementAt(i)));
199  featureList.push_back(std::make_pair(params.featurePrefix + "Grey level variance Std.", featureStd->ElementAt(i)));
200  featureList.push_back(std::make_pair(params.featurePrefix + "Grey level variance Comb.", featureCombined->ElementAt(i)));
201  break;
202  case TextureFilterType::RunLengthVariance :
203  featureList.push_back(std::make_pair(params.featurePrefix + "Run length variance Means", featureMeans->ElementAt(i)));
204  featureList.push_back(std::make_pair(params.featurePrefix + "Run length variance Std.", featureStd->ElementAt(i)));
205  featureList.push_back(std::make_pair(params.featurePrefix + "Run length variance Comb.", featureCombined->ElementAt(i)));
206  break;
207  case TextureFilterType::RunEntropy :
208  featureList.push_back(std::make_pair(params.featurePrefix + "Run length entropy Means", featureMeans->ElementAt(i)));
209  featureList.push_back(std::make_pair(params.featurePrefix + "Run length entropy Std.", featureStd->ElementAt(i)));
210  featureList.push_back(std::make_pair(params.featurePrefix + "Run length entropy Comb.", featureCombined->ElementAt(i)));
211  break;
212  default:
213  break;
214  }
215  }
216 }
217 
219 {
220  SetShortName("rl");
221  SetLongName("run-length");
222  SetFeatureClassName("Run Length");
223 }
224 
226 {
227  InitializeQuantifier(image, mask);
228  FeatureListType featureList;
229 
230  ParameterStruct params;
231 
232  params.m_Direction = GetDirection();
233 
234  params.MinimumIntensity = GetQuantifier()->GetMinimum();
235  params.MaximumIntensity = GetQuantifier()->GetMaximum();
236  params.Bins = GetQuantifier()->GetBins();
238 
239  MITK_INFO << params.MinimumIntensity;
240  MITK_INFO << params.MaximumIntensity;
241  MITK_INFO << params.m_Direction;
242  MITK_INFO << params.Bins;
243 
244  AccessByItk_3(image, CalculateGrayLevelRunLengthFeatures, mask, featureList,params);
245 
246  return featureList;
247 }
248 
250 {
251  FeatureNameListType featureList;
252  return featureList;
253 }
254 
255 
257 {
258  std::string name = GetOptionPrefix();
259 
260  parser.addArgument(GetLongName(), name, mitkCommandLineParser::Bool, "Use Run-Length", "Calculates Run-Length based features", us::Any());
261  AddQuantifierArguments(parser);
262 }
263 
264 void
266 {
267  auto parsedArgs = GetParameter();
268  std::string name = GetOptionPrefix();
269 
270  if (parsedArgs.count(GetLongName()))
271  {
272  InitializeQuantifierFromParameters(feature, maskNoNAN);
273 
274  MITK_INFO << "Start calculating Run-length";
275  auto localResults = this->CalculateFeatures(feature, maskNoNAN);
276  featureList.insert(featureList.end(), localResults.begin(), localResults.end());
277  MITK_INFO << "Finished calculating Run-length";
278  }
279 
280 }
281 
283 {
284  return QuantifierParameterString();
285 }
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
FeatureListType CalculateFeatures(const Image::Pointer &image, const Image::Pointer &feature) override
Calculates the Cooccurence-Matrix based features for this class.
#define MITK_INFO
Definition: mitkLogMacros.h:18
itk::Image< unsigned char, 3 > ImageType
virtual void SetLongName(std::string _arg)
void AddArguments(mitkCommandLineParser &parser) override
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
FeatureNameListType GetFeatureNames() override
Returns a list of the names of all features that are calculated from this class.
virtual void SetShortName(std::string _arg)
void CalculateGrayLevelRunLengthFeatures(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer mask, mitk::GIFGreyLevelRunLength::FeatureListType &featureList, mitk::GIFGreyLevelRunLength::ParameterStruct params)
std::vector< std::pair< std::string, double > > FeatureListType
std::string GetCurrentFeatureEncoding() override
Adds an additional Separator to the name of the feature, which encodes the used parameters.
static Vector3D offset
std::string FeatureDescriptionPrefix()
Returns a string that encodes the feature class name.
virtual std::string GetLongName() const
Definition: usAny.h:163
virtual int GetDirection() const
mitk::Image::Pointer image
void CalculateFeaturesUsingParameters(const Image::Pointer &feature, const Image::Pointer &mask, const Image::Pointer &maskNoNAN, FeatureListType &featureList) override
Calculates the feature of this abstact interface. Does not necessarily considers the parameter settin...
void AddQuantifierArguments(mitkCommandLineParser &parser)
virtual void SetFeatureClassName(std::string _arg)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
mitk::Image::Pointer mask
virtual IntensityQuantifier::Pointer GetQuantifier()
void InitializeQuantifierFromParameters(const Image::Pointer &feature, const Image::Pointer &mask, unsigned int defaultBins=256)
void InitializeQuantifier(const Image::Pointer &feature, const Image::Pointer &mask, unsigned int defaultBins=256)
virtual ParameterTypes GetParameter() const