Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
DiffusionIndices.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 <itkExceptionObject.h>
19 #include <itkImageFileWriter.h>
20 #include <mitkQBallImage.h>
23 #include <mitkTensorImage.h>
24 #include "mitkCommandLineParser.h"
25 #include <boost/algorithm/string.hpp>
26 #include <itksys/SystemTools.hxx>
27 #include <itkMultiThreader.h>
28 #include <mitkIOUtil.h>
29 
30 using namespace std;
31 
35 int main(int argc, char* argv[])
36 {
37  mitkCommandLineParser parser;
38 
39  parser.setTitle("Diffusion Indices");
40  parser.setCategory("Diffusion Related Measures");
41  parser.setDescription("Computes requested diffusion related measures");
42  parser.setContributor("MBI");
43 
44  parser.setArgumentPrefix("--", "-");
45  parser.addArgument("input", "i", mitkCommandLineParser::InputFile, "Input:", "input image (tensor, Q-ball or FSL/MRTrix SH-coefficient image)", us::Any(), false);
46  parser.addArgument("index", "idx", mitkCommandLineParser::String, "Index:", "index (fa, gfa, ra, ad, rd, ca, l2, l3, md)", us::Any(), false);
47  parser.addArgument("outFile", "o", mitkCommandLineParser::OutputFile, "Output:", "output file", us::Any(), false);
48 
49  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
50  if (parsedArgs.size()==0)
51  return EXIT_FAILURE;
52 
53  string inFileName = us::any_cast<string>(parsedArgs["input"]);
54  string index = us::any_cast<string>(parsedArgs["index"]);
55  string outFileName = us::any_cast<string>(parsedArgs["outFile"]);
56 
57  string ext = itksys::SystemTools::GetFilenameLastExtension(outFileName);
58  if (ext.empty())
59  outFileName += ".nrrd";
60 
61  try
62  {
63  // load input image
64  std::vector<mitk::BaseData::Pointer> infile = mitk::IOUtil::Load( inFileName );
65 
66  if( boost::algorithm::ends_with(inFileName, ".qbi") && index=="gfa" )
67  {
68  typedef itk::Vector<float, QBALL_ODFSIZE> OdfVectorType;
69  typedef itk::Image<OdfVectorType,3> ItkQballImageType;
70  mitk::QBallImage::Pointer mitkQballImage = dynamic_cast<mitk::QBallImage*>(infile[0].GetPointer());
72  mitk::CastToItkImage(mitkQballImage, itk_qbi);
73 
74 
77  gfaFilter->SetInput(itk_qbi);
78  gfaFilter->SetComputationMethod(GfaFilterType::GFA_STANDARD);
79  gfaFilter->Update();
80 
81  itk::ImageFileWriter< itk::Image<float,3> >::Pointer fileWriter = itk::ImageFileWriter< itk::Image<float,3> >::New();
82  fileWriter->SetInput(gfaFilter->GetOutput());
83  fileWriter->SetFileName(outFileName);
84  fileWriter->Update();
85  }
86  else if( boost::algorithm::ends_with(inFileName, ".dti") )
87  {
88  typedef itk::Image< itk::DiffusionTensor3D<float>, 3 > ItkTensorImage;
89  mitk::TensorImage::Pointer mitkTensorImage = dynamic_cast<mitk::TensorImage*>(infile[0].GetPointer());
91  mitk::CastToItkImage(mitkTensorImage, itk_dti);
92 
93  typedef itk::TensorDerivedMeasurementsFilter<float> MeasurementsType;
94  MeasurementsType::Pointer measurementsCalculator = MeasurementsType::New();
95  measurementsCalculator->SetInput(itk_dti.GetPointer() );
96 
97  if(index=="fa")
98  measurementsCalculator->SetMeasure(MeasurementsType::FA);
99  else if(index=="ra")
100  measurementsCalculator->SetMeasure(MeasurementsType::RA);
101  else if(index=="ad")
102  measurementsCalculator->SetMeasure(MeasurementsType::AD);
103  else if(index=="rd")
104  measurementsCalculator->SetMeasure(MeasurementsType::RD);
105  else if(index=="ca")
106  measurementsCalculator->SetMeasure(MeasurementsType::CA);
107  else if(index=="l2")
108  measurementsCalculator->SetMeasure(MeasurementsType::L2);
109  else if(index=="l3")
110  measurementsCalculator->SetMeasure(MeasurementsType::L3);
111  else if(index=="md")
112  measurementsCalculator->SetMeasure(MeasurementsType::MD);
113  else
114  {
115  MITK_WARN << "No valid diffusion index for input image (tensor image) defined";
116  return EXIT_FAILURE;
117  }
118 
119  measurementsCalculator->Update();
120 
121  itk::ImageFileWriter< itk::Image<float,3> >::Pointer fileWriter = itk::ImageFileWriter< itk::Image<float,3> >::New();
122  fileWriter->SetInput(measurementsCalculator->GetOutput());
123  fileWriter->SetFileName(outFileName);
124  fileWriter->Update();
125  }
126  else
127  std::cout << "Diffusion index " << index << " not supported for supplied file type.";
128  }
129  catch (itk::ExceptionObject e)
130  {
131  std::cout << e;
132  return EXIT_FAILURE;
133  }
134  catch (std::exception e)
135  {
136  std::cout << e.what();
137  return EXIT_FAILURE;
138  }
139  catch (...)
140  {
141  std::cout << "ERROR!?!";
142  return EXIT_FAILURE;
143  }
144  return EXIT_SUCCESS;
145 }
itk::SmartPointer< Self > Pointer
void setContributor(std::string contributor)
STL namespace.
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
this class encapsulates qball images
#define MITK_WARN
Definition: mitkLogMacros.h:23
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
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
this class encapsulates tensor images
itk::Image< itk::DiffusionTensor3D< float >, 3 > ItkTensorImage
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:483
int main(int argc, char *argv[])
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.