Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
PlanarFigureMaskCreatorMiniapp.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 
17 #include "mitkCommandLineParser.h"
18 #include "mitkImage.h"
19 #include "mitkIOUtil.h"
20 #include <iostream>
21 #include <usAny.h>
22 #include <fstream>
24 #include <mitkPlanarFigure.h>
26 
27 
28 int main( int argc, char* argv[] )
29 {
30  mitkCommandLineParser parser;
31 
32  parser.setTitle("Test basic hotspot functionality");
33  parser.setCategory("Preprocessing Tools");
34  parser.setDescription("");
35  parser.setContributor("MBI");
36 
37  parser.setArgumentPrefix("--", "-");
38  parser.addArgument("help", "h", mitkCommandLineParser::String, "Help:", "Show this help text");
39  parser.addArgument("input", "i", mitkCommandLineParser::InputFile, "Input:", "input image", us::Any(),false);
40  parser.addArgument("planarfigure", "p", mitkCommandLineParser::InputFile, "PlanarFigure:", "mask image / roi image denotin area on which statistics are calculated", us::Any(),false);
41  parser.addArgument("out", "o", mitkCommandLineParser::OutputFile, "Output", "output file (default: hotspotMiniApp_output.nrrd)", us::Any());
42 
43  std::cout << "test...." << std::endl;
44 
45  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
46  std::cout << "parsedArgs.size()= " << parsedArgs.size() << std::endl;
47  if (parsedArgs.size()==0 || parsedArgs.count("help") || parsedArgs.count("h"))
48  {
49 // std::cout << "\n\n MiniApp Description: \nCalculates and saves the hotspot of an image." << endl;
50 // std::cout << "Output is written to the designated output file" << endl;
51 // std::cout << parser.helpText();
52 // return EXIT_SUCCESS;
53  }
54 
55  // Parameters:
56  std::string inputImageFile;
57  if (!parsedArgs.count("i") && !parsedArgs.count("input"))
58  {
59  inputImageFile = "/home/fabian/MITK/MITK_platform_project/bin/MITK-superbuild/MITK-Data/Pic3D.nrrd";
60  }
61  else
62  {
63  inputImageFile = us::any_cast<std::string>(parsedArgs["input"]);
64  }
65 
66  std::string inputPFFile;
67  if (parsedArgs.count("planarfigure") || parsedArgs.count("p"))
68  {
69  inputPFFile = us::any_cast<std::string>(parsedArgs["planarfigure"]);
70  }
71  else
72  {
73  inputPFFile = "/home/fabian/MITK/MITK_platform_project/bin/MITK-superbuild/MITK-Data/Pic3D_rectangle.pf";
74  }
75 
76 
77  mitk::PlanarFigure::Pointer planarFigure;
78  try
79  {
80  std::vector<mitk::BaseData::Pointer> loadedObjects;
81  // try except
82  loadedObjects = mitk::IOUtil::Load(inputPFFile);
83  mitk::BaseData::Pointer pf = loadedObjects[0];
84  planarFigure = dynamic_cast<mitk::PlanarFigure*>(pf.GetPointer());
85  if (planarFigure.IsNull())
86  {
87  MITK_ERROR << "something went wrong";
88  return -1;
89  }
90  }
91  catch (const itk::ExceptionObject& e)
92  {
93  MITK_ERROR << "Failed: " << e.what();
94  return -1;
95  }
96 
97  std::string outFile;
98  if (parsedArgs.count("out") || parsedArgs.count("o") )
99  outFile = us::any_cast<std::string>(parsedArgs["out"]);
100  else
101  outFile = "planarFigureExtraction_output.nrrd";
102 
103  // Load image and mask
104  mitk::Image::Pointer inputImage = mitk::IOUtil::LoadImage(inputImageFile);
105 
106  // Calculate statistics
108  planarFigMaskExtr->SetPlanarFigure(planarFigure);
109  planarFigMaskExtr->SetInputImage(inputImage);
110 
111  mitk::Image::Pointer outImage;
112  try
113  {
114  outImage = planarFigMaskExtr->GetMask();
115  }
116  catch( const itk::ExceptionObject& e)
117  {
118  MITK_ERROR << "Failed - ITK Exception:" << e.what();
119  return -1;
120  }
121 
122  mitk::BaseGeometry *imageGeo = outImage->GetGeometry();
123  std::cout << "origin: " << imageGeo->GetOrigin()[0] << " " << imageGeo->GetOrigin()[1] << " " << imageGeo->GetOrigin()[2] << std::endl;
124  if (outImage != nullptr)
125  {
126  mitk::IOUtil::SaveImage(outImage, outFile);
127  }
128 
129 
130  return EXIT_SUCCESS;
131 }
const Point3D GetOrigin() const
Get the origin, e.g. the upper-left corner of the plane.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
static bool SaveImage(mitk::Image::Pointer image, const std::string &path)
SaveImage Convenience method to save an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:870
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)
Base-class for geometric planar (2D) figures, such as lines, circles, rectangles, polygons...
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:483
void setTitle(std::string title)
int main(int argc, char *argv[])
void setDescription(std::string description)
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:597
BaseGeometry Describes the geometry of a data object.