19 #include "itkImageFileWriter.h"
20 #include "itkImageRegionIterator.h"
21 #include "itkWarpImageFilter.h"
23 #include "itkBSplineDeformableTransform.h"
26 #include "itkImageRegistrationMethod.h"
27 #include "itkMattesMutualInformationImageToImageMetric.h"
28 #include "itkMeanSquaresImageToImageMetric.h"
29 #include "itkResampleImageFilter.h"
36 #include <itkHistogramMatchingImageFilter.h>
37 #include <itkRescaleIntensityImageFilter.h>
43 m_ResultName(
"deformedImage.mhd"),
45 m_SaveDeformationField(false),
46 m_UpdateInputImage(false),
47 m_MatchHistograms(true),
57 template <
typename TPixel,
unsigned int VImageDimension>
60 std::cout <<
"start bspline registration" << std::endl;
63 typedef typename itk::Image<TPixel, VImageDimension> InternalImageType;
65 typedef typename itk::Vector<float, VImageDimension> VectorPixelType;
68 typedef itk::BSplineDeformableTransform<double, VImageDimension, 3> TransformType;
70 typedef typename TransformType::ParametersType ParametersType;
73 typedef itk::SingleValuedNonLinearOptimizer OptimizerType;
76 typedef itk::MattesMutualInformationImageToImageMetric<InternalImageType, InternalImageType> MetricType;
78 typedef itk::MeanSquaresImageToImageMetric<InternalImageType, InternalImageType> MetricTypeMS;
80 typedef itk::LinearInterpolateImageFunction<InternalImageType, double> InterpolatorType;
82 typedef itk::ImageRegistrationMethod<InternalImageType, InternalImageType>
RegistrationType;
84 typedef typename itk::WarpImageFilter<InternalImageType, InternalImageType, DeformationFieldType> WarperType;
86 typedef typename TransformType::SpacingType SpacingType;
88 typedef typename TransformType::OriginType OriginType;
90 typedef itk::ResampleImageFilter<InternalImageType, InternalImageType> ResampleFilterType;
95 typedef itk::CastImageFilter<InternalImageType, InternalImageType> CastFilterType;
97 typedef itk::Vector<float, VImageDimension>
VectorType;
110 metric->SetNumberOfHistogramBins(32);
111 metric->SetNumberOfSpatialSamples(90000);
112 registration->SetMetric(metric);
117 registration->SetMetric(metric);
122 optFac->SetNumberOfTransformParameters(transform->GetNumberOfParameters());
125 optimizer->AddObserver(itk::AnyEvent(),
m_Observer);
135 typename InternalImageType::RegionType fixedRegion = fixedImage->GetBufferedRegion();
136 typename InternalImageType::RegionType movingRegion = movingImage->GetBufferedRegion();
140 typedef itk::RescaleIntensityImageFilter<InternalImageType, InternalImageType> FilterType;
141 typedef itk::HistogramMatchingImageFilter<InternalImageType, InternalImageType> HEFilterType;
146 referenceRescaleFilter->SetInput(fixedImage);
147 inputRescaleFilter->SetInput(movingImage);
149 TPixel desiredMinimum = 0;
150 TPixel desiredMaximum = 255;
152 referenceRescaleFilter->SetOutputMinimum(desiredMinimum);
153 referenceRescaleFilter->SetOutputMaximum(desiredMaximum);
154 referenceRescaleFilter->UpdateLargestPossibleRegion();
155 inputRescaleFilter->SetOutputMinimum(desiredMinimum);
156 inputRescaleFilter->SetOutputMaximum(desiredMaximum);
157 inputRescaleFilter->UpdateLargestPossibleRegion();
162 intensityEqualizeFilter->SetReferenceImage(inputRescaleFilter->GetOutput());
163 intensityEqualizeFilter->SetInput(referenceRescaleFilter->GetOutput());
164 intensityEqualizeFilter->SetNumberOfHistogramLevels(64);
165 intensityEqualizeFilter->SetNumberOfMatchPoints(12);
166 intensityEqualizeFilter->ThresholdAtMeanIntensityOn();
167 intensityEqualizeFilter->Update();
172 fixedImage = intensityEqualizeFilter->GetOutput();
173 movingImage = inputRescaleFilter->GetOutput();
177 registration->SetOptimizer(optimizer);
178 registration->SetInterpolator(interpolator);
179 registration->SetFixedImage(fixedImage);
180 registration->SetMovingImage(movingImage);
181 registration->SetFixedImageRegion(fixedRegion);
183 initializer->SetTransform(transform);
184 initializer->SetImage(fixedImage);
186 initializer->InitializeTransform();
188 registration->SetTransform(transform);
190 const unsigned int numberOfParameters = transform->GetNumberOfParameters();
192 typename itk::BSplineDeformableTransform<double, VImageDimension, 3>::ParametersType parameters;
194 parameters.set_size(numberOfParameters);
195 parameters.Fill(0.0);
196 transform->SetParameters(parameters);
200 registration->SetInitialTransformParameters(transform->GetParameters());
202 std::cout <<
"Intial Parameters = " << std::endl;
203 std::cout << transform->GetParameters() << std::endl;
205 std::cout << std::endl <<
"Starting Registration" << std::endl;
209 double tstart(clock());
210 registration->Update();
211 double time = clock() - tstart;
212 time = time / CLOCKS_PER_SEC;
213 MITK_INFO <<
"Registration time: " << time;
215 catch (itk::ExceptionObject &err)
217 std::cerr <<
"ExceptionObject caught !" << std::endl;
218 std::cerr << err << std::endl;
221 typename OptimizerType::ParametersType finalParameters = registration->GetLastTransformParameters();
223 std::cout <<
"Last Transform Parameters" << std::endl;
224 std::cout << finalParameters << std::endl;
226 transform->SetParameters(finalParameters);
242 field->SetRegions(movingRegion);
243 field->SetOrigin(movingImage->GetOrigin());
244 field->SetSpacing(movingImage->GetSpacing());
245 field->SetDirection(movingImage->GetDirection());
248 typedef itk::ImageRegionIterator<DeformationFieldType> FieldIterator;
249 FieldIterator fi(field, movingRegion);
252 typename TransformType::InputPointType fixedPoint;
253 typename TransformType::OutputPointType movingPoint;
254 typename DeformationFieldType::IndexType index;
256 VectorType displacement;
258 while (!fi.IsAtEnd())
260 index = fi.GetIndex();
261 field->TransformIndexToPhysicalPoint(index, fixedPoint);
262 movingPoint = transform->TransformPoint(fixedPoint);
263 displacement = movingPoint - fixedPoint;
264 fi.Set(displacement);
270 warper->SetInput(movingImage);
271 warper->SetInterpolator(interpolator);
272 warper->SetOutputSpacing(movingImage->GetSpacing());
273 warper->SetOutputOrigin(movingImage->GetOrigin());
274 warper->SetOutputDirection(movingImage->GetDirection());
275 warper->SetDisplacementField(field);
289 typedef itk::ImageFileWriter<DeformationFieldType> FieldWriterType;
292 fieldWriter->SetInput(field);
297 fieldWriter->Update();
299 catch (itk::ExceptionObject &excp)
301 std::cerr <<
"Exception thrown " << std::endl;
302 std::cerr << excp << std::endl;
itk::SmartPointer< Self > Pointer
void SetResultFileName(const char *resultName)
Sets the filename for the resulting deformed image.
std::string m_DeformationFileName
mitk::OptimizerParameters::Pointer m_OptimizerParameters
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< const Self > ConstPointer
itk::Vector< float, 3 > VectorType
bool m_SaveDeformationField
RigidRegistrationObserver::Pointer m_Observer
Image::Pointer m_ReferenceImage
virtual ~BSplineRegistration()
Default destructor.
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.
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 SetSaveResult(bool saveResult)
Sets whether the result should be saved or not.
BSplineRegistration()
Default constructor.
const char * m_ResultName
OutputType * GetOutput()
Get the output data of this image source object.
void SetNumberOfIterations(int iterations)
Sets the number of iterations which will be performed during the registration process.
::map::core::RegistrationBase RegistrationType
void GenerateData2(const itk::Image< TPixel, VImageDimension > *itkImage1)
Template class to perform the demons registration with any kind of image. Called by GenerateData()...
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.