Medical Imaging Interaction Toolkit  2018.4.99-9a29ffc6
Medical Imaging Interaction Toolkit
mitkGIFImageDescriptionFeatures.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
21 #include <itkLabelStatisticsImageFilter.h>
22 #include <itkMinimumMaximumImageCalculator.h>
23 #include <itkImageRegionConstIteratorWithIndex.h>
24 
25 // STL
26 #include <sstream>
27 
28 template<typename TPixel, unsigned int VImageDimension>
29 static void
30 CalculateFirstOrderStatistics(itk::Image<TPixel, VImageDimension>* itkImage, mitk::Image::Pointer mask, mitk::GIFImageDescriptionFeatures::FeatureListType & featureList, std::string prefix)
31 {
32  typedef itk::Image<TPixel, VImageDimension> ImageType;
33  typedef itk::Image<unsigned short, VImageDimension> MaskType;
34  typedef itk::MinimumMaximumImageCalculator<ImageType> MinMaxComputerType;
35 
36  typename MaskType::Pointer maskImage = MaskType::New();
37  mitk::CastToItkImage(mask, maskImage);
38 
39  unsigned int imageDimensionX = itkImage->GetLargestPossibleRegion().GetSize()[0];
40  unsigned int imageDimensionY = itkImage->GetLargestPossibleRegion().GetSize()[1];
41  unsigned int imageDimensionZ = itkImage->GetLargestPossibleRegion().GetSize()[2];
42 
43  double imageVoxelSpacingX = itkImage->GetSpacing()[0];
44  double imageVoxelSpacingY = itkImage->GetSpacing()[1];
45  double imageVoxelSpacingZ = itkImage->GetSpacing()[2];
46 
47  typename MinMaxComputerType::Pointer minMaxComputer = MinMaxComputerType::New();
48  minMaxComputer->SetImage(itkImage);
49  minMaxComputer->Compute();
50 
51  double imageMinimum = minMaxComputer->GetMinimum();
52  double imageMaximum = minMaxComputer->GetMaximum();
53 
54 
55  unsigned int maskDimensionX = maskImage->GetLargestPossibleRegion().GetSize()[0];
56  unsigned int maskDimensionY = maskImage->GetLargestPossibleRegion().GetSize()[1];
57  unsigned int maskDimensionZ = maskImage->GetLargestPossibleRegion().GetSize()[2];
58 
59  double maskVoxelSpacingX = maskImage->GetSpacing()[0];
60  double maskVoxelSpacingY = maskImage->GetSpacing()[1];
61  double maskVoxelSpacingZ = maskImage->GetSpacing()[2];
62 
63 
64  unsigned int voxelCount = 0;
65  unsigned int maskVoxelCount = 0;
66  double maskMinimum = imageMaximum;
67  double maskMaximum = imageMinimum;
68  double imageMean = 0;
69  double maskMean = 0;
70 
71  int maskMinimumX = maskDimensionX;
72  int maskMaximumX = 0;
73  int maskMinimumY = maskDimensionY;
74  int maskMaximumY = 0;
75  int maskMinimumZ = maskDimensionZ;
76  int maskMaximumZ = 0;
77 
78 
79  itk::ImageRegionConstIteratorWithIndex<ImageType> imIter(itkImage, itkImage->GetLargestPossibleRegion());
80  itk::ImageRegionConstIteratorWithIndex<MaskType> maIter(maskImage, maskImage->GetLargestPossibleRegion());
81 
82  while (!imIter.IsAtEnd())
83  {
84  auto pixelValue = imIter.Get();
85  if (maIter.Get() > 0)
86  {
87  ++maskVoxelCount;
88  maskMean += pixelValue;
89  maskMinimum = (maskMinimum > pixelValue) ? pixelValue : maskMinimum;
90  maskMaximum = (maskMaximum < pixelValue) ? pixelValue : maskMaximum;
91  maskMinimumX = (maskMinimumX > imIter.GetIndex()[0]) ? imIter.GetIndex()[0] : maskMinimumX;
92  maskMaximumX = (maskMaximumX < imIter.GetIndex()[0]) ? imIter.GetIndex()[0] : maskMaximumX;
93  maskMinimumY = (maskMinimumY > imIter.GetIndex()[1]) ? imIter.GetIndex()[1] : maskMinimumY;
94  maskMaximumY = (maskMaximumY < imIter.GetIndex()[1]) ? imIter.GetIndex()[1] : maskMaximumY;
95  maskMinimumZ = (maskMinimumZ > imIter.GetIndex()[2]) ? imIter.GetIndex()[2] : maskMinimumZ;
96  maskMaximumZ = (maskMaximumZ < imIter.GetIndex()[2]) ? imIter.GetIndex()[2] : maskMaximumZ;
97  }
98  ++voxelCount;
99  imageMean += pixelValue;
100  ++imIter;
101  ++maIter;
102  }
103  imageMean /= voxelCount;
104  maskMean /= maskVoxelCount;
105 
106  featureList.push_back(std::make_pair(prefix + "Image Dimension X", imageDimensionX));
107  featureList.push_back(std::make_pair(prefix + "Image Dimension Y", imageDimensionY));
108  featureList.push_back(std::make_pair(prefix + "Image Dimension Z", imageDimensionZ));
109  featureList.push_back(std::make_pair(prefix + "Image Spacing X", imageVoxelSpacingX));
110  featureList.push_back(std::make_pair(prefix + "Image Spacing Y", imageVoxelSpacingY));
111  featureList.push_back(std::make_pair(prefix + "Image Spacing Z", imageVoxelSpacingZ));
112  featureList.push_back(std::make_pair(prefix + "Image Mean intensity", imageMean));
113  featureList.push_back(std::make_pair(prefix + "Image Minimum intensity", imageMinimum));
114  featureList.push_back(std::make_pair(prefix + "Image Maximum intensity", imageMaximum));
115 
116  featureList.push_back(std::make_pair(prefix + "Mask Dimension X", maskDimensionX));
117  featureList.push_back(std::make_pair(prefix + "Mask Dimension Y", maskDimensionY));
118  featureList.push_back(std::make_pair(prefix + "Mask Dimension Z", maskDimensionZ));
119  featureList.push_back(std::make_pair(prefix + "Mask bounding box X", maskMaximumX - maskMinimumX + 1));
120  featureList.push_back(std::make_pair(prefix + "Mask bounding box Y", maskMaximumY - maskMinimumY + 1));
121  featureList.push_back(std::make_pair(prefix + "Mask bounding box Z", maskMaximumZ - maskMinimumZ + 1));
122  featureList.push_back(std::make_pair(prefix + "Mask Spacing X", maskVoxelSpacingX));
123  featureList.push_back(std::make_pair(prefix + "Mask Spacing Y", maskVoxelSpacingY));
124  featureList.push_back(std::make_pair(prefix + "Mask Spacing Z", maskVoxelSpacingZ));
125  featureList.push_back(std::make_pair(prefix + "Mask Voxel Count", maskVoxelCount));
126  featureList.push_back(std::make_pair(prefix + "Mask Mean intensity", maskMean));
127  featureList.push_back(std::make_pair(prefix + "Mask Minimum intensity", maskMinimum));
128  featureList.push_back(std::make_pair(prefix + "Mask Maximum intensity", maskMaximum));
129 
130 }
131 
133 {
134  SetShortName("id");
135  SetLongName("image-diagnostic");
136 
137  SetFeatureClassName("Diagnostic");
138 }
139 
141 {
142  FeatureListType featureList;
144 
145  return featureList;
146 }
147 
149 {
150  FeatureNameListType featureList;
151  return featureList;
152 }
153 
154 
156 {
157  std::string name = GetOptionPrefix();
158  parser.addArgument(GetLongName(), name, mitkCommandLineParser::Bool, "Use Image Description", "calculates image description features", us::Any());
159 }
160 
161 void
163 {
164  auto parsedArgs = GetParameter();
165  if (parsedArgs.count(GetLongName()))
166  {
167  MITK_INFO << "Start calculating image description features....";
168  auto localResults = this->CalculateFeatures(feature, maskNoNAN);
169  featureList.insert(featureList.end(), localResults.begin(), localResults.end());
170  MITK_INFO << "Finished calculating image description features....";
171  }
172 }
173 
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
FeatureListType CalculateFeatures(const Image::Pointer &image, const Image::Pointer &mask) 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 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)
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< std::pair< std::string, double > > FeatureListType
void AddArguments(mitkCommandLineParser &parser) override
std::string FeatureDescriptionPrefix()
Returns a string that encodes the feature class name.
FeatureNameListType GetFeatureNames() override
Returns a list of the names of all features that are calculated from this class.
virtual std::string GetLongName() const
Definition: usAny.h:163
mitk::Image::Pointer image
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
static void CalculateFirstOrderStatistics(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer mask, mitk::GIFImageDescriptionFeatures::FeatureListType &featureList, std::string prefix)
virtual ParameterTypes GetParameter() const