Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
PAResampleCropTool.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 
13 #include <mitkCommon.h>
14 #include <chrono>
15 #include <mitkIOUtil.h>
16 #include <mitkCommandLineParser.h>
17 #include <mitkException.h>
18 
22 
23 #include <itksys/SystemTools.hxx>
24 #include <tinyxml\tinyxml.h>
25 
26 struct InputParameters
27 {
28  mitk::Image::Pointer inputImage;
29  std::string outputFilename;
30  bool verbose;
31  std::string settingsFile;
32 };
33 
34 struct CropSettings
35 {
36  unsigned int dimX;
37  unsigned int dimY;
38  unsigned int cutAbove;
39 };
40 
41 struct ResamplingSettings
42 {
43  double spacing[3];
44 };
45 
46 
47 InputParameters parseInput(int argc, char* argv[])
48 {
49  mitkCommandLineParser parser;
50  parser.setCategory("MITK-Photoacoustics");
51  parser.setTitle("Mitk Photoacoustics Resample and Crop Tool");
52  parser.setDescription("Reads a nrrd file as an input and crops and resamples it as set with the parameters defined in an additionally provided xml file.");
53  parser.setContributor("Computer Assisted Medical Interventions, DKFZ");
54 
55  parser.setArgumentPrefix("--", "-");
56 
57  parser.beginGroup("Required parameters");
58  parser.addArgument(
59  "inputImage", "i", mitkCommandLineParser::Image,
60  "Input image (mitk::Image)", "input image (.nrrd file)",
61  us::Any(), false, false, false, mitkCommandLineParser::Input);
62  parser.addArgument(
63  "output", "o", mitkCommandLineParser::File,
64  "Output filename", "output image (.nrrd file)",
65  us::Any(), false, false, false, mitkCommandLineParser::Output);
66  parser.addArgument(
67  "settings", "s", mitkCommandLineParser::String,
68  "settings file", "file containing specifications (.xml file)",
69  us::Any(), false);
70  parser.endGroup();
71 
72  parser.beginGroup("Optional parameters");
73  parser.addArgument(
74  "verbose", "v", mitkCommandLineParser::Bool,
75  "Verbose Output", "Whether to produce verbose, or rather debug output. (default: false)");
76  parser.endGroup();
77 
78  InputParameters input;
79 
80  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
81  if (parsedArgs.size() == 0)
82  exit(-1);
83 
84  input.verbose = (bool)parsedArgs.count("verbose");
85  MITK_INFO(input.verbose) << "### VERBOSE OUTPUT ENABLED ###";
86 
87  if (parsedArgs.count("inputImage"))
88  {
89  MITK_INFO(input.verbose) << "Reading input image...";
90  input.inputImage = mitk::IOUtil::Load<mitk::Image>(us::any_cast<std::string>(parsedArgs["inputImage"]));
91  MITK_INFO(input.verbose) << "Reading input image...[Done]";
92  }
93  else
94  mitkThrow() << "No input image given.";
95 
96  if (parsedArgs.count("output"))
97  input.outputFilename = us::any_cast<std::string>(parsedArgs["output"]);
98  else
99  mitkThrow() << "No output image path given..";
100 
101  if (parsedArgs.count("settings"))
102  input.settingsFile = us::any_cast<std::string>(parsedArgs["settings"]);
103  else
104  mitkThrow() << "No settings image path given..";
105 
106  return input;
107 }
108 
109 void ParseXML(std::string xmlFile, InputParameters input, CropSettings& cropSet, ResamplingSettings& resSet)
110 {
111  MITK_INFO << "Loading configuration File \"" << xmlFile << "\"";
112  TiXmlDocument doc(xmlFile);
113  if (!doc.LoadFile())
114  mitkThrow() << "Failed to load settings file \"" << xmlFile << "\" Error: " << doc.ErrorDesc();
115 
116  TiXmlElement* root = doc.FirstChildElement();
117  if (root == NULL)
118  {
119  mitkThrow() << "Failed to load file: No root element.";
120  doc.Clear();
121  }
122  for (TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
123  {
124  std::string elemName = elem->Value();
125  if (elemName == "ResampleCrop")
126  {
127  cropSet.dimX = std::stoi(elem->Attribute("dimX"));
128  cropSet.dimY = std::stoi(elem->Attribute("dimY"));
129  cropSet.cutAbove = std::stoi(elem->Attribute("cutAbove"));
130  resSet.spacing[0] = std::stod(elem->Attribute("spacingX"));
131  resSet.spacing[1] = std::stod(elem->Attribute("spacingY"));
132  resSet.spacing[2] = std::stod(elem->Attribute("spacingZ"));
133  }
134  }
135 }
136 
137 int main(int argc, char * argv[])
138 {
139  auto input = parseInput(argc, argv);
140 
141  CropSettings cropSettings{ 0,0,0 };
142  ResamplingSettings resSettings{ 0 };
143 
144  MITK_INFO << "Parsing settings XML...";
145  try
146  {
147  ParseXML(input.settingsFile, input, cropSettings, resSettings);
148  }
149  catch (mitk::Exception e)
150  {
151  MITK_INFO << e;
152  return -1;
153  }
154 
155  MITK_INFO << "Parsing settings XML...[Done]";
156 
157  MITK_INFO(input.verbose) << "Processing input image...";
158 
159  mitk::PhotoacousticFilterService::Pointer m_FilterService = mitk::PhotoacousticFilterService::New();
160 
161  mitk::Image::Pointer output = input.inputImage;
162  MITK_INFO(input.verbose) << "Resampling input image...";
163  MITK_INFO << resSettings.spacing[0];
164  output = m_FilterService->ApplyResampling(output, resSettings.spacing);
165  MITK_INFO(input.verbose) << "Resampling input image...[Done]";
166 
167  if (output->GetDimension(0) != cropSettings.dimX)
168  {
169  double outputDim[] = {(double)cropSettings.dimX, (double)output->GetDimension(1), (double)output->GetDimension(2)};
170  output = m_FilterService->ApplyResamplingToDim(output, outputDim);
171  }
172 
173  int err = 0;
174  int below = output->GetDimension(1) - cropSettings.dimY - cropSettings.cutAbove;
175  if (below < 0)
176  {
177  MITK_INFO(input.verbose) << "Extending input image...";
178  output = m_FilterService->ExtendImage(output, 0, cropSettings.dimY);
179  MITK_INFO(input.verbose) << "Extending input image...[Done]";
180  }
181  else
182  {
183  MITK_INFO(input.verbose) << "Cropping input image...";
184  output = m_FilterService->ApplyCropping(output, cropSettings.cutAbove, below, 0, 0, 0, 0, &err);
185  MITK_INFO(input.verbose) << "Cropping input image...[Done]";
186  }
187 
188 
189  MITK_INFO(input.verbose) << "Saving image...";
190  mitk::IOUtil::Save(output, input.outputFilename);
191  MITK_INFO(input.verbose) << "Saving image...[Done]";
192 
193  MITK_INFO(input.verbose) << "Processing input image...[Done]";
194 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
void setContributor(std::string contributor)
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)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
std::string outputFilename
Definition: MitkMCxyz.cpp:626
void ParseXML(std::string xmlFile, InputParameters input, CropSettings &cropSet, ResamplingSettings &resSet)
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
#define mitkThrow()
bool verbose(false)
Definition: usAny.h:163
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
int main(int argc, char *argv[])
InputParameters parseInput(int argc, char *argv[])
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
void setTitle(std::string title)
void setDescription(std::string description)
void beginGroup(const std::string &description)