Medical Imaging Interaction Toolkit  2018.4.99-b7f3afaa
Medical Imaging Interaction Toolkit
mitkGIFNeighbourhoodGreyLevelDifference.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 #include <itkMinimumMaximumImageCalculator.h>
23 
24 // STL
25 #include <sstream>
26 
27 template<typename TPixel, unsigned int VImageDimension>
28 void
30 {
31  typedef itk::Image<TPixel, VImageDimension> ImageType;
32  typedef itk::Image<TPixel, VImageDimension> MaskType;
34  typedef itk::MinimumMaximumImageCalculator<ImageType> MinMaxComputerType;
35  typedef typename FilterType::NeighbourhoodGreyLevelDifferenceFeaturesFilterType TextureFilterType;
36 
37  typename MaskType::Pointer maskImage = MaskType::New();
38  mitk::CastToItkImage(mask, maskImage);
39 
40  typename FilterType::Pointer filter = FilterType::New();
41 
42  typename FilterType::OffsetVector::Pointer newOffset = FilterType::OffsetVector::New();
43  auto oldOffsets = filter->GetOffsets();
44  auto oldOffsetsIterator = oldOffsets->Begin();
45  while (oldOffsetsIterator != oldOffsets->End())
46  {
47  bool continueOuterLoop = false;
48  typename FilterType::OffsetType offset = oldOffsetsIterator->Value();
49  for (unsigned int i = 0; i < VImageDimension; ++i)
50  {
51  if (params.m_Direction == i + 2 && offset[i] != 0)
52  {
53  continueOuterLoop = true;
54  }
55  }
56  if (params.m_Direction == 1)
57  {
58  offset[0] = 0;
59  offset[1] = 0;
60  offset[2] = 1;
61  newOffset->push_back(offset);
62  break;
63  }
64 
65  oldOffsetsIterator++;
66  if (continueOuterLoop)
67  continue;
68  newOffset->push_back(offset);
69  }
70  filter->SetOffsets(newOffset);
71 
72 
73  // All features are required
74  typename FilterType::FeatureNameVectorPointer requestedFeatures = FilterType::FeatureNameVector::New();
75  requestedFeatures->push_back(TextureFilterType::Coarseness);
76  requestedFeatures->push_back(TextureFilterType::Contrast);
77  requestedFeatures->push_back(TextureFilterType::Busyness);
78  requestedFeatures->push_back(TextureFilterType::Complexity);
79  requestedFeatures->push_back(TextureFilterType::Strength);
80 
81  typename MinMaxComputerType::Pointer minMaxComputer = MinMaxComputerType::New();
82  minMaxComputer->SetImage(itkImage);
83  minMaxComputer->Compute();
84 
85  filter->SetInput(itkImage);
86  filter->SetMaskImage(maskImage);
87  filter->SetRequestedFeatures(requestedFeatures);
88  int rangeOfPixels = params.m_Range;
89  if (rangeOfPixels < 2)
90  rangeOfPixels = 256;
91 
92  if (params.m_UseCtRange)
93  {
94  filter->SetPixelValueMinMax((TPixel)(-1024.5),(TPixel)(3096.5));
95  filter->SetNumberOfBinsPerAxis(3096.5+1024.5);
96  } else
97  {
98  filter->SetPixelValueMinMax(minMaxComputer->GetMinimum(),minMaxComputer->GetMaximum());
99  filter->SetNumberOfBinsPerAxis(rangeOfPixels);
100  }
101 
102  filter->SetDistanceValueMinMax(0,rangeOfPixels);
103 
104  filter->Update();
105 
106  auto featureMeans = filter->GetFeatureMeans ();
107  auto featureStd = filter->GetFeatureStandardDeviations();
108 
109  std::ostringstream ss;
110  ss << rangeOfPixels;
111  std::string strRange = ss.str();
112  for (std::size_t i = 0; i < featureMeans->size(); ++i)
113  {
114  switch (i)
115  {
116  case TextureFilterType::Coarseness :
117  featureList.push_back(std::make_pair("NeighbourhoodGreyLevelDifference ("+ strRange+") Coarseness Means",featureMeans->ElementAt(i)));
118  break;
119  case TextureFilterType::Contrast :
120  featureList.push_back(std::make_pair("NeighbourhoodGreyLevelDifference ("+ strRange+") Contrast Means",featureMeans->ElementAt(i)));
121  break;
122  case TextureFilterType::Busyness :
123  featureList.push_back(std::make_pair("NeighbourhoodGreyLevelDifference ("+ strRange+") Busyness Means",featureMeans->ElementAt(i)));
124  break;
125  case TextureFilterType::Complexity :
126  featureList.push_back(std::make_pair("NeighbourhoodGreyLevelDifference ("+ strRange+") Complexity Means",featureMeans->ElementAt(i)));
127  break;
128  case TextureFilterType::Strength :
129  featureList.push_back(std::make_pair("NeighbourhoodGreyLevelDifference ("+ strRange+") Strength Means",featureMeans->ElementAt(i)));
130  break;
131  default:
132  break;
133  }
134  }
135 }
136 
138 m_Range(1.0), m_UseCtRange(false)
139 {
140  SetShortName("ngld");
141  SetLongName("NeighbourhoodGreyLevelDifference");
142 }
143 
145 {
146  FeatureListType featureList;
147 
148  ParameterStruct params;
149  params.m_UseCtRange=m_UseCtRange;
150  params.m_Range = m_Range;
151  params.m_Direction = GetDirection();
152 
154 
155  return featureList;
156 }
157 
159 {
160  FeatureNameListType featureList;
161  featureList.push_back("NeighbourhoodGreyLevelDifference. Coarseness Means");
162  featureList.push_back("NeighbourhoodGreyLevelDifference. Coarseness Std.");
163  featureList.push_back("NeighbourhoodGreyLevelDifference. Contrast Means");
164  featureList.push_back("NeighbourhoodGreyLevelDifference. Contrast Std.");
165  featureList.push_back("NeighbourhoodGreyLevelDifference. Busyness Means");
166  featureList.push_back("NeighbourhoodGreyLevelDifference. Busyness Std.");
167  featureList.push_back("NeighbourhoodGreyLevelDifference. Complexity Means");
168  featureList.push_back("NeighbourhoodGreyLevelDifference. Complexity Std.");
169  featureList.push_back("NeighbourhoodGreyLevelDifference. Strength Means");
170  featureList.push_back("NeighbourhoodGreyLevelDifference. Strength Std.");
171  return featureList;
172 }
173 
174 
175 
177 {
178  std::string name = GetOptionPrefix();
179 
180  parser.addArgument(GetLongName(), name, mitkCommandLineParser::String, "Use Co-occurence matrix", "calculates Co-occurence based features (new implementation)", us::Any());
181  parser.addArgument(name + "::range", name + "::range", mitkCommandLineParser::String, "Cooc 2 Range", "Define the range that is used (Semicolon-separated)", us::Any());
182  parser.addArgument(name + "::direction", name + "::dir", mitkCommandLineParser::String, "Int", "Allows to specify the direction for Cooc and RL. 0: All directions, 1: Only single direction (Test purpose), 2,3,4... without dimension 0,1,2... ", us::Any());
183 }
184 
185 void
187 {
188  auto parsedArgs = GetParameter();
189  std::string name = GetOptionPrefix();
190 
191  if (parsedArgs.count(GetLongName()))
192  {
193  int direction = 0;
194  if (parsedArgs.count(name + "::direction"))
195  {
196  direction = SplitDouble(parsedArgs[name + "::direction"].ToString(), ';')[0];
197  }
198  std::vector<double> ranges;
199  if (parsedArgs.count(name + "::range"))
200  {
201  ranges = SplitDouble(parsedArgs[name + "::range"].ToString(), ';');
202  }
203  else
204  {
205  ranges.push_back(1);
206  }
207 
208  for (std::size_t i = 0; i < ranges.size(); ++i)
209  {
210  MITK_INFO << "Start calculating Neighbourhood Grey Level Difference with range " << ranges[i] << "....";
211  this->SetRange(ranges[i]);
212  this->SetDirection(direction);
213  auto localResults = this->CalculateFeatures(feature, maskNoNAN);
214  featureList.insert(featureList.end(), localResults.begin(), localResults.end());
215  MITK_INFO << "Finished calculating coocurence with range " << ranges[i] << "....";
216  }
217  }
218 
219 }
220 
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
virtual void SetRange(double _arg)
void CalculateGrayLevelNeighbourhoodGreyLevelDifferenceFeatures(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer mask, mitk::GIFNeighbourhoodGreyLevelDifference::FeatureListType &featureList, mitk::GIFNeighbourhoodGreyLevelDifference::ParameterStruct params)
#define MITK_INFO
Definition: mitkLogMacros.h:18
itk::Image< unsigned char, 3 > ImageType
virtual void SetLongName(std::string _arg)
void AddArguments(mitkCommandLineParser &parser) override
virtual void SetDirection(int _arg)
FeatureNameListType GetFeatureNames() override
Returns a list of the names of all features that are calculated from this class.
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)
virtual void SetShortName(std::string _arg)
std::vector< std::pair< std::string, double > > FeatureListType
static Vector3D offset
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...
std::vector< double > SplitDouble(std::string str, char delimiter)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
FeatureListType CalculateFeatures(const Image::Pointer &image, const Image::Pointer &feature) override
Calculates the Cooccurence-Matrix based features for this class.
mitk::Image::Pointer mask
virtual ParameterTypes GetParameter() const