Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkTrackingForestHandler.h
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 #ifndef _TrackingForestHandler
18 #define _TrackingForestHandler
19 
20 #include "mitkBaseData.h"
21 
22 #include <mitkFiberBundle.h>
25 #include <itkMersenneTwisterRandomVariateGenerator.h>
27 #include <chrono>
28 
29 #undef DIFFERENCE
30 #define VIGRA_STATIC_LIB
31 #include <vigra/random_forest.hxx>
32 #include <vigra/multi_array.hxx>
33 #include <vigra/random_forest_hdf5_impex.hxx>
34 
35 #define _USE_MATH_DEFINES
36 #include <math.h>
37 
38 namespace mitk
39 {
40 
44 template< int ShOrder=6, int NumberOfSignalFeatures=100 >
46 {
47 
48 public:
49 
52 
53  typedef itk::Image<unsigned char, 3> ItkUcharImgType;
54  typedef itk::Image< itk::Vector< float, NumberOfSignalFeatures*2 > , 3 > InterpolatedRawImageType;
55  typedef itk::Image< Vector< float, NumberOfSignalFeatures > , 3 > FeatureImageType;
56 
57  void SetRawData( std::vector< Image::Pointer > images ){ m_RawData = images; }
58  void AddRawData( Image::Pointer img ){ m_RawData.push_back(img); }
59  void SetTractograms( std::vector< FiberBundle::Pointer > tractograms )
60  {
61  m_Tractograms.clear();
62  for (auto fib : tractograms)
63  {
64  m_Tractograms.push_back(fib->GetDeepCopy());
65  }
66  }
67  void SetMaskImages( std::vector< ItkUcharImgType::Pointer > images ){ m_MaskImages = images; }
68  void SetWhiteMatterImages( std::vector< ItkUcharImgType::Pointer > images ){ m_WhiteMatterImages = images; }
69 
70  void StartTraining();
71  void SaveForest(std::string forestFile);
72  void LoadForest(std::string forestFile);
73 
74  // training parameters
75 
76  void SetNumTrees(int num){ m_NumTrees = num; }
77  void SetMaxTreeDepth(int depth){ m_MaxTreeDepth = depth; }
78  void SetStepSize(double step){ m_WmSampleDistance = step; }
79  void SetGrayMatterSamplesPerVoxel(int samples){ m_GmSamplesPerVoxel = samples; }
80  void SetSampleFraction(double fraction){ m_SampleFraction = fraction; }
81  std::shared_ptr< vigra::RandomForest<int> > GetForest(){ return m_Forest; }
82 
83  void InitForTracking();
84  vnl_vector_fixed<double,3> Classify(itk::Point<double, 3>& pos, int& candidates, vnl_vector_fixed<double,3>& olddir, double angularThreshold, double& w, ItkUcharImgType::Pointer mask=nullptr);
85 
86  bool IsForestValid();
87 
88 protected:
89 
90  // tracking
92  typename FeatureImageType::PixelType GetFeatureValues(itk::Point<float, 3> itkP);
93 
94  // training
98  void TrainForest();
99  typename InterpolatedRawImageType::PixelType GetImageValues(itk::Point<float, 3> itkP, typename InterpolatedRawImageType::Pointer image);
100 
101 
102  std::vector< Image::Pointer > m_RawData;
103  std::shared_ptr< vigra::RandomForest<int> > m_Forest;
104  std::chrono::time_point<std::chrono::system_clock> m_StartTime;
105  std::chrono::time_point<std::chrono::system_clock> m_EndTime;
106 
107  // only for training
108  std::vector< FiberBundle::Pointer > m_Tractograms;
109  std::vector< ItkUcharImgType::Pointer > m_MaskImages;
110  std::vector< ItkUcharImgType::Pointer > m_WhiteMatterImages;
111  std::vector< typename InterpolatedRawImageType::Pointer > m_InterpolatedRawImages;
116  unsigned int m_NumberOfSamples;
117  std::vector< unsigned int > m_GmSamples;
119  vigra::MultiArray<2, double> m_FeatureData;
120 
121  // only for tracking
123  vigra::MultiArray<2, double> m_LabelData;
124  std::vector< int > m_DirectionIndices;
126 };
127 
128 }
129 
131 
132 #endif
std::vector< int > m_DirectionIndices
maps each of the NumberOfSignalFeatures possible output directions to one of the 2*NumberOfSignalFeat...
std::chrono::time_point< std::chrono::system_clock > m_EndTime
itk::SmartPointer< Self > Pointer
std::vector< Image::Pointer > m_RawData
original input DWI data
std::vector< ItkUcharImgType::Pointer > m_WhiteMatterImages
defines white matter voxels. if not set, theses mask images are automatically generated from the inpu...
vigra::MultiArray< 2, double > m_FeatureData
vigra container for training features
unsigned int m_NumberOfSamples
stores overall number of samples used for training
int m_NumTrees
number of trees in random forest
void PreprocessInputDataForTraining()
Generate masks if necessary, resample fibers, spherically interpolate raw DWIs.
void SetGrayMatterSamplesPerVoxel(int samples)
void SetTractograms(std::vector< FiberBundle::Pointer > tractograms)
itk::OrientationDistributionFunction< double, NumberOfSignalFeatures *2 > m_DirContainer
direction container
DataCollection - Class to facilitate loading/accessing structured data.
std::vector< unsigned int > m_GmSamples
number of gray matter samples
std::shared_ptr< vigra::RandomForest< int > > m_Forest
random forest classifier
std::shared_ptr< vigra::RandomForest< int > > GetForest()
void SetSampleFraction(double fraction)
void SetRawData(std::vector< Image::Pointer > images)
void LoadForest(std::string forestFile)
Manages random forests for fiber tractography. The preparation of the features from the inputa data a...
std::chrono::time_point< std::chrono::system_clock > m_StartTime
void InputDataValidForTracking()
check if raw data is set and tracking forest is valid
double m_SampleFraction
fraction of samples used to train each tree
int m_MaxTreeDepth
limits the tree depth
FeatureImageType::Pointer m_FeatureImage
feature image used for tracking
std::vector< typename InterpolatedRawImageType::Pointer > m_InterpolatedRawImages
spherically interpolated and resampled raw datasets
vigra::MultiArray< 2, double > m_LabelData
vigra container for training labels
int m_GmSamplesPerVoxel
number of gray matter samplees per voxel. if -1, then the number is automatically chosen to gain an o...
vnl_vector_fixed< double, 3 > Classify(itk::Point< double, 3 > &pos, int &candidates, vnl_vector_fixed< double, 3 > &olddir, double angularThreshold, double &w, ItkUcharImgType::Pointer mask=nullptr)
predicts next progression direction at the given position
double m_WmSampleDistance
deterines the number of white matter samples (distance of sampling points on each fiber)...
void SetWhiteMatterImages(std::vector< ItkUcharImgType::Pointer > images)
void CalculateFeaturesForTraining()
Calculate GM and WM features using the interpolated raw data, the WM masks and the fibers...
itk::Image< Vector< float, NumberOfSignalFeatures >, 3 > FeatureImageType
void SaveForest(std::string forestFile)
InterpolatedRawImageType::PixelType GetImageValues(itk::Point< float, 3 > itkP, typename InterpolatedRawImageType::Pointer image)
get trilinearly interpolated raw image values at given world position
void InputDataValidForTraining()
Check if everything is tehere for training (raw datasets, fiber tracts)
void InitForTracking()
calls InputDataValidForTracking() and creates feature images from the war input DWI ...
std::vector< FiberBundle::Pointer > m_Tractograms
training tractograms
void AddRawData(Image::Pointer img)
unsigned short PixelType
FeatureImageType::PixelType GetFeatureValues(itk::Point< float, 3 > itkP)
get trilinearly interpolated feature values at given world position
itk::Image< itk::Vector< float, NumberOfSignalFeatures *2 >, 3 > InterpolatedRawImageType
void TrainForest()
start training process
itk::Image< unsigned char, 3 > ItkUcharImgType
void SetMaskImages(std::vector< ItkUcharImgType::Pointer > images)
section MAP_FRAME_Mapper_Settings Mapper settings For the mapping of corrected images
bool IsForestValid()
true is forest is not null, has more than 0 trees and the correct number of features (NumberOfSignalF...
std::vector< ItkUcharImgType::Pointer > m_MaskImages
binary mask images to constrain training to a certain area (e.g. brain mask)