Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkDiffusionImageTransformedCreationFilter.cxx
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 DIFFUSIONIMAGETRANSFORMEDCREATIONFILTER_CXX
18 #define DIFFUSIONIMAGETRANSFORMEDCREATIONFILTER_CXX
19 
22 
23 #include "mitkImageCast.h"
24 #include "mitkImageWriteAccessor.h"
25 #include "mitkImageTimeSelector.h"
26 #include "mitkProperties.h"
27 #include "mitkIOUtil.h"
28 
29 
30 #include <itkResampleImageFilter.h>
31 #include <itkNearestNeighborInterpolateImageFunction.h>
32 #include <itkWindowedSincInterpolateImageFunction.h>
33 #include <itkBSplineInterpolateImageFunction.h>
34 #include <itkLinearInterpolateImageFunction.h>
35 
36 
37 template <typename TTransformType, typename ItkImageType>
38 static void ResampleImage( typename ItkImageType::Pointer itk_reference,
39  mitk::Image::Pointer mitk_input,
40  typename TTransformType::Pointer transform,
41  unsigned int interpolator,
42  mitk::Image::Pointer output_target, unsigned int position )
43 {
44 
45 
46  typedef itk::LinearInterpolateImageFunction< ItkImageType, double > InterpolatorType;
47  typename InterpolatorType::Pointer linear_interpolator = InterpolatorType::New();
48 
49  typedef itk::NearestNeighborInterpolateImageFunction< ItkImageType, double > NearestNeighborInterpolatorType;
51 
52  typedef itk::WindowedSincInterpolateImageFunction< ItkImageType, 7> WindowedSincInterpolatorType;
54 
55  typedef itk::BSplineInterpolateImageFunction< ItkImageType, double, double> BSplineInterpolatorType;
56  typename BSplineInterpolatorType::Pointer bicubic_interpolator = BSplineInterpolatorType::New();
57  bicubic_interpolator->SetSplineOrder(3);
58 
59  typedef typename itk::ResampleImageFilter< ItkImageType, ItkImageType, double> ResampleImageFilterType;
61 
62  // Create interpolator pool
63  typedef itk::InterpolateImageFunction< ItkImageType, double> InterpolateFunctionBaseType;
64  std::vector< InterpolateFunctionBaseType* > InterpolateFunctionsPool;
65 
66  InterpolateFunctionsPool.push_back( nn_interpolator.GetPointer() );
67  InterpolateFunctionsPool.push_back( linear_interpolator.GetPointer() );
68  InterpolateFunctionsPool.push_back( bicubic_interpolator.GetPointer() );
69  InterpolateFunctionsPool.push_back( sinc_interpolator.GetPointer() );
70 
71  // select interpolator by the selection flag
72  resampler->SetInterpolator( InterpolateFunctionsPool.at( interpolator ) );
73 
74  typename ItkImageType::Pointer itk_input;
75  mitk::CastToItkImage< ItkImageType >( mitk_input, itk_input );
76 
77  resampler->SetInput( itk_input );
78  resampler->SetTransform( transform );
79  resampler->SetReferenceImage( itk_reference );
80  resampler->UseReferenceImageOn();
81  resampler->Update();
82 
83  mitk::Image::Pointer current_resampled = mitk::Image::New();
84  current_resampled->InitializeByItk( resampler->GetOutput() );
85  current_resampled->SetImportChannel( resampler->GetOutput()->GetBufferPointer(),
87 
88  mitk::ImageWriteAccessor imac( current_resampled );
89  output_target->SetImportVolume( imac.GetData(),
90  position, 0, mitk::Image::CopyMemory );
91 
92 }
93 /*
94 template< typename TTransformType >
95 static mitk::DiffusionPropertyHelper::GradientDirectionType
96 TransformGradientDirection( mitk::DiffusionPropertyHelper::GradientDirectionType vec_in,
97  typename TTransformType::Pointer transform )
98 {
99  mitk::DiffusionPropertyHelper::GradientDirectionType vec_out;
100  vec_out.fill(1.0);
101 
102  typedef typename TTransformType::MatrixType TMatrixType;
103 
104  return vec_out;
105 }
106 */
107 template< typename TTransformType>
110 {
111  this->m_OutputPrefix = "/tmp/";
112 }
113 
114 template< typename TTransformType>
117 {
118 
119 }
120 
121 template< typename TTransformType>
125 {
127 
129  dwhdesc.m_BValue = DPH::GetReferenceBValue( this->m_DiffusionReferenceImage );
130  // TODO : here comes transformation of the gradients
131  dwhdesc.m_GradientDirections = DPH::GetGradientContainer( this->m_DiffusionReferenceImage );
132  dwhdesc.m_MeasurementFrame = DPH::GetMeasurementFrame( this->m_DiffusionReferenceImage );
133  dwhdesc.m_BValueMapType = DPH::GetBValueMap( this->m_DiffusionReferenceImage );
134 
135  return dwhdesc;
136 }
137 
138 template< typename TTransformType>
141 {
142  MITK_INFO << "Debug info";
143 }
144 
145 
146 template< typename TTransformType>
149 {
150 
151  mitk::Image::Pointer input = this->GetInput();
152 
153  // validity checks
154  if( m_InternalTransforms.size() != input->GetTimeSteps() )
155  {
156  mitkThrow() << "Number of transforms" << m_InternalTransforms.size() << "differ from number of timesteps" << input->GetTimeSteps();
157  }
158 
159  typedef itk::Image< DiffusionPropertyHelper::DiffusionPixelType, 3> ImageType;
160 
161  // create intermediate output
162  mitk::Image::Pointer resampled_output = mitk::Image::New();
163  resampled_output->Initialize( input );
164 
165  ImageType::Pointer current_itk_reference = ImageType::New();
166  CastToItkImage( this->m_ResamplingReferenceImage, current_itk_reference );
167 
168  unsigned int time_dim = input->GetDimension(3);
169  for( unsigned int time_idx = 0; time_idx < time_dim; time_idx++ )
170  {
171 
173  t_sel->SetInput( input );
174  t_sel->SetTimeNr( time_idx );
175  t_sel->Update();
176  ResampleImage< TTransformType, ImageType>( current_itk_reference,
177  t_sel->GetOutput(),
178  this->m_InternalTransforms.at(time_idx),
179  this->m_InterpolationLevel,
180  resampled_output, time_idx);
181  }
182 
183  // call here creation filter
186 
187  creator->SetInput( resampled_output );
188  creator->SetHeaderDescriptor( this->GetTransformedHeaderInformation() );
189  creator->Update();
190 
191  mitk::Image::Pointer output = creator->GetOutput();
192 
194  float BValue = mitk::DiffusionPropertyHelper::GetReferenceBValue( creator->GetOutput() );
195 
199  output->SetProperty( DPH::BVALUEMAPPROPERTYNAME.c_str(), mitk::BValueMapProperty::New( BValueMap ) );
200  output->SetProperty( DPH::REFERENCEBVALUEPROPERTYNAME.c_str(), mitk::FloatProperty::New( BValue ) );
201 
202  // correct gradients
204  corrector->SetImage( output) ;
205  corrector->CorrectDirections( this->m_RotationMatrices );
206 
207  DPH pHelper( output );
208  pHelper.InitializeImage();
209 
210  m_OutputCache = output;
211  this->SetPrimaryOutput( m_OutputCache );
212  m_OutputCache->Modified();
213 }
214 
215 template< typename TTransformType>
218 {
219  if( params.empty() )
220  {
221  MITK_ERROR << "Empty parameter list given.";
222  return;
223  }
224 
225  TransformContainerType transforms;
226 
227  auto iter = std::begin( params );
228  while( iter != std::end( params ) )
229  {
230  typename TTransformType::Pointer transform = TTransformType::New();
231  transform->SetParameters( (*iter) );
232 
233  transforms.push_back( transform );
234 
235  ++iter;
236  }
237 
238  this->SetTransforms( transforms );
239 }
240 
241 template< typename TTransformType>
244 {
245  if( transforms.empty() )
246  {
247  MITK_ERROR << "Empty transform list given.";
248  return;
249  }
250 
251  this->m_InternalTransforms.reserve( transforms.size() );
252  std::copy( transforms.begin(), transforms.end(),
253  std::back_inserter(this->m_InternalTransforms ) );
254 
255  MatrixType E;
256  E.set_identity();
257 
258  for( auto iter = std::begin(this->m_InternalTransforms); iter != std::end( this->m_InternalTransforms); ++iter)
259  {
260  MatrixType A = E * (*iter)->GetMatrix().GetVnlMatrix();
261  this->m_RotationMatrices.push_back( A );
262  }
263 }
264 
265 
266 
267 #endif // DIFFUSIONIMAGETRANSFORMEDCREATIONFILTER_CXX
mitk::DiffusionPropertyHelper::MeasurementFrameType m_MeasurementFrame
const MeasurementFrameType & GetMeasurementFrame() const
static const std::string REFERENCEBVALUEPROPERTYNAME
itk::SmartPointer< Self > Pointer
std::map< unsigned int, std::vector< unsigned int > > BValueMap
The BValueMap contains seperated IndicesVectors for each b value (index for GradientDirectionContaine...
void SetTransformParameters(const TransformParametersContainerType &)
#define MITK_INFO
Definition: mitkLogMacros.h:22
itk::Image< unsigned char, 3 > ImageType
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static BValueMap CreateBValueMap(const GradientDirectionsContainerType *gdc, float referenceBValue)
static Pointer New()
Method for creation through the object factory.
Helper class for mitk::Images containing diffusion weighted data.
void * GetData()
Gives full data access.
std::vector< typename TTransformType::ParametersType > TransformParametersContainerType
mitk::DiffusionPropertyHelper::GradientDirectionsContainerType::Pointer m_GradientDirections
static const std::string MEASUREMENTFRAMEPROPERTYNAME
const BValueMapType & GetBValueMap() const
DiffusionImageCorrectionFilter::TransformMatrixType MatrixType
void InitializeImage()
Make certain the owned image is up to date with all necessary properties.
void GenerateData() override
A version of GenerateData() specific for image processing filters.
#define mitkThrow()
static Pointer New()
mitk::DiffusionPropertyHelper DPH
static void ResampleImage(typename ItkImageType::Pointer itk_reference, mitk::Image::Pointer mitk_input, typename TTransformType::Pointer transform, unsigned int interpolator, mitk::Image::Pointer output_target, unsigned int position)
mitk::DiffusionPropertyHelper::BValueMapType m_BValueMapType
static Pointer New()
static Pointer New()
The DiffusionImageHeaderDescriptor struct bundles the necessary diffusion-weighted image header meta ...
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
GradientDirectionsContainerType::Pointer GetGradientContainer() const
ImageWriteAccessor class to get locked write-access for a particular image part.
std::vector< typename TTransformType::Pointer > TransformContainerType
static const std::string GRADIENTCONTAINERPROPERTYNAME
static const std::string BVALUEMAPPROPERTYNAME
static Pointer New()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.