Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkDiffusionImageCreationFilter.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 
18 #include "mitkProperties.h"
19 #include "mitkImageTimeSelector.h"
20 
21 #include "mitkImageCast.h"
22 #include "mitkImageToItk.h"
23 #include "mitkImageAccessByItk.h"
24 #include "mitkITKImageImport.h"
25 
26 #include "mitkIOUtil.h"
27 
28 #include <itkImageFileWriter.h>
29 
30 #include <itkComposeImageFilter.h>
31 
32 
39 {
40  typedef itk::Image<mitk::DiffusionPropertyHelper::DiffusionPixelType, 3> ImageVolumeType;
41  typedef itk::ComposeImageFilter< ImageVolumeType > ComposeFilterType;
42 
45  t_selector->SetInput( input );
46 
47  for( unsigned int i=0; i< input->GetTimeSteps(); i++)
48  {
49  t_selector->SetTimeNr(i);
50  t_selector->Update();
51 
52  ImageVolumeType::Pointer singleImageItk;
53  mitk::CastToItkImage( t_selector->GetOutput(), singleImageItk );
54 
55  vec_composer->SetInput( i, singleImageItk );
56  }
57 
58  try
59  {
60  vec_composer->Update();
61  }
62  catch( const itk::ExceptionObject& e)
63  {
64  MITK_ERROR << "Caught exception while updating compose filter: " << e.what();
65  }
66 
67  mitk::DiffusionImageCreationFilter::VectorImageType::Pointer vector_image = vec_composer->GetOutput();
68  vector_image->GetPixelContainer()->ContainerManageMemoryOff();
69 
70  return vector_image;
71 }
72 
74  : m_ReferenceImage( nullptr )
75 {
76  m_HeaderDescriptorSet = false;
77 
78  this->SetNumberOfRequiredInputs(1);
79  this->SetNumberOfRequiredOutputs(1);
80 }
81 
83 {
84 
85 }
86 
88 {
89  if( reference_image.IsNull() )
90  {
91  mitkThrow() << "Null-pointer image provided as reference. ";
92  }
93 
94  if( ! DPH::IsDiffusionWeightedImage(reference_image) )
95  {
96  mitkThrow() << "The image provided as reference is not a diffusion-weighted image. Cannot proceed. ";
97  }
98 
99  this->m_ReferenceImage = reference_image;
100 }
101 
103 {
104  const mitk::Image::Pointer input_image = this->GetInput(0);
105  if( input_image.IsNull() )
106  {
107  mitkThrow() << "No input specified. Cannot proceed ";
108  }
109 
110  if( !( m_HeaderDescriptorSet ^ m_ReferenceImage.IsNotNull() ) )
111  {
112  mitkThrow() << "Either a header descriptor or a reference diffusion-weighted image have to be provided. Terminating.";
113  }
114 
115  mitk::Image::Pointer outputForCache = this->GetOutput();
116 
117  if( input_image->GetTimeSteps() > 1 )
118  {
119  mitk::Image::Pointer mitkvectorimage = mitk::GrabItkImageMemory<DPH::ImageType>( RemapIntoVectorImage( input_image ));
120  outputForCache->Initialize( mitkvectorimage );
121  }
122  // no need to remap, we expect to have a vector image directly
123  else
124  {
125  outputForCache->Initialize( input_image );
126  }
127 
128  // header information
129  GradientDirectionContainerType::Pointer DiffusionVectors = this->InternalGetGradientDirections( );
130  MeasurementFrameType MeasurementFrame = this->InternalGetMeasurementFrame();
131  float BValue = this->InternalGetBValue();
132 
133  // create BValueMap
135  outputForCache->SetProperty( DPH::GRADIENTCONTAINERPROPERTYNAME.c_str(), mitk::GradientDirectionsProperty::New( DiffusionVectors ) );
136  outputForCache->SetProperty( DPH::MEASUREMENTFRAMEPROPERTYNAME.c_str(), mitk::MeasurementFrameProperty::New( MeasurementFrame ) );
137  outputForCache->SetProperty( DPH::BVALUEMAPPROPERTYNAME.c_str(), mitk::BValueMapProperty::New( BValueMap ) );
138  outputForCache->SetProperty( DPH::REFERENCEBVALUEPROPERTYNAME.c_str(), mitk::FloatProperty::New( BValue ) );
139 
140  outputForCache->Modified();
141 }
142 
144 {
145  this->m_HeaderDescriptor = header_descriptor;
146  this->m_HeaderDescriptorSet = true;
147 }
148 
151 {
152  MeasurementFrameType MeasurementFrame;
153 
154  if( m_ReferenceImage.IsNotNull() )
155  {
156  MeasurementFrame = DPH::GetMeasurementFrame( m_ReferenceImage );
157  }
158  else if ( m_HeaderDescriptorSet )
159  {
160  MeasurementFrame = m_HeaderDescriptor.m_MeasurementFrame;
161  }
162  else
163  {
164  MeasurementFrame(0,0) = 1;
165  MeasurementFrame(0,1) = 0;
166  MeasurementFrame(0,2) = 0;
167  MeasurementFrame(1,0) = 0;
168  MeasurementFrame(1,1) = 1;
169  MeasurementFrame(1,2) = 0;
170  MeasurementFrame(2,0) = 0;
171  MeasurementFrame(2,1) = 0;
172  MeasurementFrame(2,2) = 1;
173 
174  MITK_WARN << "Created default measurement frame as non provided ( no reference image or header information provided)";
175  }
176 
177  return MeasurementFrame;
178 }
179 
182 {
184 
185  if( this->m_ReferenceImage )
186  {
187  DiffusionVectors = DPH::GetGradientContainer( this->m_ReferenceImage );
188  }
189  else if ( m_HeaderDescriptorSet )
190  {
191  DiffusionVectors = m_HeaderDescriptor.m_GradientDirections;
192  }
193 
194  return DiffusionVectors;
195 
196 }
197 
199 {
200  float bvalue = -1;
201 
202  if( m_ReferenceImage.IsNotNull() )
203  {
204  bvalue = DPH::GetReferenceBValue( m_ReferenceImage );
205  }
206  else if ( m_HeaderDescriptorSet )
207  {
208  bvalue = m_HeaderDescriptor.m_BValue;
209  }
210  else
211  {
212  MITK_ERROR << "No reference image and no header descriptor provided.";
213  }
214 
215  return bvalue;
216 }
217 
218 
219 
220 
221 
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...
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void SetHeaderDescriptor(DiffusionImageHeaderDescriptor header_descriptor)
SetHeaderDescriptor set the information to be used with the dw image.
static BValueMap CreateBValueMap(const GradientDirectionsContainerType *gdc, float referenceBValue)
VectorImageType::Pointer RemapIntoVectorImage(mitk::Image::Pointer input)
RemapIntoVectorImage Take a 3d+t image and reinterpret it as vector image.
virtual void GenerateData() override
A version of GenerateData() specific for image processing filters.
static const std::string MEASUREMENTFRAMEPROPERTYNAME
#define MITK_WARN
Definition: mitkLogMacros.h:23
#define mitkThrow()
static Pointer New()
The DiffusionImageHeaderDescriptor struct bundles the necessary diffusion-weighted image header meta ...
void SetReferenceImage(mitk::Image::Pointer reference_image)
SetReferenceImage Set a diffusion image as reference, i.e. the header information will be extracted f...
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
static const std::string GRADIENTCONTAINERPROPERTYNAME
GradientDirectionContainerType::Pointer InternalGetGradientDirections()
static const std::string BVALUEMAPPROPERTYNAME
mitk::DiffusionPropertyHelper::MeasurementFrameType MeasurementFrameType
static Pointer New()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.