Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
CLGlobalImageFeatures.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 #ifndef mitkCLPolyToNrrd_cpp
17 #define mitkCLPolyToNrrd_cpp
18 
19 #include "time.h"
20 #include <sstream>
21 #include <fstream>
22 
23 #include <mitkIOUtil.h>
24 #include "mitkCommandLineParser.h"
25 
30 
31 typedef itk::Image< double, 3 > FloatImageType;
32 typedef itk::Image< unsigned char, 3 > MaskImageType;
33 
34 static std::vector<double> splitDouble(std::string str, char delimiter) {
35  std::vector<double> internal;
36  std::stringstream ss(str); // Turn the string into a stream.
37  std::string tok;
38  double val;
39  while (std::getline(ss, tok, delimiter)) {
40  std::stringstream s2(tok);
41  s2 >> val;
42  internal.push_back(val);
43  }
44 
45  return internal;
46 }
47 
48 int main(int argc, char* argv[])
49 {
50  mitkCommandLineParser parser;
51  parser.setArgumentPrefix("--", "-");
52  // required params
53  parser.addArgument("image", "i", mitkCommandLineParser::InputImage, "Input Image", "Path to the input VTK polydata", us::Any(), false);
54  parser.addArgument("mask", "m", mitkCommandLineParser::InputImage, "Input Mask", "Mask Image that specifies the area over for the statistic, (Values = 1)", us::Any(), false);
55  parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "Output text file", "Target file. The output statistic is appended to this file.", us::Any(), false);
56 
57  parser.addArgument("cooccurence","cooc",mitkCommandLineParser::String, "Use Co-occurence matrix", "calculates Co-occurence based features",us::Any());
58  parser.addArgument("volume","vol",mitkCommandLineParser::String, "Use Volume-Statistic", "calculates volume based features",us::Any());
59  parser.addArgument("run-length","rl",mitkCommandLineParser::String, "Use Co-occurence matrix", "calculates Co-occurence based features",us::Any());
60  parser.addArgument("first-order","fo",mitkCommandLineParser::String, "Use First Order Features", "calculates First order based features",us::Any());
61  parser.addArgument("header","head",mitkCommandLineParser::String,"Add Header (Labels) to output","",us::Any());
62  parser.addArgument("description","d",mitkCommandLineParser::String,"Text","Description that is added to the output",us::Any());
63  parser.addArgument("same-space", "sp", mitkCommandLineParser::String, "Bool", "Set the spacing of all images to equal. Otherwise an error will be thrown. ", us::Any());
64  parser.addArgument("direction", "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());
65 
66  // Miniapp Infos
67  parser.setCategory("Classification Tools");
68  parser.setTitle("Global Image Feature calculator");
69  parser.setDescription("Calculates different global statistics for a given segmentation / image combination");
70  parser.setContributor("MBI");
71 
72  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
73 
74  if (parsedArgs.size()==0)
75  {
76  return EXIT_FAILURE;
77  }
78  if ( parsedArgs.count("help") || parsedArgs.count("h"))
79  {
80  return EXIT_SUCCESS;
81  }
82 
83  MITK_INFO << "Version: "<< 1.3;
84 
85  bool useCooc = parsedArgs.count("cooccurence");
86 
87  mitk::Image::Pointer image = mitk::IOUtil::LoadImage(parsedArgs["image"].ToString());
88  mitk::Image::Pointer mask = mitk::IOUtil::LoadImage(parsedArgs["mask"].ToString());
89 
90  bool fixDifferentSpaces = parsedArgs.count("same-space");
91  if ( ! mitk::Equal(mask->GetGeometry(0)->GetOrigin(), image->GetGeometry(0)->GetOrigin()))
92  {
93  MITK_INFO << "Not equal Origins";
94  if (fixDifferentSpaces)
95  {
96  image->GetGeometry(0)->SetOrigin(mask->GetGeometry(0)->GetOrigin());
97  } else
98  {
99  return -1;
100  }
101  }
102  if ( ! mitk::Equal(mask->GetGeometry(0)->GetSpacing(), image->GetGeometry(0)->GetSpacing()))
103  {
104  MITK_INFO << "Not equal Sapcings";
105  if (fixDifferentSpaces)
106  {
107  image->GetGeometry(0)->SetSpacing(mask->GetGeometry(0)->GetSpacing());
108  } else
109  {
110  return -1;
111  }
112  }
113 
114  int direction = 0;
115  if (parsedArgs.count("direction"))
116  {
117  direction = splitDouble(parsedArgs["direction"].ToString(), ';')[0];
118  }
119 
122  // CAlculate First Order Features
124  if (parsedArgs.count("first-order"))
125  {
126  MITK_INFO << "Start calculating first order statistics....";
128  auto localResults = firstOrderCalculator->CalculateFeatures(image, mask);
129  stats.insert(stats.end(), localResults.begin(), localResults.end());
130  MITK_INFO << "Finished calculating first order statistics....";
131  }
132 
134  // CAlculate Volume based Features
136  if (parsedArgs.count("volume"))
137  {
138  MITK_INFO << "Start calculating volumetric ....";
140  auto localResults = volCalculator->CalculateFeatures(image, mask);
141  stats.insert(stats.end(), localResults.begin(), localResults.end());
142  MITK_INFO << "Finished calculating volumetric....";
143  }
144 
146  // CAlculate Co-occurence Features
148  if (parsedArgs.count("cooccurence"))
149  {
150  auto ranges = splitDouble(parsedArgs["cooccurence"].ToString(),';');
151 
152  for (int i = 0; i < ranges.size(); ++i)
153  {
154  MITK_INFO << "Start calculating coocurence with range " << ranges[i] << "....";
156  coocCalculator->SetRange(ranges[i]);
157  coocCalculator->SetDirection(direction);
158  auto localResults = coocCalculator->CalculateFeatures(image, mask);
159  stats.insert(stats.end(), localResults.begin(), localResults.end());
160  MITK_INFO << "Finished calculating coocurence with range " << ranges[i] << "....";
161  }
162  }
163 
165  // CAlculate Run-Length Features
167  if (parsedArgs.count("run-length"))
168  {
169  auto ranges = splitDouble(parsedArgs["run-length"].ToString(),';');
170 
171  for (int i = 0; i < ranges.size(); ++i)
172  {
173  MITK_INFO << "Start calculating run-length with number of bins " << ranges[i] << "....";
175  calculator->SetRange(ranges[i]);
176 
177  auto localResults = calculator->CalculateFeatures(image, mask);
178  stats.insert(stats.end(), localResults.begin(), localResults.end());
179  MITK_INFO << "Finished calculating run-length with number of bins " << ranges[i] << "....";
180  }
181  }
182  for (int i = 0; i < stats.size(); ++i)
183  {
184  std::cout << stats[i].first << " - " << stats[i].second <<std::endl;
185  }
186 
187  std::ofstream output(parsedArgs["output"].ToString(),std::ios::app);
188  if ( parsedArgs.count("header") )
189  {
190  if ( parsedArgs.count("description") )
191  {
192  output << "Description" << ";";
193  }
194  for (int i = 0; i < stats.size(); ++i)
195  {
196  output << stats[i].first << ";";
197  }
198  output << std::endl;
199  }
200  if ( parsedArgs.count("description") )
201  {
202  output << parsedArgs["description"].ToString() << ";";
203  }
204  for (int i = 0; i < stats.size(); ++i)
205  {
206  output << stats[i].second << ";";
207  }
208  output << std::endl;
209  output.close();
210 
211  return 0;
212 }
213 
214 #endif
#define MITK_INFO
Definition: mitkLogMacros.h:22
void setContributor(std::string contributor)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
itk::Image< unsigned char, 3 > MaskImageType
std::vector< std::pair< std::string, double > > FeatureListType
static std::vector< double > splitDouble(std::string str, char delimiter)
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)
Definition: usAny.h:163
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
void setTitle(std::string title)
void setDescription(std::string description)
itk::Image< double, 3 > FloatImageType
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:597
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.
int main(int argc, char *argv[])