Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
DFTracking.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 <mitkIOUtil.h>
25 #include <iostream>
26 #include <fstream>
27 #include <itksys/SystemTools.hxx>
28 #include <mitkCoreObjectFactory.h>
29 
30 #include <mitkFiberBundle.h>
31 #include <itkMLBSTrackingFilter.h>
33 
34 #define _USE_MATH_DEFINES
35 #include <math.h>
36 
37 using namespace std;
38 
39 const int numOdfSamples = 200;
40 typedef itk::Image< itk::Vector< float, numOdfSamples > , 3 > SampledShImageType;
41 
45 int main(int argc, char* argv[])
46 {
47  mitkCommandLineParser parser;
48 
49  parser.setTitle("Machine Learning Based Streamline Tractography");
50  parser.setCategory("Fiber Tracking and Processing Methods");
51  parser.setDescription("Perform machine learning based streamline tractography");
52  parser.setContributor("MBI");
53 
54  parser.setArgumentPrefix("--", "-");
55  parser.addArgument("image", "i", mitkCommandLineParser::String, "DWI:", "input diffusion-weighted image", us::Any(), false);
56  parser.addArgument("forest", "f", mitkCommandLineParser::String, "Forest:", "input random forest (HDF5 file)", us::Any(), false);
57  parser.addArgument("out", "o", mitkCommandLineParser::OutputDirectory, "Output:", "output fiberbundle", us::Any(), false);
58 
59  parser.addArgument("stop", "st", mitkCommandLineParser::String, "Stop image:", "streamlines entering the binary mask will stop immediately", us::Any());
60  parser.addArgument("mask", "m", mitkCommandLineParser::String, "Mask image:", "restrict tractography with a binary mask image", us::Any());
61  parser.addArgument("seed", "s", mitkCommandLineParser::String, "Seed image:", "binary mask image defining seed voxels", us::Any());
62 
63  parser.addArgument("stepsize", "se", mitkCommandLineParser::Float, "Stepsize:", "stepsize (in voxels)", us::Any());
64  parser.addArgument("samples", "ns", mitkCommandLineParser::Int, "Samples:", "number of neighborhood samples", us::Any());
65  parser.addArgument("samplingdist", "sd", mitkCommandLineParser::Float, "Sampling distance:", "distance of neighborhood sampling points (in voxels)", us::Any());
66  parser.addArgument("seeds", "nse", mitkCommandLineParser::Int, "Seeds per voxel:", "number of seed points per voxel", us::Any());
67 
68  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
69  if (parsedArgs.size()==0)
70  return EXIT_FAILURE;
71 
72  string imageFile = us::any_cast<string>(parsedArgs["image"]);
73  string forestFile = us::any_cast<string>(parsedArgs["forest"]);
74  string outFile = us::any_cast<string>(parsedArgs["out"]);
75 
76  string maskFile = "";
77  if (parsedArgs.count("mask"))
78  maskFile = us::any_cast<string>(parsedArgs["mask"]);
79 
80  string seedFile = "";
81  if (parsedArgs.count("seed"))
82  seedFile = us::any_cast<string>(parsedArgs["seed"]);
83 
84  string stopFile = "";
85  if (parsedArgs.count("stop"))
86  stopFile = us::any_cast<string>(parsedArgs["stop"]);
87 
88  float stepsize = -1;
89  if (parsedArgs.count("stepsize"))
90  stepsize = us::any_cast<float>(parsedArgs["stepsize"]);
91 
92  float samplingdist = 0.25;
93  if (parsedArgs.count("samplingdist"))
94  samplingdist = us::any_cast<float>(parsedArgs["samplingdist"]);
95 
96  int samples = 30;
97  if (parsedArgs.count("samples"))
98  samples = us::any_cast<int>(parsedArgs["samples"]);
99 
100  int seeds = 1;
101  if (parsedArgs.count("seeds"))
102  seeds = us::any_cast<int>(parsedArgs["seeds"]);
103 
104  typedef itk::Image<unsigned char, 3> ItkUcharImgType;
105 
106  MITK_INFO << "loading diffusion-weighted image";
107  mitk::Image::Pointer dwi = dynamic_cast<mitk::Image*>(mitk::IOUtil::LoadImage(imageFile).GetPointer());
108 
110  if (!maskFile.empty())
111  {
112  MITK_INFO << "loading mask image";
113  mitk::Image::Pointer img = dynamic_cast<mitk::Image*>(mitk::IOUtil::LoadImage(maskFile).GetPointer());
114  mask = ItkUcharImgType::New();
115  mitk::CastToItkImage(img, mask);
116  }
117 
119  if (!seedFile.empty())
120  {
121  MITK_INFO << "loading seed image";
122  mitk::Image::Pointer img = dynamic_cast<mitk::Image*>(mitk::IOUtil::LoadImage(seedFile).GetPointer());
123  seed = ItkUcharImgType::New();
124  mitk::CastToItkImage(img, seed);
125  }
126 
128  if (!stopFile.empty())
129  {
130  MITK_INFO << "loading stop image";
131  mitk::Image::Pointer img = dynamic_cast<mitk::Image*>(mitk::IOUtil::LoadImage(stopFile).GetPointer());
132  stop = ItkUcharImgType::New();
133  mitk::CastToItkImage(img, stop);
134  }
135 
137  tfh.LoadForest(forestFile);
138  tfh.AddRawData(dwi);
139 
140  typedef itk::MLBSTrackingFilter<> TrackerType;
142  tracker->SetInput(0, mitk::DiffusionPropertyHelper::GetItkVectorImage(dwi));
143  tracker->SetMaskImage(mask);
144  tracker->SetSeedImage(seed);
145  tracker->SetStoppingRegions(stop);
146  tracker->SetSeedsPerVoxel(seeds);
147  tracker->SetStepSize(stepsize);
148  tracker->SetForestHandler(tfh);
149  tracker->SetSamplingDistance(samplingdist);
150  tracker->SetNumberOfSamples(samples);
151  //tracker->SetAvoidStop(false);
152  tracker->SetAposterioriCurvCheck(false);
153  tracker->SetRemoveWmEndFibers(false);
154  tracker->Update();
155  vtkSmartPointer< vtkPolyData > poly = tracker->GetFiberPolyData();
157 
158  mitk::IOUtil::Save(outFib, outFile);
159 
160  return EXIT_SUCCESS;
161 }
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
void setContributor(std::string contributor)
STL namespace.
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
Performes deterministic streamline tracking on the input tensor image.
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
void LoadForest(std::string forestFile)
Manages random forests for fiber tractography. The preparation of the features from the inputa data a...
static ImageType::Pointer GetItkVectorImage(Image *image)
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[])
Perform machine learning based streamline tractography.
Definition: DFTracking.cpp:45
Image class for storing images.
Definition: mitkImage.h:76
Definition: usAny.h:163
itk::Image< itk::Vector< float, numOdfSamples >, 3 > SampledShImageType
Definition: DFTracking.cpp:40
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.
void AddRawData(Image::Pointer img)
const int numOdfSamples
Definition: DFTracking.cpp:39
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
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.