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