Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
SingleImageArithmetic.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 "mitkProperties.h"
14 
15 #include "mitkCommandLineParser.h"
16 #include "mitkIOUtil.h"
17 
19 
20 static bool ConvertToBool(std::map<std::string, us::Any> &data, std::string name)
21 {
22  if (!data.count(name))
23  {
24  return false;
25  }
26  try {
27  return us::any_cast<bool>(data[name]);
28  }
29  catch ( const us::BadAnyCastException & )
30  {
31  return false;
32  }
33 }
34 
35 
36 int main(int argc, char* argv[])
37 {
38  mitkCommandLineParser parser;
39 
40  parser.setTitle("File Converter");
41  parser.setCategory("Basic Image Processing");
42  parser.setDescription("");
43  parser.setContributor("German Cancer Research Center (DKFZ)");
44 
45  parser.setArgumentPrefix("--","-");
46  // Add command line argument names
47  parser.addArgument("help", "h",mitkCommandLineParser::Bool, "Help:", "Show this help text");
48  parser.addArgument("input", "i", mitkCommandLineParser::File, "Input file:", "Input File",us::Any(),false, false, false, mitkCommandLineParser::Input);
49  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output file:", "Output file", us::Any(), false, false, false, mitkCommandLineParser::Output);
50 
51  parser.addArgument("as-double", "double", mitkCommandLineParser::Bool, "Result as double", "Result as double image type", false, true);
52 
53  parser.addArgument("tan", "tan", mitkCommandLineParser::Bool, "Calculate tan operation", "Calculate tan operation", us::Any(false), true);
54  parser.addArgument("atan", "atan", mitkCommandLineParser::Bool, "Calculate atan operation", "Calculate atan operation", us::Any(false), true);
55  parser.addArgument("cos", "cos", mitkCommandLineParser::Bool, "Calculate cos operation", "Calculate cos operation", us::Any(false), true);
56  parser.addArgument("acos", "acos", mitkCommandLineParser::Bool, "Calculate acos operation", "Calculate acos operation", us::Any(false), true);
57  parser.addArgument("sin", "sin", mitkCommandLineParser::Bool, "Calculate sin operation", "Calculate sin operation", us::Any(false), true);
58  parser.addArgument("asin", "asin", mitkCommandLineParser::Bool, "Calculate asin operation", "Calculate asin operation", us::Any(false), true);
59  parser.addArgument("square", "square", mitkCommandLineParser::Bool, "Calculate square operation", "Calculate square operation", us::Any(false), true);
60  parser.addArgument("sqrt", "sqrt", mitkCommandLineParser::Bool, "Calculate sqrt operation", "Calculate sqrt operation", us::Any(false), true);
61  parser.addArgument("abs", "abs", mitkCommandLineParser::Bool, "Calculate abs operation", "Calculate abs operation", us::Any(false), true);
62  parser.addArgument("exp", "exp", mitkCommandLineParser::Bool, "Calculate exp operation", "Calculate exp operation", us::Any(false), true);
63  parser.addArgument("expneg", "expneg", mitkCommandLineParser::Bool, "Calculate expneg operation", "Calculate expneg operation", us::Any(false), true);
64  parser.addArgument("log10", "log10", mitkCommandLineParser::Bool, "Calculate log10 operation", "Calculate log10 operation", us::Any(false), true);
65 
66  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
67 
68  if (parsedArgs.size()==0)
69  return EXIT_FAILURE;
70 
71  // Show a help message
72  if ( parsedArgs.count("help") || parsedArgs.count("h"))
73  {
74  std::cout << parser.helpText();
75  return EXIT_SUCCESS;
76  }
77 
78  std::string inputFilename = us::any_cast<std::string>(parsedArgs["input"]);
79  std::string outputFilename = us::any_cast<std::string>(parsedArgs["output"]);
80 
81  auto nodes = mitk::IOUtil::Load(inputFilename);
82  if (nodes.size() == 0)
83  {
84  MITK_INFO << "No Image Loaded";
85  return 0;
86  }
87  mitk::Image::Pointer image = dynamic_cast<mitk::Image*>(nodes[0].GetPointer());
88 
89  if (image.IsNull())
90  {
91  MITK_INFO << "Loaded data is not of type image";
92  return 0;
93  }
94 
95  bool resultAsDouble = ConvertToBool(parsedArgs, "as-double");
96  MITK_INFO << "Output image as double: " << resultAsDouble;
97 
98  mitk::Image::Pointer tmpImage = image->Clone();
99 
100  if (ConvertToBool(parsedArgs, "tan"))
101  {
102  MITK_INFO << " Start Doing Operation: TAN()";
103  tmpImage = mitk::ArithmeticOperation::Tan(tmpImage, resultAsDouble);
104  }
105  if (ConvertToBool(parsedArgs, "atan"))
106  {
107  MITK_INFO << " Start Doing Operation: ATAN()";
108  tmpImage = mitk::ArithmeticOperation::Atan(tmpImage, resultAsDouble);
109  }
110  if (ConvertToBool(parsedArgs, "cos"))
111  {
112  MITK_INFO << " Start Doing Operation: COS()";
113  tmpImage = mitk::ArithmeticOperation::Cos(tmpImage, resultAsDouble);
114  }
115  if (ConvertToBool(parsedArgs, "acos"))
116  {
117  MITK_INFO << " Start Doing Operation: ACOS()";
118  tmpImage = mitk::ArithmeticOperation::Acos(tmpImage, resultAsDouble);
119  }
120  if (ConvertToBool(parsedArgs, "sin"))
121  {
122  MITK_INFO << " Start Doing Operation: SIN()";
123  tmpImage = mitk::ArithmeticOperation::Sin(tmpImage, resultAsDouble);
124  }
125  if (ConvertToBool(parsedArgs, "asin"))
126  {
127  MITK_INFO << " Start Doing Operation: ASIN()";
128  tmpImage = mitk::ArithmeticOperation::Asin(tmpImage, resultAsDouble);
129  }
130  if (ConvertToBool(parsedArgs, "square"))
131  {
132  MITK_INFO << " Start Doing Operation: SQUARE()";
133  tmpImage = mitk::ArithmeticOperation::Square(tmpImage, resultAsDouble);
134  }
135  if (ConvertToBool(parsedArgs, "sqrt"))
136  {
137  MITK_INFO << " Start Doing Operation: SQRT()";
138  tmpImage = mitk::ArithmeticOperation::Sqrt(tmpImage, resultAsDouble);
139  }
140  if (ConvertToBool(parsedArgs, "abs"))
141  {
142  MITK_INFO << " Start Doing Operation: ABS()";
143  tmpImage = mitk::ArithmeticOperation::Abs(tmpImage, resultAsDouble);
144  }
145  if (ConvertToBool(parsedArgs, "exp"))
146  {
147  MITK_INFO << " Start Doing Operation: EXP()";
148  tmpImage = mitk::ArithmeticOperation::Exp(tmpImage, resultAsDouble);
149  }
150  if (ConvertToBool(parsedArgs, "expneg"))
151  {
152  MITK_INFO << " Start Doing Operation: EXPNEG()";
153  tmpImage = mitk::ArithmeticOperation::ExpNeg(tmpImage, resultAsDouble);
154  }
155  if (ConvertToBool(parsedArgs, "log10"))
156  {
157  MITK_INFO << " Start Doing Operation: LOG10()";
158  tmpImage = mitk::ArithmeticOperation::Log10(tmpImage, resultAsDouble);
159  }
160 
161  mitk::IOUtil::Save(tmpImage, outputFilename);
162 
163  return EXIT_SUCCESS;
164 }
static bool ConvertToBool(std::map< std::string, us::Any > &data, std::string name)
#define MITK_INFO
Definition: mitkLogMacros.h:18
static Image::Pointer Abs(Image::Pointer &imageA, bool outputAsDouble=true)
int main(int argc, char *argv[])
std::string inputFilename
Definition: MitkMCxyz.cpp:625
void setContributor(std::string contributor)
static Image::Pointer Sin(Image::Pointer &imageA, bool outputAsDouble=true)
static Image::Pointer Asin(Image::Pointer &imageA, bool outputAsDouble=true)
static Image::Pointer Square(Image::Pointer &imageA, bool outputAsDouble=true)
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)
static Image::Pointer Sqrt(Image::Pointer &imageA, bool outputAsDouble=true)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
static Image::Pointer Log10(Image::Pointer &imageA, bool outputAsDouble=true)
std::string outputFilename
Definition: MitkMCxyz.cpp:626
static Image::Pointer Cos(Image::Pointer &imageA, bool outputAsDouble=true)
static Image::Pointer Acos(Image::Pointer &imageA, bool outputAsDouble=true)
static Image::Pointer Tan(Image::Pointer &imageA, bool outputAsDouble=true)
Image class for storing images.
Definition: mitkImage.h:72
Definition: usAny.h:163
std::string helpText() const
static Image::Pointer ExpNeg(Image::Pointer &imageA, bool outputAsDouble=true)
void setCategory(std::string category)
mitk::Image::Pointer image
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
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)
static Image::Pointer Atan(Image::Pointer &imageA, bool outputAsDouble=true)
static Image::Pointer Exp(Image::Pointer &imageA, bool outputAsDouble=true)
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:489