Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkSymmetricForcesDemonsRegistration.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 
19 #include "itkImageFileWriter.h"
20 #include "itkWarpImageFilter.h"
21 
22 #include "itkInverseDisplacementFieldImageFilter.h"
23 
25 
26 namespace mitk
27 {
29  : m_Iterations(50),
30  m_StandardDeviation(1.0),
31  m_FieldName("newField.mhd"),
32  m_ResultName("deformedImage.mhd"),
33  m_SaveField(true),
34  m_SaveResult(true),
35  m_DeformationField(nullptr)
36  {
37  }
38 
44  {
45  m_FieldName = fieldName;
46  }
47 
48  void SymmetricForcesDemonsRegistration::SetSaveResult(bool saveResult) { m_SaveResult = saveResult; }
49  void SymmetricForcesDemonsRegistration::SetResultFileName(const char *resultName) { m_ResultName = resultName; }
50  itk::Image<class itk::Vector<float, 3>, 3>::Pointer SymmetricForcesDemonsRegistration::GetDeformationField()
51  {
52  return m_DeformationField;
53  }
54 
55  template <typename TPixel, unsigned int VImageDimension>
56  void SymmetricForcesDemonsRegistration::GenerateData2(const itk::Image<TPixel, VImageDimension> *itkImage1)
57  {
58  typedef typename itk::Image<TPixel, VImageDimension> FixedImageType;
59  typedef typename itk::Image<TPixel, VImageDimension> MovingImageType;
60 
61  typedef float InternalPixelType;
62  typedef typename itk::Image<InternalPixelType, VImageDimension> InternalImageType;
63  typedef typename itk::CastImageFilter<FixedImageType, InternalImageType> FixedImageCasterType;
64  typedef typename itk::CastImageFilter<MovingImageType, InternalImageType> MovingImageCasterType;
65  typedef typename itk::Vector<float, VImageDimension> VectorPixelType;
66  typedef typename itk::Image<VectorPixelType, VImageDimension> DeformationFieldType;
67  typedef
68  typename itk::SymmetricForcesDemonsRegistrationFilter<InternalImageType, InternalImageType, DeformationFieldType>
69  RegistrationFilterType;
70  typedef typename itk::WarpImageFilter<MovingImageType, MovingImageType, DeformationFieldType> WarperType;
71  typedef typename itk::LinearInterpolateImageFunction<MovingImageType, double> InterpolatorType;
72 
73  typedef TPixel OutputPixelType;
74  typedef typename itk::Image<OutputPixelType, VImageDimension> OutputImageType;
75  typedef typename itk::CastImageFilter<MovingImageType, OutputImageType> CastFilterType;
76  typedef typename itk::ImageFileWriter<OutputImageType> WriterType;
77  typedef typename itk::ImageFileWriter<DeformationFieldType> FieldWriterType;
78 
79  typename FixedImageType::Pointer fixedImage = FixedImageType::New();
81  typename MovingImageType::ConstPointer movingImage = itkImage1;
82 
83  if (fixedImage.IsNotNull() && movingImage.IsNotNull())
84  {
86 
87  this->AddStepsToDo(4);
90  command->SetCallbackFunction(this, &SymmetricForcesDemonsRegistration::SetProgress);
91  filter->AddObserver(itk::IterationEvent(), command);
92 
93  typename FixedImageCasterType::Pointer fixedImageCaster = FixedImageCasterType::New();
94  fixedImageCaster->SetInput(fixedImage);
95  filter->SetFixedImage(fixedImageCaster->GetOutput());
96  typename MovingImageCasterType::Pointer movingImageCaster = MovingImageCasterType::New();
97  movingImageCaster->SetInput(movingImage);
98  filter->SetMovingImage(movingImageCaster->GetOutput());
99  filter->SetNumberOfIterations(m_Iterations);
100  filter->SetStandardDeviations(m_StandardDeviation);
101  filter->Update();
102 
103  typename WarperType::Pointer warper = WarperType::New();
104  typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
105 
106  warper->SetInput(movingImage);
107  warper->SetInterpolator(interpolator);
108  warper->SetOutputSpacing(fixedImage->GetSpacing());
109  warper->SetOutputOrigin(fixedImage->GetOrigin());
110  warper->SetDisplacementField(filter->GetOutput());
111  warper->Update();
112  typename WriterType::Pointer writer = WriterType::New();
113  typename CastFilterType::Pointer caster = CastFilterType::New();
114 
115  writer->SetFileName(m_ResultName);
116 
117  caster->SetInput(warper->GetOutput());
118  writer->SetInput(caster->GetOutput());
119  if (m_SaveResult)
120  {
121  writer->Update();
122  }
123  Image::Pointer outputImage = this->GetOutput();
124  mitk::CastToMitkImage(warper->GetOutput(), outputImage);
125 
126  if (VImageDimension == 2)
127  {
128  typedef DeformationFieldType VectorImage2DType;
129  typedef typename DeformationFieldType::PixelType Vector2DType;
130 
131  typename VectorImage2DType::ConstPointer vectorImage2D = filter->GetOutput();
132 
133  typename VectorImage2DType::RegionType region2D = vectorImage2D->GetBufferedRegion();
134  typename VectorImage2DType::IndexType index2D = region2D.GetIndex();
135  typename VectorImage2DType::SizeType size2D = region2D.GetSize();
136 
137  typedef typename itk::Vector<float, 3> Vector3DType;
138  typedef typename itk::Image<Vector3DType, 3> VectorImage3DType;
139 
140  typedef typename itk::ImageFileWriter<VectorImage3DType> WriterType;
141 
143 
145 
146  VectorImage3DType::RegionType region3D;
147  VectorImage3DType::IndexType index3D;
148  VectorImage3DType::SizeType size3D;
149 
150  index3D[0] = index2D[0];
151  index3D[1] = index2D[1];
152  index3D[2] = 0;
153 
154  size3D[0] = size2D[0];
155  size3D[1] = size2D[1];
156  size3D[2] = 1;
157 
158  region3D.SetSize(size3D);
159  region3D.SetIndex(index3D);
160 
161  typename VectorImage2DType::SpacingType spacing2D = vectorImage2D->GetSpacing();
162  VectorImage3DType::SpacingType spacing3D;
163 
164  spacing3D[0] = spacing2D[0];
165  spacing3D[1] = spacing2D[1];
166  spacing3D[2] = 1.0;
167 
168  vectorImage3D->SetSpacing(spacing3D);
169 
170  vectorImage3D->SetRegions(region3D);
171  vectorImage3D->Allocate();
172 
173  typedef typename itk::ImageRegionConstIterator<VectorImage2DType> Iterator2DType;
174 
175  typedef typename itk::ImageRegionIterator<VectorImage3DType> Iterator3DType;
176 
177  Iterator2DType it2(vectorImage2D, region2D);
178  Iterator3DType it3(vectorImage3D, region3D);
179 
180  it2.GoToBegin();
181  it3.GoToBegin();
182 
183  Vector2DType vector2D;
184  Vector3DType vector3D;
185 
186  vector3D[2] = 0; // set Z component to zero.
187 
188  while (!it2.IsAtEnd())
189  {
190  vector2D = it2.Get();
191  vector3D[0] = vector2D[0];
192  vector3D[1] = vector2D[1];
193  it3.Set(vector3D);
194  ++it2;
195  ++it3;
196  }
197 
198  writer3D->SetInput(vectorImage3D);
199  m_DeformationField = vectorImage3D;
200 
201  writer3D->SetFileName(m_FieldName);
202 
203  try
204  {
205  if (m_SaveField)
206  {
207  writer3D->Update();
208  }
209  }
210  catch (itk::ExceptionObject &excp)
211  {
212  MITK_ERROR << excp << std::endl;
213  }
214  }
215  else
216  {
217  typename FieldWriterType::Pointer fieldwriter = FieldWriterType::New();
218  fieldwriter->SetFileName(m_FieldName);
219  fieldwriter->SetInput(filter->GetOutput());
220  // m_DeformationField = filter->GetOutput();
221  m_DeformationField = (itk::Image<itk::Vector<float, 3>, 3> *)(filter->GetOutput()); // see BUG #3732
222  if (m_SaveField)
223  {
224  fieldwriter->Update();
225  }
226  }
227  this->SetRemainingProgress(4);
228  }
229  }
230 } // end namespace
itk::SmartPointer< Self > Pointer
void SetSaveDeformationField(bool saveField)
Sets whether the resulting deformation field should be saved or not.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void SetSaveResult(bool saveResult)
Sets whether the result should be saved or not.
DataCollection - Class to facilitate loading/accessing structured data.
virtual void SetRemainingProgress(int steps)
Sets the remaining progress to the progress bar.
void SetResultFileName(const char *resultName)
Sets the filename for the resulting deformed image.
itk::SmartPointer< const Self > ConstPointer
virtual void AddStepsToDo(int steps)
Adds steps to the progress bar, which will be done with AddStepsToDo(int steps) and SetRemainingProgr...
virtual void SetProgress(const itk::EventObject &)
Sets one step of progress to the progress bar.
void SetStandardDeviation(float deviation)
Sets the standard deviation used by the symmetric forces demons registration.
mitk::Image OutputImageType
Some convenient typedefs.
void GenerateData2(const itk::Image< TPixel, VImageDimension > *itkImage1)
Template class to perform the symmetric forces demons registration with any kind of image...
itk::Image< VectorType, 3 > DeformationFieldType
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:78
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 SetNumberOfIterations(int iterations)
Sets the number of iterations which will be performed during the registration process.
itk::Image< class itk::Vector< float, 3 >, 3 >::Pointer GetDeformationField()
Returns the deformation field, which results by the registration.
unsigned short PixelType
OutputType * GetOutput()
Get the output data of this image source object.
itk::Image< class itk::Vector< float, 3 >, 3 >::Pointer m_DeformationField
void SetDeformationFieldFileName(const char *fieldName)
Sets the filename for the resulting deformation field.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.