Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
CopyGeometry.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 <mitkImageCast.h>
18 #include <mitkImage.h>
19 #include <mitkIOUtil.h>
20 #include "mitkCommandLineParser.h"
21 
22 using namespace mitk;
23 using namespace std;
24 
28 int main(int argc, char* argv[])
29 {
30  mitkCommandLineParser parser;
31 
32  parser.setTitle("Copy Geometry");
33  parser.setCategory("Preprocessing Tools");
34  parser.setDescription("Copies transformation matrix of one image to another");
35  parser.setContributor("MBI");
36 
37  parser.setArgumentPrefix("--", "-");
38  parser.addArgument("in", "i", mitkCommandLineParser::InputFile, "Input:", "input image", us::Any(), false);
39  parser.addArgument("ref", "r", mitkCommandLineParser::InputFile, "Reference:", "reference image", us::Any(), false);
40  parser.addArgument("alignCentroid", "a", mitkCommandLineParser::Bool, "align centroids", "align centroids", us::Any(), true);
41  parser.addArgument("out", "o", mitkCommandLineParser::OutputFile, "Output:", "output image", us::Any(), false);
42 
43  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
44  if (parsedArgs.size()==0)
45  return EXIT_FAILURE;
46 
47  // mandatory arguments
48  string imageName = us::any_cast<string>(parsedArgs["in"]);
49  string refImage = us::any_cast<string>(parsedArgs["ref"]);
50  string outImage = us::any_cast<string>(parsedArgs["out"]);
51 
52  bool originOnly = false;
53 
54  // Show a help message
55  if ( parsedArgs.count("alignCentroid") || parsedArgs.count("a"))
56  {
57  originOnly = true;
58  }
59 
60  try
61  {
62  Image::Pointer source = mitk::IOUtil::LoadImage(refImage);
63  Image::Pointer target = mitk::IOUtil::LoadImage(imageName);
64  if (originOnly)
65  {
66  // Calculate correction to align centroids
67  double c[3];
68  c[0] = source->GetGeometry()->GetOrigin()[0]
69  + source->GetGeometry()->GetExtent(0)/2.0
70  - target->GetGeometry()->GetOrigin()[0]
71  - target->GetGeometry()->GetExtent(0)/2.0;
72 
73  c[1] = source->GetGeometry()->GetOrigin()[1]
74  + source->GetGeometry()->GetExtent(1)/2.0
75  - target->GetGeometry()->GetOrigin()[1]
76  - target->GetGeometry()->GetExtent(1)/2.0;
77 
78 
79  c[2] = source->GetGeometry()->GetOrigin()[2]
80  + source->GetGeometry()->GetExtent(2)/2.0
81  - target->GetGeometry()->GetOrigin()[2]
82  - target->GetGeometry()->GetExtent(2)/2.0;
83 
84  double newOrigin[3];
85  newOrigin[0] = target->GetGeometry()->GetOrigin()[0] +c[0];
86  newOrigin[1] = target->GetGeometry()->GetOrigin()[1] +c[1];
87  newOrigin[2] = target->GetGeometry()->GetOrigin()[2] +c[2];
88 
89  target->GetGeometry()->SetOrigin(newOrigin);
90  }
91  else
92  {
93  mitk::BaseGeometry* s_geom = source->GetGeometry();
94  mitk::BaseGeometry* t_geom = target->GetGeometry();
95 
97  target->SetGeometry(t_geom);
98  }
99  mitk::IOUtil::SaveImage(target, outImage);
100  }
101  catch (itk::ExceptionObject e)
102  {
103  std::cout << e;
104  return EXIT_FAILURE;
105  }
106  catch (std::exception e)
107  {
108  std::cout << e.what();
109  return EXIT_FAILURE;
110  }
111  catch (...)
112  {
113  std::cout << "ERROR!?!";
114  return EXIT_FAILURE;
115  }
116  return EXIT_SUCCESS;
117 }
void SetIndexToWorldTransform(mitk::AffineTransform3D *transform)
void setContributor(std::string contributor)
STL namespace.
DataCollection - Class to facilitate loading/accessing structured data.
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[])
Copies transformation matrix of one image to another.
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
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
BaseGeometry Describes the geometry of a data object.
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.