Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
ExportShImage.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 <mitkBaseData.h>
18 #include <mitkImageCast.h>
19 #include <mitkImageToItk.h>
20 #include <metaCommand.h>
21 #include "mitkCommandLineParser.h"
22 #include <usAny.h>
23 #include <itkImageFileWriter.h>
24 #include <itkImageFileReader.h>
25 #include <mitkIOUtil.h>
26 #include <boost/lexical_cast.hpp>
28 #include <itkFlipImageFilter.h>
29 #include <mitkIOUtil.h>
30 #include <mitkITKImageImport.h>
31 
32 #define _USE_MATH_DEFINES
33 #include <math.h>
34 
35 using namespace std;
36 
40 template<int shOrder>
41 int StartShConversion(int argc, char* argv[])
42 {
43  mitkCommandLineParser parser;
44 
45  parser.setTitle("Export SH Image");
46  parser.setCategory("Preprocessing Tools");
47  parser.setDescription("Convert MITK spherical harmonics image type to MRtrix spherical harmonics image");
48  parser.setContributor("MBI");
49 
50  parser.setArgumentPrefix("--", "-");
51  parser.addArgument("input", "i", mitkCommandLineParser::InputFile, "Input:", "MITK SH image", us::Any(), false);
52  parser.addArgument("output", "o", mitkCommandLineParser::InputFile, "Output", "MRtrix SH image", us::Any(), false);
53  parser.addArgument("shOrder", "sh", mitkCommandLineParser::Int, "SH order:", "spherical harmonics order");
54 
55  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
56  if (parsedArgs.size()==0)
57  return EXIT_FAILURE;
58 
59  string inFile = us::any_cast<string>(parsedArgs["input"]);
60  string outFile = us::any_cast<string>(parsedArgs["output"]);
61 
62  try
63  {
64 
65  typedef itk::Image< float, 4 > OutImageType;
66  typedef itk::Image< itk::Vector< float, (shOrder*shOrder + shOrder + 2)/2 + shOrder >, 3 > InputImageType;
67 
68  typename InputImageType::Pointer itkInImage = InputImageType::New();
69  typedef itk::ImageFileReader< InputImageType > ReaderType;
70  typename ReaderType::Pointer reader = ReaderType::New();
71  std::cout << "reading " << inFile;
72  reader->SetFileName(inFile.c_str());
73  reader->Update();
74  itkInImage = reader->GetOutput();
75 
76  // extract directions from fiber bundle
78  filter->SetInputImage(itkInImage);
79  filter->GenerateData();
80  OutImageType::Pointer outImage = filter->GetOutputImage();
81 
82  mitk::Image::Pointer image = mitk::GrabItkImageMemory(outImage.GetPointer());
83  mitk::IOUtil::Save(image, outFile );
84  }
85  catch (itk::ExceptionObject e)
86  {
87  std::cout << e;
88  return EXIT_FAILURE;
89  }
90  catch (std::exception e)
91  {
92  std::cout << e.what();
93  return EXIT_FAILURE;
94  }
95  catch (...)
96  {
97  std::cout << "ERROR!?!";
98  return EXIT_FAILURE;
99  }
100  return EXIT_SUCCESS;
101 }
102 
103 int main(int argc, char* argv[])
104 {
105 
106  mitkCommandLineParser parser;
107  parser.setArgumentPrefix("--", "-");
108  parser.addArgument("input", "i", mitkCommandLineParser::InputFile, "Input image", "MITK SH image", us::Any(), false);
109  parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "Output image", "MRtrix SH image", us::Any(), false);
110  parser.addArgument("shOrder", "sh", mitkCommandLineParser::Int, "Spherical harmonics order", "spherical harmonics order");
111 
112  parser.setCategory("Preprocessing Tools");
113  parser.setTitle("Export SH Image");
114  parser.setDescription(" ");
115  parser.setContributor("MBI");
116 
117  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
118  if (parsedArgs.size()==0)
119  return EXIT_FAILURE;
120 
121  int shOrder = -1;
122  if (parsedArgs.count("shOrder"))
123  shOrder = us::any_cast<int>(parsedArgs["shOrder"]);
124 
125  switch (shOrder)
126  {
127  case 4:
128  return StartShConversion<4>(argc, argv);
129  case 6:
130  return StartShConversion<6>(argc, argv);
131  case 8:
132  return StartShConversion<8>(argc, argv);
133  case 10:
134  return StartShConversion<10>(argc, argv);
135  case 12:
136  return StartShConversion<12>(argc, argv);
137  }
138  return EXIT_FAILURE;
139 }
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
itk::SmartPointer< Self > Pointer
void setContributor(std::string contributor)
STL namespace.
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
int StartShConversion(int argc, char *argv[])
Convert MITK spherical harmonics image type to MRtrix spherical harmonics image.
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
class ITK_EXPORT Image
itk::Image< double, 3 > InputImageType
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)
int main(int argc, char *argv[])
Definition: usAny.h:163
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 itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.