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