Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
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("logfile", "log", mitkCommandLineParser::File, "Text Logfile", "Path to the location of the target log file. ", us::Any(), true, false, false, mitkCommandLineParser::Input);
40  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);
41  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);
42  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);
43 
44  parser.addArgument("header", "head", mitkCommandLineParser::Bool, "Add Header (Labels) to output", "", us::Any());
45  parser.addArgument("first-line-header", "fl-head", mitkCommandLineParser::Bool, "Add Header (Labels) to first line of output", "", us::Any());
46  parser.addArgument("decimal-point", "decimal", mitkCommandLineParser::String, "Decima Point that is used in Conversion", "", us::Any());
47 
48  parser.addArgument("resample-mask", "rm", mitkCommandLineParser::Bool, "Bool", "Resamples the mask to the resolution of the input image ", us::Any());
49  parser.addArgument("same-space", "sp", mitkCommandLineParser::Bool, "Bool", "Set the spacing of all images to equal. Otherwise an error will be thrown. ", us::Any());
50  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());
51 
52  parser.addArgument("minimum-intensity", "minimum", mitkCommandLineParser::Float, "Float", "Minimum intensity. If set, it is overwritten by more specific intensity minima", us::Any());
53  parser.addArgument("maximum-intensity", "maximum", mitkCommandLineParser::Float, "Float", "Maximum intensity. If set, it is overwritten by more specific intensity maxima", us::Any());
54  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());
55  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());
56  parser.addArgument("ignore-mask-for-histogram", "ignore-mask", mitkCommandLineParser::Bool, "Bool", "If the whole image is used to calculate the histogram. ", us::Any());
57  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());
58 }
59 
60 void mitk::cl::GlobalImageFeaturesParameter::ParseParameter(std::map<std::string, us::Any> parsedArgs)
61 {
62  ParseFileLocations(parsedArgs);
63  ParseAdditionalOutputs(parsedArgs);
64  ParseHeaderInformation(parsedArgs);
65  ParseMaskAdaptation(parsedArgs);
66  ParseGlobalFeatureParameter(parsedArgs);
67 }
68 
69 void mitk::cl::GlobalImageFeaturesParameter::ParseFileLocations(std::map<std::string, us::Any> &parsedArgs)
70 {
71 
72  //
73  // Read input and output file informations
74  //
75  imagePath = parsedArgs["image"].ToString();
76  maskPath = parsedArgs["mask"].ToString();
77  outputPath = parsedArgs["output"].ToString();
78 
79  imageFolder = itksys::SystemTools::GetFilenamePath(imagePath);
80  imageName = itksys::SystemTools::GetFilenameName(imagePath);
81  maskFolder = itksys::SystemTools::GetFilenamePath(maskPath);
82  maskName = itksys::SystemTools::GetFilenameName(maskPath);
83 
84  useMorphMask = false;
85  if (parsedArgs.count("morph-mask"))
86  {
87  useMorphMask = true;
88  morphPath = parsedArgs["morph-mask"].ToString();
89  morphName = itksys::SystemTools::GetFilenameName(morphPath);
90  }
91 
92 }
93 
94 void mitk::cl::GlobalImageFeaturesParameter::ParseAdditionalOutputs(std::map<std::string, us::Any> &parsedArgs)
95 {
96 
97  //
98  // Read input and output file informations
99  //
100  useLogfile = false;
101  if (parsedArgs.count("logfile"))
102  {
103  useLogfile = true;
104  logfilePath = us::any_cast<std::string>(parsedArgs["logfile"]);
105  }
106  writeAnalysisImage = false;
107  if (parsedArgs.count("save-image"))
108  {
109  writeAnalysisImage = true;
110  anaylsisImagePath = us::any_cast<std::string>(parsedArgs["save-image"]);
111  }
112  writeAnalysisMask = false;
113  if (parsedArgs.count("save-mask"))
114  {
115  writeAnalysisMask = true;
116  analysisMaskPath = us::any_cast<std::string>(parsedArgs["save-mask"]);
117  }
118  writePNGScreenshots = false;
119  if (parsedArgs.count("save-image-screenshots"))
120  {
121  writePNGScreenshots = true;
122  pngScreenshotsPath = us::any_cast<std::string>(parsedArgs["save-image-screenshots"]);
123  std::string pngScrenshotFolderPath = itksys::SystemTools::GetFilenamePath(pngScreenshotsPath);
124  if (pngScreenshotsPath.back() == '/' || pngScreenshotsPath.back() == '\\')
125  {
126  pngScrenshotFolderPath = pngScreenshotsPath;
127  }
128  itk::FileTools::CreateDirectory(pngScrenshotFolderPath.c_str());
129  }
130  useDecimalPoint = false;
131  if (parsedArgs.count("decimal-point"))
132  {
133  auto tmpDecimalPoint = us::any_cast<std::string>(parsedArgs["decimal-point"]);
134  if (tmpDecimalPoint.length() > 0)
135  {
136  useDecimalPoint = true;
137  decimalPoint = tmpDecimalPoint.at(0);
138  }
139  }
140 
141 }
142 
143 void mitk::cl::GlobalImageFeaturesParameter::ParseHeaderInformation(std::map<std::string, us::Any> &parsedArgs)
144 {
145  //
146  // Check if an header is required or not. Consider also first line header option.
147  //
148  useHeader = false;
150  if (parsedArgs.count("header"))
151  {
152  useHeader = us::any_cast<bool>(parsedArgs["header"]);
153  }
154  if (parsedArgs.count("first-line-header"))
155  {
156  useHeaderForFirstLineOnly = us::any_cast<bool>(parsedArgs["first-line-header"]);
157  }
159  {
161  }
162 }
163 
164 void mitk::cl::GlobalImageFeaturesParameter::ParseMaskAdaptation(std::map<std::string, us::Any> &parsedArgs)
165 {
166  //
167  // Parse parameters that control how the input mask is adapted to the input image
168  //
169  resampleMask = false;
170  ensureSameSpace = false;
171  resampleToFixIsotropic = false;
172  resampleResolution = 1.0;
173  if (parsedArgs.count("resample-mask"))
174  {
175  resampleMask = us::any_cast<bool>(parsedArgs["resample-mask"]);
176  }
177  if (parsedArgs.count("same-space"))
178  {
179  ensureSameSpace = us::any_cast<bool>(parsedArgs["same-space"]);
180  }
181  if (parsedArgs.count("fixed-isotropic"))
182  {
183  resampleToFixIsotropic = true;
184  resampleResolution = us::any_cast<float>(parsedArgs["fixed-isotropic"]);
185  }
186 }
187 
188 void mitk::cl::GlobalImageFeaturesParameter::ParseGlobalFeatureParameter(std::map<std::string, us::Any> &parsedArgs)
189 {
190  //
191  // Parse parameters that control how the input mask is adapted to the input image
192  //
195  defineGlobalNumberOfBins = false;
196  encodeParameter = false;
197  if (parsedArgs.count("minimum-intensity"))
198  {
200  globalMinimumIntensity = us::any_cast<float>(parsedArgs["minimum-intensity"]);
201  }
202  if (parsedArgs.count("maximum-intensity"))
203  {
205  globalMaximumIntensity = us::any_cast<float>(parsedArgs["maximum-intensity"]);
206  }
207  if (parsedArgs.count("bins"))
208  {
210  globalNumberOfBins = us::any_cast<int>(parsedArgs["bins"]);
211  }
212  if (parsedArgs.count("encode-parameter-in-name"))
213  {
214  encodeParameter = true;
215  }
216 }
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)