Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
CLMatchPointReg.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 
21 // MatchPoint
22 #include <mapRegistrationAlgorithmInterface.h>
23 #include <mapAlgorithmEvents.h>
24 #include <mapAlgorithmWrapperEvent.h>
25 #include <mapExceptionObjectMacros.h>
26 #include <mapDeploymentDLLDirectoryBrowser.h>
27 #include <mapImageRegistrationAlgorithmInterface.h>
28 #include <mapPointSetRegistrationAlgorithmInterface.h>
29 #include <mapMaskedRegistrationAlgorithmInterface.h>
30 #include <mapConvert.h>
31 #include <mapDeploymentDLLAccess.h>
32 #include <mapRegistrationBase.h>
33 
34 #include <mitkAlgorithmHelper.h>
35 // Qt
36 #include <QDir>
37 #include <QFileInfo>
39 //#include <QApplication>
40 #include <QStringList>
41 #include <QCoreApplication>
42 #include <QmitkRegistrationJob.h>
43 #include <mitkImageMappingHelper.h>
44 
45 
46 int main(int argc, char* argv[])
47 {
48  mitkCommandLineParser parser;
49 
50  parser.setTitle("Dicom Loader");
51  parser.setCategory("Preprocessing Tools");
52  parser.setDescription("");
53  parser.setContributor("German Cancer Research Center (DKFZ)");
54 
55  parser.setArgumentPrefix("--","-");
56  // Add command line argument names
57  parser.addArgument("help", "h",mitkCommandLineParser::Bool, "Help:", "Show this help text");
58  parser.addArgument("moving", "m", mitkCommandLineParser::Directory, "Input folder:", "Input folder", us::Any(), false, false, false, mitkCommandLineParser::Input);
59  parser.addArgument("fixed", "f", mitkCommandLineParser::Directory, "Input folder:", "Input folder", us::Any(), false, false, false, mitkCommandLineParser::Input);
60  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output file:", "Output file", us::Any(), false, false, false, mitkCommandLineParser::Output);
61  parser.addArgument("reader", "r", mitkCommandLineParser::Int, "Reader ID", "Reader Name", us::Any(), false);
62  parser.addArgument("interpolation", "interp", mitkCommandLineParser::Int, "Reader ID", "Reader Name", us::Any(), false);
63 
64  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
65 
66  QFileInfo fi(argv[0]);
67  map::deployment::DLLDirectoryBrowser::Pointer browser = map::deployment::DLLDirectoryBrowser::New();
68  browser->addDLLSearchLocation(QDir::homePath().toStdString());
69  browser->addDLLSearchLocation(QDir::currentPath().toStdString());
70  browser->addDLLSearchLocation(fi.canonicalPath().toStdString());
71  browser->update();
72  auto dllList = browser->getLibraryInfos();
73 
74  int id = 0;
75  std::cout << std::endl << " --- Algorithm List --- " << std::endl;
76  for (auto info : dllList)
77  {
78  std::cout << "Algorithm ID " << id << ": " << info->getAlgorithmUID().getName() << std::endl;
79  ++id;
80  }
81  std::cout << std::endl << " --- Interpolation List --- " << std::endl;
82  std::cout << "Interpolation ID 0: Linear Interpolation " << std::endl;
83  std::cout << "Interpolation ID 1: Nearest Neighbour" << std::endl;
84  std::cout << "Interpolation ID 2: BSpline 3D" << std::endl << std::endl;
85 
87 
88  if (parsedArgs.size()==0)
89  return EXIT_FAILURE;
90 
91  // Show a help message
92  if ( parsedArgs.count("help") || parsedArgs.count("h"))
93  {
94  std::cout << parser.helpText();
95  return EXIT_SUCCESS;
96  }
97 
98 
99  std::string movingFile = us::any_cast<std::string>(parsedArgs["moving"]);
100  std::string fixedFile = us::any_cast<std::string>(parsedArgs["fixed"]);
101  int selectedAlgorithm = us::any_cast<int>(parsedArgs["reader"]);
102  std::string outputPath = us::any_cast<std::string>(parsedArgs["output"]);
103 
104  if (parsedArgs.count("interpolation"))
105  {
106  switch (us::any_cast<int>(parsedArgs["interpolation"]))
107  {
108  case 0:
109  interpolationMode = mitk::ImageMappingInterpolator::Linear;
110  break;
111  case 1:
113  break;
114  case 2:
115  interpolationMode = mitk::ImageMappingInterpolator::BSpline_3;
116  break;
117  default:
118  interpolationMode = mitk::ImageMappingInterpolator::Linear;
119  }
120  }
121 
122 
123  mitk::Image::Pointer movingImage = mitk::IOUtil::Load<mitk::Image>(movingFile);
124  mitk::Image::Pointer fixedImage = mitk::IOUtil::Load<mitk::Image>(fixedFile);
125 
126  auto dllInfo = dllList[selectedAlgorithm];
127 
128  if (!dllInfo)
129  {
130  MITK_ERROR << "No valid algorithm is selected. Cannot load algorithm. ABORTING.";
131  return -1;
132  }
133 
134  ::map::deployment::DLLHandle::Pointer tempDLLHandle = ::map::deployment::openDeploymentDLL(
135  dllInfo->getLibraryFilePath());
136  ::map::algorithm::RegistrationAlgorithmBase::Pointer tempAlgorithm
137  = ::map::deployment::getRegistrationAlgorithm(tempDLLHandle);
138  MITK_INFO << "Well....";
139  if (tempAlgorithm.IsNull())
140  {
141  MITK_ERROR << "Error. Cannot load selected algorithm.";
142  return -2;
143  }
144 
145  mitk::MITKAlgorithmHelper helper(tempAlgorithm);
146  helper.SetData(movingImage, fixedImage);
147  auto registration = helper.GetRegistration();
148  MITK_INFO << "Well....";
149 
150  mitk::Image::Pointer spResultData= mitk::ImageMappingHelper::map(movingImage,
151  registration,
152  false, // Use all Pixels
153  0.0, // Padding Value
154  fixedImage->GetGeometry()->Clone().GetPointer(), // Ref. Geometry
155  false,
156  0, // Error Value
157  interpolationMode // Interpolator Type
158  );
159 
160  MITK_INFO << "Well....";
161  mitk::IOUtil::Save(spResultData, outputPath);
162 
163  return EXIT_SUCCESS;
164 }
MITKAlgorithmHelper.
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
map::core::RegistrationBase::Pointer GetRegistration() const
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
int main(int argc, char *argv[])
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)
static void info(const char *fmt,...)
Definition: svm.cpp:86
void SetData(const mitk::BaseData *moving, const mitk::BaseData *target)
Definition: usAny.h:163
std::string helpText() const
void setCategory(std::string category)
MITKMATCHPOINTREGISTRATION_EXPORT ResultImageType::Pointer map(const InputImageType *input, const RegistrationType *registration, bool throwOnOutOfInputAreaError=false, const double &paddingValue=0, const ResultImageGeometryType *resultGeometry=nullptr, bool throwOnMappingError=true, const double &errorValue=0, mitk::ImageMappingInterpolator::Type interpolatorType=mitk::ImageMappingInterpolator::Linear)
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)