Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
Fiberfox.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 <mitkITKImageImport.h>
19 #include <mitkProperties.h>
20 #include <mitkImage.h>
21 #include <mitkIOUtil.h>
22 #include <mitkFiberBundle.h>
23 #include <mitkFiberfoxParameters.h>
24 #include "mitkCommandLineParser.h"
25 
27 #include <boost/lexical_cast.hpp>
28 
29 using namespace mitk;
30 
35 int main(int argc, char* argv[])
36 {
37  mitkCommandLineParser parser;
38  parser.setTitle("Fiberfox");
39  parser.setCategory("Fiber Tracking and Processing Methods");
40  parser.setContributor("MBI");
41  parser.setDescription("Command line interface to Fiberfox."
42  " Simulate a diffusion-weighted image from a tractogram using the specified parameter file.");
43  parser.setArgumentPrefix("--", "-");
44  parser.addArgument("out", "o", mitkCommandLineParser::OutputFile, "Output root:",
45  "output root", us::Any(), false);
46  parser.addArgument("parameters", "p", mitkCommandLineParser::InputFile, "Parameter file:",
47  "fiberfox parameter file (.ffp)", us::Any(), false);
48  parser.addArgument("input", "i", mitkCommandLineParser::String, "Input:",
49  "Input tractogram or diffusion-weighted image.", us::Any(), false);
50  parser.addArgument("verbose", "v", mitkCommandLineParser::Bool, "Output additional images:",
51  "output volume fraction images etc.", us::Any());
52 
53  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
54  if (parsedArgs.size()==0)
55  {
56  return EXIT_FAILURE;
57  }
58  string outName = us::any_cast<string>(parsedArgs["out"]);
59  string paramName = us::any_cast<string>(parsedArgs["parameters"]);
60 
61  string input="";
62  if (parsedArgs.count("input"))
63  {
64  input = us::any_cast<string>(parsedArgs["input"]);
65  }
66  bool verbose = false;
67  if (parsedArgs.count("verbose"))
68  {
69  verbose = us::any_cast<bool>(parsedArgs["verbose"]);
70  }
71  FiberfoxParameters<double> parameters;
72  parameters.LoadParameters(paramName);
73 
74  // Test if /path/dir is an existing directory:
75  if( itksys::SystemTools::FileIsDirectory( outName ) )
76  {
77  while( *(--(outName.cend())) == '/')
78  {
79  outName.pop_back();
80  }
81  outName = outName + '/';
82  parameters.m_Misc.m_OutputPath = outName;
83  outName = outName + parameters.m_Misc.m_OutputPrefix; // using default m_OutputPrefix as initialized.
84  }
85  else
86  {
87  // outName is NOT an existing directory, so we need to remove all trailing slashes:
88  while( *(--(outName.cend())) == '/')
89  {
90  outName.pop_back();
91  }
92 
93  // now split up the given outName into directory and (prefix of) filename:
94  if( ! itksys::SystemTools::GetFilenamePath( outName ).empty()
95  && itksys::SystemTools::FileIsDirectory(itksys::SystemTools::GetFilenamePath( outName ) ) )
96  {
97  parameters.m_Misc.m_OutputPath = itksys::SystemTools::GetFilenamePath( outName ) + '/';
98  }
99  else
100  {
101  parameters.m_Misc.m_OutputPath = mitk::IOUtil::GetTempPath() + '/';
102  }
103 
104  if( ! itksys::SystemTools::GetFilenameName( outName ).empty() )
105  {
106  parameters.m_Misc.m_OutputPrefix = itksys::SystemTools::GetFilenameName( outName );
107  }
108  else
109  {
110  parameters.m_Misc.m_OutputPrefix = "fiberfox";
111  }
112 
113  outName = parameters.m_Misc.m_OutputPath + parameters.m_Misc.m_OutputPrefix;
114  }
115 
116  // check if log file already exists and avoid overwriting existing files:
117  std::string NameTest = outName;
118  int c = 0;
119  while( itksys::SystemTools::FileExists( outName + ".log" )
121  {
122  outName = NameTest + "_" + boost::lexical_cast<std::string>(c);
123  ++c;
124  }
125 
126  if (verbose)
127  {
128  MITK_DEBUG << outName << ".ffp";
129  parameters.SaveParameters(outName+".ffp");
130  }
131 
132  mitk::BaseData::Pointer inputData = mitk::IOUtil::Load(input)[0];
133 
135 
136  if ( dynamic_cast<mitk::FiberBundle*>(inputData.GetPointer()) ) // simulate dataset from fibers
137  {
138  tractsToDwiFilter->SetFiberBundle(dynamic_cast<mitk::FiberBundle*>(inputData.GetPointer()));
139  }
140  else if ( dynamic_cast<mitk::Image*>(inputData.GetPointer()) ) // add artifacts to existing image
141  {
142  typedef itk::VectorImage< short, 3 > ItkDwiType;
143  mitk::Image::Pointer diffImg = dynamic_cast<mitk::Image*>(inputData.GetPointer());
144  ItkDwiType::Pointer itkVectorImagePointer = ItkDwiType::New();
145  mitk::CastToItkImage(diffImg, itkVectorImagePointer);
146 
147  parameters.m_SignalGen.m_SignalScale = 1;
148  parameters.m_SignalGen.m_ImageRegion = itkVectorImagePointer->GetLargestPossibleRegion();
149  parameters.m_SignalGen.m_ImageSpacing = itkVectorImagePointer->GetSpacing();
150  parameters.m_SignalGen.m_ImageOrigin = itkVectorImagePointer->GetOrigin();
151  parameters.m_SignalGen.m_ImageDirection = itkVectorImagePointer->GetDirection();
152  parameters.m_SignalGen.m_Bvalue = static_cast<mitk::FloatProperty*>
153  (diffImg->GetProperty(mitk::DiffusionPropertyHelper::REFERENCEBVALUEPROPERTYNAME.c_str()).GetPointer() )
154  ->GetValue();
155  parameters.m_SignalGen.SetGradienDirections( static_cast<mitk::GradientDirectionsProperty*>
156  ( diffImg->GetProperty(mitk::DiffusionPropertyHelper::GRADIENTCONTAINERPROPERTYNAME.c_str()).GetPointer() )
157  ->GetGradientDirectionsContainer() );
158 
159  tractsToDwiFilter->SetInputImage(itkVectorImagePointer);
160  }
161  tractsToDwiFilter->SetParameters(parameters);
162  tractsToDwiFilter->Update();
163 
164  mitk::Image::Pointer image = mitk::GrabItkImageMemory( tractsToDwiFilter->GetOutput() );
169  mitk::DiffusionPropertyHelper propertyHelper( image );
170  propertyHelper.InitializeImage();
171 
172  mitk::IOUtil::Save(image, outName+".dwi");
173 
174  if (verbose)
175  {
176  std::vector< itk::TractsToDWIImageFilter< short >::ItkDoubleImgType::Pointer > volumeFractions = tractsToDwiFilter->GetVolumeFractions();
177  for (unsigned int k=0; k<volumeFractions.size(); k++)
178  {
180  image->InitializeByItk(volumeFractions.at(k).GetPointer());
181  image->SetVolume(volumeFractions.at(k)->GetBufferPointer());
182  mitk::IOUtil::Save(image, outName+"_Compartment"+boost::lexical_cast<string>(k)+".nrrd");
183  }
184 
185  if (tractsToDwiFilter->GetPhaseImage().IsNotNull())
186  {
188  itk::TractsToDWIImageFilter< short >::DoubleDwiType::Pointer itkPhase = tractsToDwiFilter->GetPhaseImage();
189  image = mitk::GrabItkImageMemory( itkPhase.GetPointer() );
190  mitk::IOUtil::Save(image, outName+"_Phase.nrrd");
191  }
192 
193  if (tractsToDwiFilter->GetKspaceImage().IsNotNull())
194  {
196  itk::TractsToDWIImageFilter< short >::DoubleDwiType::Pointer itkImage = tractsToDwiFilter->GetKspaceImage();
197  image = mitk::GrabItkImageMemory( itkImage.GetPointer() );
198  mitk::IOUtil::Save(image, outName+"_kSpace.nrrd");
199  }
200  }
201 
202  return EXIT_SUCCESS;
203 }
204 
MiscFiberfoxParameters m_Misc
GUI realted and I/O parameters.
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
static const std::string REFERENCEBVALUEPROPERTYNAME
itk::SmartPointer< Self > Pointer
itk::Point< double, 3 > m_ImageOrigin
Image origin.
Generates artificial diffusion weighted image volume from the input fiberbundle using a generic multi...
static std::string GetTempPath()
Definition: mitkIOUtil.cpp:372
itk::Matrix< double, 3, 3 > m_ImageDirection
Image rotation matrix.
void SetGradienDirections(GradientListType gradientList)
void setContributor(std::string contributor)
void SaveParameters(string filename)
Save image generation parameters to .ffp file.
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
Helper class for mitk::Images containing diffusion weighted data.
DataCollection - Class to facilitate loading/accessing structured data.
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
double m_Bvalue
Acquisition b-value.
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
itk::Vector< double, 3 > m_ImageSpacing
Image voxel size.
double m_SignalScale
Scaling factor for output signal (before noise is added).
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.
SignalGenerationParameters m_SignalGen
Signal generation parameters.
void InitializeImage()
Make certain the owned image is up to date with all necessary properties.
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)
GradientListType GetGradientDirections()
Return gradient direction container.
Image class for storing images.
Definition: mitkImage.h:76
itk::ImageRegion< 3 > m_ImageRegion
Image size.
Definition: usAny.h:163
static T max(T x, T y)
Definition: svm.cpp:70
void setCategory(std::string category)
static Pointer New()
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
static Pointer New()
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
string m_OutputPath
Image is automatically saved to the specified folder after simulation is finished.
void LoadParameters(string filename)
Load image generation parameters from .ffp file.
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:483
static const std::string GRADIENTCONTAINERPROPERTYNAME
void setTitle(std::string title)
int main(int argc, char *argv[])
Command line interface to Fiberfox. Simulate a diffusion-weighted image from a tractogram using the s...
Definition: Fiberfox.cpp:35
void setDescription(std::string description)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.