11 #include <itkResampleImageFilter.h> 20 template <
unsigned int ImageDimension>
23 m_ReferenceImage = NULL;
24 m_FloatingImage = NULL;
26 m_OutputTransform = BaseTransformType::New();
27 m_OutputTransform->SetIdentity();
29 m_outputTransformFile =
"";
39 m_FiniteStrainImageReorientation =
true;
47 m_MaximumIterations = 10;
48 m_MinimalTransformError = 0.01;
49 m_OptimizerMaximumIterations = 100;
51 m_SearchAngleRadius = 5;
52 m_SearchScaleRadius = 0.1;
53 m_FinalRadius = 0.001;
55 m_TranslateUpperBound = 50;
56 m_AngleUpperBound = 180;
57 m_ScaleUpperBound = 3;
59 m_ExtrapolationSigma = 3;
62 m_MEstimateConvergenceThreshold = 0.01;
63 m_NeighborhoodApproximation = 2.5;
64 m_BCHCompositionOrder = 1;
65 m_ExponentiationOrder = 1;
66 m_NumberOfPyramidLevels = 3;
67 m_LastPyramidLevel = 0;
68 m_PercentageKept = 0.8;
69 this->SetNumberOfWorkUnits(itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads());
72 template <
unsigned int ImageDimension>
77 template <
unsigned int ImageDimension>
85 interpolator->Register();
89 template <
unsigned int ImageDimension>
98 template <
unsigned int ImageDimension>
102 this->SetupPyramids();
105 for (
unsigned int i = 0;i < m_ReferencePyramid->GetNumberOfLevels();++i)
107 if (i + m_LastPyramidLevel >= m_ReferencePyramid->GetNumberOfLevels())
111 refImage->DisconnectPipeline();
114 floImage->DisconnectPipeline();
117 if (m_OutputTransform->GetParametersAsVectorField() != NULL)
119 typedef itk::ResampleImageFilter<VelocityFieldType,VelocityFieldType> VectorResampleFilterType;
120 typedef typename VectorResampleFilterType::Pointer VectorResampleFilterPointer;
123 tmpIdentity->SetIdentity();
125 VectorResampleFilterPointer tmpResample = VectorResampleFilterType::New();
126 tmpResample->SetTransform(tmpIdentity);
127 tmpResample->SetInput(m_OutputTransform->GetParametersAsVectorField());
129 tmpResample->SetSize(refImage->GetLargestPossibleRegion().GetSize());
130 tmpResample->SetOutputOrigin(refImage->GetOrigin());
131 tmpResample->SetOutputSpacing(refImage->GetSpacing());
132 tmpResample->SetOutputDirection(refImage->GetDirection());
134 tmpResample->Update();
137 m_OutputTransform->SetParametersAsVectorField(tmpOut);
138 tmpOut->DisconnectPipeline();
141 std::cout <<
"Processing pyramid level " << i << std::endl;
142 std::cout <<
"Image size: " << refImage->GetLargestPossibleRegion().GetSize() << std::endl;
144 double meanSpacing = 0;
145 for (
unsigned int j = 0;j < ImageDimension;++j)
146 meanSpacing += refImage->GetSpacing()[j];
147 meanSpacing /= ImageDimension;
159 if (this->GetNumberOfWorkUnits() != 0)
165 agreg->
SetDistanceBoundary(m_ExtrapolationSigma * meanSpacing * m_NeighborhoodApproximation);
177 if (this->GetNumberOfWorkUnits() != 0)
198 switch (m_SymmetryType)
203 m_bmreg = BlockMatchRegistrationType::New();
210 typename BlockMatchRegistrationType::Pointer tmpReg = BlockMatchRegistrationType::New();
211 reverseMatcher = this->CreateBlockMatcher();
221 tmpReg->SetReverseBlockMatcher(reverseMatcher);
229 m_bmreg = BlockMatchRegistrationType::New();
234 m_bmreg->SetBlockMatcher(mainMatcher);
235 m_bmreg->SetAgregator(agregPtr);
236 m_bmreg->SetBCHCompositionOrder(m_BCHCompositionOrder);
237 m_bmreg->SetExponentiationOrder(m_ExponentiationOrder);
239 if (this->GetNumberOfWorkUnits() != 0)
240 m_bmreg->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
242 m_bmreg->SetFixedImage(refImage);
243 m_bmreg->SetMovingImage(floImage);
245 m_bmreg->SetSVFElasticRegSigma(m_ElasticSigma * meanSpacing);
251 typename ResampleFilterType::Pointer refResampler = ResampleFilterType::New();
252 refResampler->SetOutputLargestPossibleRegion(floImage->GetLargestPossibleRegion());
253 refResampler->SetOutputOrigin(floImage->GetOrigin());
254 refResampler->SetOutputSpacing(floImage->GetSpacing());
255 refResampler->SetOutputDirection(floImage->GetDirection());
256 refResampler->SetNumberOfWorkUnits(GetNumberOfWorkUnits());
257 refResampler->SetReferenceOutputModel(floImage->GetDescriptionModel());
258 refResampler->SetFiniteStrainReorientation(this->GetFiniteStrainImageReorientation());
259 refResampler->SetInterpolator(interpolator.GetPointer());
260 m_bmreg->SetReferenceImageResampler(refResampler);
262 interpolator = this->CreateInterpolator(refImage);
264 typename ResampleFilterType::Pointer movingResampler = ResampleFilterType::New();
265 movingResampler->SetOutputLargestPossibleRegion(refImage->GetLargestPossibleRegion());
266 movingResampler->SetOutputOrigin(refImage->GetOrigin());
267 movingResampler->SetOutputSpacing(refImage->GetSpacing());
268 movingResampler->SetOutputDirection(refImage->GetDirection());
269 movingResampler->SetNumberOfWorkUnits(GetNumberOfWorkUnits());
270 movingResampler->SetReferenceOutputModel(refImage->GetDescriptionModel());
271 movingResampler->SetFiniteStrainReorientation(this->GetFiniteStrainImageReorientation());
272 movingResampler->SetInterpolator(interpolator.GetPointer());
273 m_bmreg->SetMovingImageResampler(movingResampler);
275 switch (GetTransform())
295 switch (GetOptimizer())
341 switch (m_MetricOrientation)
361 m_bmreg->SetMaximumIterations(m_MaximumIterations);
362 m_bmreg->SetMinimalTransformError(m_MinimalTransformError);
363 m_bmreg->SetInitialTransform(m_OutputTransform.GetPointer());
367 double sr = m_SearchRadius;
370 double sar = m_SearchAngleRadius;
373 double scr = m_SearchScaleRadius;
376 double fr = m_FinalRadius;
379 double ss = m_StepSize;
382 double tub = m_TranslateUpperBound;
385 double aub = m_AngleUpperBound;
388 double scub = m_ScaleUpperBound;
410 catch (itk::ExceptionObject & err)
412 std::cout <<
"Exception: " << err << std::endl;
417 m_OutputTransform->SetParametersAsVectorField(resTrsf->GetParametersAsVectorField());
421 delete reverseMatcher;
429 typedef itk::MultiplyImageFilter <VelocityFieldType,itk::Image <double,ImageDimension>,
VelocityFieldType> MultiplyFilterType;
431 typename MultiplyFilterType::Pointer fieldMultiplier = MultiplyFilterType::New();
432 fieldMultiplier->SetInput(finalTrsfField);
433 fieldMultiplier->SetConstant(2.0);
434 fieldMultiplier->SetNumberOfWorkUnits(GetNumberOfWorkUnits());
435 fieldMultiplier->InPlaceOn();
437 fieldMultiplier->Update();
440 m_OutputTransform->SetParametersAsVectorField(fieldMultiplier->GetOutput());
441 outputField->DisconnectPipeline();
445 anima::GetSVFExponential(m_OutputTransform.GetPointer(), outputDispTrsf.GetPointer(), m_ExponentiationOrder, GetNumberOfWorkUnits(),
false);
448 typename ResampleFilterType::Pointer tmpResample = ResampleFilterType::New();
452 typedef itk::Transform<typename BaseAgregatorType::ScalarType,ImageDimension,ImageDimension>
BaseTransformType;
453 typename BaseTransformType::Pointer baseTrsf = outputDispTrsf.GetPointer();
454 tmpResample->SetTransform(baseTrsf);
455 tmpResample->SetFiniteStrainReorientation(this->GetFiniteStrainImageReorientation());
456 tmpResample->SetInput(m_FloatingImage);
458 if (this->GetNumberOfWorkUnits() != 0)
459 tmpResample->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
461 tmpResample->SetOutputLargestPossibleRegion(m_ReferenceImage->GetLargestPossibleRegion());
462 tmpResample->SetOutputOrigin(m_ReferenceImage->GetOrigin());
463 tmpResample->SetOutputSpacing(m_ReferenceImage->GetSpacing());
464 tmpResample->SetOutputDirection(m_ReferenceImage->GetDirection());
465 tmpResample->SetReferenceOutputModel(m_ReferenceImage->GetDescriptionModel());
466 tmpResample->SetInterpolator(interpolator.GetPointer());
467 tmpResample->Update();
469 m_OutputImage = tmpResample->GetOutput();
470 m_OutputImage->DisconnectPipeline();
473 template <
unsigned int ImageDimension>
477 std::cout <<
"Writing output image to: " << m_resultFile << std::endl;
484 if (m_outputTransformFile !=
"")
486 std::cout <<
"Writing output SVF to: " << m_outputTransformFile << std::endl;
487 anima::writeImage <VelocityFieldType> (m_outputTransformFile,
488 const_cast <
VelocityFieldType *> (m_OutputTransform->GetParametersAsVectorField()));
492 template <
unsigned int ImageDimension>
497 m_ReferencePyramid = PyramidType::New();
499 m_ReferencePyramid->SetInput(m_ReferenceImage);
500 m_ReferencePyramid->SetNumberOfLevels(m_NumberOfPyramidLevels);
502 if (this->GetNumberOfWorkUnits() != 0)
503 m_ReferencePyramid->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
508 typename ResampleFilterType::Pointer refResampler = ResampleFilterType::New();
509 refResampler->SetReferenceOutputModel(m_ReferenceImage->GetDescriptionModel());
510 refResampler->SetFiniteStrainReorientation(this->GetFiniteStrainImageReorientation());
511 refResampler->SetInterpolator(interpolator.GetPointer());
512 m_ReferencePyramid->SetImageResampler(refResampler);
514 m_ReferencePyramid->Update();
517 m_FloatingPyramid = PyramidType::New();
519 m_FloatingPyramid->SetInput(m_FloatingImage);
520 m_FloatingPyramid->SetNumberOfLevels(m_NumberOfPyramidLevels);
522 if (this->GetNumberOfWorkUnits() != 0)
523 m_FloatingPyramid->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
525 typename ResampleFilterType::Pointer floResampler = ResampleFilterType::New();
526 interpolator = this->CreateInterpolator(m_FloatingImage);
528 floResampler->SetReferenceOutputModel(m_FloatingImage->GetDescriptionModel());
529 floResampler->SetFiniteStrainReorientation(this->GetFiniteStrainImageReorientation());
530 floResampler->SetInterpolator(interpolator.GetPointer());
531 m_FloatingPyramid->SetImageResampler(floResampler);
533 m_FloatingPyramid->Update();
MEstimateAgregatorType::BaseOutputTransformType BaseTransformType
MCMPointer & GetDescriptionModel()
void SetGradientDirections(std::vector< vnl_vector_fixed< double, 3 > > &grads)
void SetFinalRadius(double val)
void SetBigDelta(double val)
void SetTranslateMax(double val)
void SetSearchAngleRadius(double val)
void SetBlockSize(unsigned int val)
void SetSmallDelta(double val)
void SetNumberOfWorkUnits(unsigned int val)
void SetOptimizerType(OptimizerDefinition val)
void SetInputImage(InputImageType *input)
const double DiffusionBigDelta
Default big delta value (classical values)
DisplacementFieldTransformType::Pointer DisplacementFieldTransformPointer
void SetSearchScaleRadius(double val)
void SetGradientStrengths(std::vector< double > &val)
virtual ~PyramidalDenseMCMSVFMatchingBridge()
const double DiffusionSmallDelta
Default small delta value (classical values)
itk::SmartPointer< Self > Pointer
void SetSimilarityType(SimilarityDefinition val)
virtual InterpolatorType * CreateInterpolator(InputImageType *image)
void SetScaleMax(double val)
virtual BlockMatcherType * CreateBlockMatcher()
void SetBlockSpacing(unsigned int val)
void Update() ITK_OVERRIDE
BaseTransformType::VectorFieldType VelocityFieldType
void SetFileName(std::string fileName)
void SetStepSize(double val)
void SetBlockPercentageKept(double val)
void SetOptimizerMaximumIterations(unsigned int val)
void GetSVFExponential(itk::StationaryVelocityFieldTransform< ScalarType, NDimensions > *baseTrsf, rpi::DisplacementFieldTransform< ScalarType, NDimensions > *resultTransform, unsigned int exponentiationOrder, unsigned int numThreads, bool invert)
PyramidalDenseMCMSVFMatchingBridge()
void SetBlockTransformType(TransformDefinition val)
void SetAngleMax(double val)
void SetModelRotationType(ModelRotationType val)
void SetBlockVarianceThreshold(double val)
itk::SmartPointer< Self > Pointer
AffineTransformType::Pointer AffineTransformPointer
void SetSearchRadius(double val)