Medical Imaging Interaction Toolkit  2018.4.99-b20efe7f
Medical Imaging Interaction Toolkit
mitkGlobalImageFeaturesParameter.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 
16 #include <fstream>
17 #include <itkFileTools.h>
18 #include <itksys/SystemTools.hxx>
19 
20 
21 static bool fileExists(const std::string& filename)
22 {
23  std::ifstream infile(filename.c_str());
24  bool isGood = infile.good();
25  infile.close();
26  return isGood;
27 }
28 
29 
31 {
32  // Required Parameter
33  parser.addArgument("image", "i", mitkCommandLineParser::Image, "Input Image", "Path to the input image file", us::Any(), false, false, false, mitkCommandLineParser::Input);
34  parser.addArgument("mask", "m", mitkCommandLineParser::Image, "Input Mask", "Path to the mask Image that specifies the area over for the statistic (Values = 1)", us::Any(), false, false, false, mitkCommandLineParser::Input);
35  parser.addArgument("morph-mask", "morph", mitkCommandLineParser::Image, "Morphological Image Mask", "Path to the mask Image that specifies the area over for the statistic (Values = 1)", us::Any(), true, false, false, mitkCommandLineParser::Input);
36  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output text file", "Path to output file. The output statistic is appended to this file.", us::Any(), false, false, false, mitkCommandLineParser::Output);
37 
38  // Optional Parameter
39  parser.addArgument("xml-output", "x", mitkCommandLineParser::File, "XML result file", "Path where the results should be stored as XML result file. ", us::Any(), true, false, false, mitkCommandLineParser::Input);
40  parser.addArgument("logfile", "log", mitkCommandLineParser::File, "Text Logfile", "Path to the location of the target log file. ", us::Any(), true, false, false, mitkCommandLineParser::Input);
41  parser.addArgument("save-image", "save-image", mitkCommandLineParser::File, "Output Image", "If spezified, the image that is used for the analysis is saved to this location.", us::Any(), true, false, false, mitkCommandLineParser::Output);
42  parser.addArgument("save-mask", "save-mask", mitkCommandLineParser::File, "Output Image", "If spezified, the mask that is used for the analysis is saved to this location. ", us::Any(), true, false, false, mitkCommandLineParser::Output);
43  parser.addArgument("save-image-screenshots", "save-screenshot", mitkCommandLineParser::File, "Output Image", "If spezified, a screenshot of each slice is saved. Specify an EXISTING folder with prefix (for example ~/demo/ or ~/demo/image-", us::Any(), true, false, false, mitkCommandLineParser::Output);
44 
45  parser.addArgument("header", "head", mitkCommandLineParser::Bool, "Add Header (Labels) to output", "", us::Any());
46  parser.addArgument("first-line-header", "fl-head", mitkCommandLineParser::Bool, "Add Header (Labels) to first line of output", "", us::Any());
47  parser.addArgument("decimal-point", "decimal", mitkCommandLineParser::String, "Decima Point that is used in Conversion", "", us::Any());
48 
49  parser.addArgument("resample-mask", "rm", mitkCommandLineParser::Bool, "Bool", "Resamples the mask to the resolution of the input image ", us::Any());
50  parser.addArgument("same-space", "sp", mitkCommandLineParser::Bool, "Bool", "Set the spacing of all images to equal. Otherwise an error will be thrown. ", us::Any());
51  parser.addArgument("fixed-isotropic", "fi", mitkCommandLineParser::Float, "Float", "Input image resampled to fixed isotropic resolution given in mm. Should be used with resample-mask ", us::Any());
52 
53  parser.addArgument("minimum-intensity", "minimum", mitkCommandLineParser::Float, "Float", "Minimum intensity. If set, it is overwritten by more specific intensity minima", us::Any());
54  parser.addArgument("maximum-intensity", "maximum", mitkCommandLineParser::Float, "Float", "Maximum intensity. If set, it is overwritten by more specific intensity maxima", us::Any());
55  parser.addArgument("bins", "bins", mitkCommandLineParser::Int, "Int", "Number of bins if bins are used. If set, it is overwritten by more specific bin count", us::Any());
56  parser.addArgument("binsize", "binsize", mitkCommandLineParser::Float, "Int", "Size of bins that is used. If set, it is overwritten by more specific bin count", us::Any());
57  parser.addArgument("ignore-mask-for-histogram", "ignore-mask", mitkCommandLineParser::Bool, "Bool", "If the whole image is used to calculate the histogram. ", us::Any());
58  parser.addArgument("encode-parameter-in-name", "encode-parameter", mitkCommandLineParser::Bool, "Bool", "If true, the parameters used for each feature is encoded in its name.", us::Any());
59  parser.addArgument("pipeline-uid", "p", mitkCommandLineParser::String, "Pipeline UID", "UID that is stored in the XML output and identifies the processing pipeline the app is used in.", us::Any());
60  parser.addArgument("all-features", "a", mitkCommandLineParser::Bool, "Calculate all features", "If true, all features will be calculated and the feature specific activation will be ignored.", us::Any());
61 }
62 
63 void mitk::cl::GlobalImageFeaturesParameter::ParseParameter(std::map<std::string, us::Any> parsedArgs)
64 {
65  ParseFileLocations(parsedArgs);
66  ParseAdditionalOutputs(parsedArgs);
67  ParseHeaderInformation(parsedArgs);
68  ParseMaskAdaptation(parsedArgs);
69  ParseGlobalFeatureParameter(parsedArgs);
70 }
71 
72 void mitk::cl::GlobalImageFeaturesParameter::ParseFileLocations(std::map<std::string, us::Any> &parsedArgs)
73 {
74 
75  //
76  // Read input and output file informations
77  //
78  imagePath = parsedArgs["image"].ToString();
79  maskPath = parsedArgs["mask"].ToString();
80  outputPath = parsedArgs["output"].ToString();
81 
82  imageFolder = itksys::SystemTools::GetFilenamePath(imagePath);
83  imageName = itksys::SystemTools::GetFilenameName(imagePath);
84  maskFolder = itksys::SystemTools::GetFilenamePath(maskPath);
85  maskName = itksys::SystemTools::GetFilenameName(maskPath);
86 
87  useMorphMask = false;
88  if (parsedArgs.count("morph-mask"))
89  {
90  useMorphMask = true;
91  morphPath = parsedArgs["morph-mask"].ToString();
92  morphName = itksys::SystemTools::GetFilenameName(morphPath);
93  }
94 
95  outputXMLPath = "";
96  if (parsedArgs.count("xml-output"))
97  {
98  outputXMLPath = parsedArgs["xml-output"].ToString();
99  }
100 }
101 
102 void mitk::cl::GlobalImageFeaturesParameter::ParseAdditionalOutputs(std::map<std::string, us::Any> &parsedArgs)
103 {
104 
105  //
106  // Read input and output file informations
107  //
108  useLogfile = false;
109  if (parsedArgs.count("logfile"))
110  {
111  useLogfile = true;
112  logfilePath = us::any_cast<std::string>(parsedArgs["logfile"]);
113  }
114  writeAnalysisImage = false;
115  if (parsedArgs.count("save-image"))
116  {
117  writeAnalysisImage = true;
118  anaylsisImagePath = us::any_cast<std::string>(parsedArgs["save-image"]);
119  }
120  writeAnalysisMask = false;
121  if (parsedArgs.count("save-mask"))
122  {
123  writeAnalysisMask = true;
124  analysisMaskPath = us::any_cast<std::string>(parsedArgs["save-mask"]);
125  }
126  writePNGScreenshots = false;
127  if (parsedArgs.count("save-image-screenshots"))
128  {
129  writePNGScreenshots = true;
130  pngScreenshotsPath = us::any_cast<std::string>(parsedArgs["save-image-screenshots"]);
131  std::string pngScrenshotFolderPath = itksys::SystemTools::GetFilenamePath(pngScreenshotsPath);
132  if (pngScreenshotsPath.back() == '/' || pngScreenshotsPath.back() == '\\')
133  {
134  pngScrenshotFolderPath = pngScreenshotsPath;
135  }
136  itk::FileTools::CreateDirectory(pngScrenshotFolderPath.c_str());
137  }
138  useDecimalPoint = false;
139  if (parsedArgs.count("decimal-point"))
140  {
141  auto tmpDecimalPoint = us::any_cast<std::string>(parsedArgs["decimal-point"]);
142  if (tmpDecimalPoint.length() > 0)
143  {
144  useDecimalPoint = true;
145  decimalPoint = tmpDecimalPoint.at(0);
146  }
147  }
148 
149  pipelineUID = "";
150  if (parsedArgs.count("pipeline-uid"))
151  {
152  pipelineUID = us::any_cast<std::string>(parsedArgs["pipeline-uid"]);
153  }
154 
155  calculateAllFeatures = parsedArgs.count("all-features");
156 }
157 
158 void mitk::cl::GlobalImageFeaturesParameter::ParseHeaderInformation(std::map<std::string, us::Any> &parsedArgs)
159 {
160  //
161  // Check if an header is required or not. Consider also first line header option.
162  //
163  useHeader = false;
165  if (parsedArgs.count("header"))
166  {
167  useHeader = us::any_cast<bool>(parsedArgs["header"]);
168  }
169  if (parsedArgs.count("first-line-header"))
170  {
171  useHeaderForFirstLineOnly = us::any_cast<bool>(parsedArgs["first-line-header"]);
172  }
174  {
176  }
177 }
178 
179 void mitk::cl::GlobalImageFeaturesParameter::ParseMaskAdaptation(std::map<std::string, us::Any> &parsedArgs)
180 {
181  //
182  // Parse parameters that control how the input mask is adapted to the input image
183  //
184  resampleMask = false;
185  ensureSameSpace = false;
186  resampleToFixIsotropic = false;
187  resampleResolution = 1.0;
188  if (parsedArgs.count("resample-mask"))
189  {
190  resampleMask = us::any_cast<bool>(parsedArgs["resample-mask"]);
191  }
192  if (parsedArgs.count("same-space"))
193  {
194  ensureSameSpace = us::any_cast<bool>(parsedArgs["same-space"]);
195  }
196  if (parsedArgs.count("fixed-isotropic"))
197  {
198  resampleToFixIsotropic = true;
199  resampleResolution = us::any_cast<float>(parsedArgs["fixed-isotropic"]);
200  }
201 }
202 
203 void mitk::cl::GlobalImageFeaturesParameter::ParseGlobalFeatureParameter(std::map<std::string, us::Any> &parsedArgs)
204 {
205  //
206  // Parse parameters that control how the input mask is adapted to the input image
207  //
210  defineGlobalNumberOfBins = false;
211  encodeParameter = false;
212  if (parsedArgs.count("minimum-intensity"))
213  {
215  globalMinimumIntensity = us::any_cast<float>(parsedArgs["minimum-intensity"]);
216  }
217  if (parsedArgs.count("maximum-intensity"))
218  {
220  globalMaximumIntensity = us::any_cast<float>(parsedArgs["maximum-intensity"]);
221  }
222  if (parsedArgs.count("bins"))
223  {
225  globalNumberOfBins = us::any_cast<int>(parsedArgs["bins"]);
226  }
227  if (parsedArgs.count("encode-parameter-in-name"))
228  {
229  encodeParameter = true;
230  }
231 }
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
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)
Definition: usAny.h:163
void ParseParameter(std::map< std::string, us::Any > parsedArgs)
static bool fileExists(const std::string &filename)