Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
CLImageCropper.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 "mitkIOUtil.h"
20 #include <mitkCuboid.h>
21 
22 int main(int argc, char* argv[])
23 {
24  mitkCommandLineParser parser;
25 
26  parser.setTitle("Dicom Loader");
27  parser.setCategory("Preprocessing Tools");
28  parser.setDescription("");
29  parser.setContributor("MBI");
30 
31  parser.setArgumentPrefix("--","-");
32  // Add command line argument names
33  parser.addArgument("help", "h",mitkCommandLineParser::Bool, "Help:", "Show this help text");
34  parser.addArgument("reference", "r", mitkCommandLineParser::InputDirectory, "Input file:", "Input file",us::Any(),false);
35  parser.addArgument("input", "i", mitkCommandLineParser::InputDirectory, "Input file:", "Input file",us::Any(),false);
36  parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "Output file:", "Output file",us::Any(),false);
37 
38  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
39 
40  if (parsedArgs.size()==0)
41  return EXIT_FAILURE;
42 
43  // Show a help message
44  if ( parsedArgs.count("help") || parsedArgs.count("h"))
45  {
46  std::cout << parser.helpText();
47  return EXIT_SUCCESS;
48  }
49 
50  std::string referenceName = us::any_cast<std::string>(parsedArgs["reference"]);
51  std::string inputName = us::any_cast<std::string>(parsedArgs["input"]);
52  std::string outputName = us::any_cast<std::string>(parsedArgs["output"]);
53 
54  mitk::Image::Pointer imageToCrop = mitk::IOUtil::LoadImage(inputName);
55  mitk::Image::Pointer referenceImage = mitk::IOUtil::LoadImage(referenceName);
57  mitk::BoundingObject::Pointer boundingObject = (mitk::Cuboid::New()).GetPointer();
58  boundingObject->FitGeometry(referenceImage->GetGeometry());
59 
60  cutter->SetBoundingObject(boundingObject);
61  cutter->SetInput(imageToCrop);
62  cutter->Update();
63  mitk::IOUtil::SaveImage(cutter->GetOutput(), outputName);
64 
65  return EXIT_SUCCESS;
66 }
static Pointer New()
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
int main(int argc, char *argv[])
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
std::string helpText() const
void setTitle(std::string title)
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