SimpleITK  
ImageRegistrationMethodBSpline3/ImageRegistrationMethodBSpline3.cs
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
// A SimpleITK example demonstrating image registration using the
// BSplineTransform and the JointHistogramMutualInformation metric.
using System;
using itk.simple;
namespace itk.simple.examples
{
class IterationUpdate : Command
{
private ImageRegistrationMethod m_Method;
private Transform m_BSplineTransform;
public IterationUpdate(ImageRegistrationMethod m, Transform bsplineTransform)
{
m_Method = m;
m_BSplineTransform = bsplineTransform;
}
public override void Execute()
{
if (m_Method.GetOptimizerIteration() == 0)
{
// The BSpline is resized before the first optimizer
// iteration is completed per level. Print the transform object
// to show the adapted BSpline transform.
Console.WriteLine(m_BSplineTransform.ToString());
}
Console.WriteLine(String.Format("{0,3} = {1,10:F5}",
m_Method.GetMetricValue()));
}
}
class MultiResolutionIterationUpdate : Command
{
private ImageRegistrationMethod m_Method;
public MultiResolutionIterationUpdate(ImageRegistrationMethod m)
{
m_Method = m;
}
public override void Execute()
{
if (m_Method.GetCurrentLevel() > 0)
{
Console.WriteLine(String.Format("Optimizer stop condition: {0}",
Console.WriteLine(String.Format(" Iteration: {0}", m_Method.GetOptimizerIteration()));
Console.WriteLine(String.Format(" Metric value: {0}", m_Method.GetMetricValue()));
}
Console.WriteLine("--------- Resolution Changing ---------");
}
}
class ImageRegistrationMethodBSpline3
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Usage: {0} <fixedImageFile> <movingImageFile> <outputTransformFile>",
System.AppDomain.CurrentDomain.FriendlyName);
return;
}
Image fixedImage = SimpleITK.ReadImage(args[0], PixelIDValueEnum.sitkFloat32);
Image movingImage = SimpleITK.ReadImage(args[1], PixelIDValueEnum.sitkFloat32);
VectorUInt32 transformDomainMeshSize = new VectorUInt32();
for (uint i = 0; i < fixedImage.GetDimension(); i++)
{
transformDomainMeshSize.Add(2);
}
BSplineTransform tx = SimpleITK.BSplineTransformInitializer(fixedImage, transformDomainMeshSize);
Console.WriteLine(String.Format("Initial Number of Parameters: {0}", tx.GetNumberOfParameters()));
double learningRate = 5.0;
uint numberOfIterations = 100;
double convergenceMinimumValue = 1e-4;
uint convergenceWindowSize = 5;
R.SetOptimizerAsGradientDescentLineSearch(learningRate, numberOfIterations,
convergenceMinimumValue, convergenceWindowSize);
VectorUInt32 scaleFactors = new VectorUInt32();
scaleFactors.Add(1); scaleFactors.Add(2); scaleFactors.Add(5);
R.SetInitialTransformAsBSpline(tx, true, scaleFactors);
VectorUInt32 shrinkFactors = new VectorUInt32();
shrinkFactors.Add(4); shrinkFactors.Add(2); shrinkFactors.Add(1);
R.SetShrinkFactorsPerLevel(shrinkFactors);
VectorDouble smoothingSigmas = new VectorDouble();
smoothingSigmas.Add(4); smoothingSigmas.Add(2); smoothingSigmas.Add(1);
R.SetSmoothingSigmasPerLevel(smoothingSigmas);
IterationUpdate cmd1 = new IterationUpdate(R, tx);
R.AddCommand(EventEnum.sitkIterationEvent, cmd1);
MultiResolutionIterationUpdate cmd2 = new MultiResolutionIterationUpdate(R);
R.AddCommand(EventEnum.sitkMultiResolutionIterationEvent, cmd2);
Transform outTx = R.Execute(fixedImage, movingImage);
Console.WriteLine("-------");
Console.WriteLine(tx.ToString());
Console.WriteLine(outTx.ToString());
Console.WriteLine(String.Format("Optimizer stop condition: {0}", R.GetOptimizerStopConditionDescription()));
Console.WriteLine(String.Format(" Iteration: {0}", R.GetOptimizerIteration()));
Console.WriteLine(String.Format(" Metric value: {0}", R.GetMetricValue()));
SimpleITK.WriteTransform(outTx, args[2]);
if (Environment.GetEnvironmentVariable("SITK_NOSHOW") == null)
{
resampler.SetReferenceImage(fixedImage);
resampler.SetInterpolator(InterpolatorEnum.sitkLinear);
resampler.SetDefaultPixelValue(100);
resampler.SetTransform(outTx);
Image outputImage = resampler.Execute(movingImage);
Image simg1 = SimpleITK.Cast(SimpleITK.RescaleIntensity(fixedImage), PixelIDValueEnum.sitkUInt8);
Image simg2 = SimpleITK.Cast(SimpleITK.RescaleIntensity(outputImage), PixelIDValueEnum.sitkUInt8);
Image cimg = SimpleITK.Compose(simg1, simg2, SimpleITK.Divide(SimpleITK.Add(simg1, simg2), 2.0));
SimpleITK.Show(cimg, "Image Registration Composition");
}
}
}
}
A deformable transform over a bounded spatial domain using a BSpline representation for a 2D or 3D co...
An implementation of the Command design pattern for callback.
Definition sitkCommand.h:45
An interface method to the modular ITKv4 registration framework.
void SetInterpolator(InterpolatorEnum Interpolator)
Set and get the interpolator to use.
Transform Execute(const Image &fixed, const Image &moving)
Optimize the configured registration problem.
void SetMetricAsJointHistogramMutualInformation(unsigned int numberOfHistogramBins=20, double varianceForJointPDFSmoothing=1.5)
Use mutual information between two images.
void SetOptimizerAsGradientDescentLineSearch(double learningRate, unsigned int numberOfIterations, double convergenceMinimumValue=1e-6, unsigned int convergenceWindowSize=10, double lineSearchLowerLimit=0, double lineSearchUpperLimit=5.0, double lineSearchEpsilon=0.01, unsigned int lineSearchMaximumIterations=20, EstimateLearningRateType estimateLearningRate=Once, double maximumStepSizeInPhysicalUnits=0.0)
Gradient descent optimizer with a golden section line search.
void SetShrinkFactorsPerLevel(const std::vector< unsigned int > &shrinkFactors)
Set the isotropic shrink factors for each level.
std::string GetOptimizerStopConditionDescription() const
void SetSmoothingSigmasPerLevel(const std::vector< double > &smoothingSigmas)
Set the sigmas of Gaussian used for smoothing.
unsigned int GetCurrentLevel() const
unsigned int GetOptimizerIteration() const
void SetInitialTransformAsBSpline(BSplineTransform &transform, bool inPlace=true, const std::vector< unsigned int > &scaleFactors=std::vector< unsigned int >())
Set an initial BSpline transform to optimize.
The Image class for SimpleITK.
Definition sitkImage.h:77
unsigned int GetDimension() const
virtual int AddCommand(itk::simple::EventEnum event, itk::simple::Command &cmd)
Add a Command Object to observer the event.
Resample an image via a coordinate transform.
Image Execute(const Image &image1)
void SetInterpolator(InterpolatorEnum Interpolator)
void SetReferenceImage(const Image &refImage)
void SetDefaultPixelValue(double DefaultPixelValue)
A simplified wrapper around a variety of ITK transforms.
std::string ToString() const
unsigned int GetNumberOfParameters() const
void WriteTransform(const std::string &filename) const
EventEnum
Events which can be observed from ProcessObject.
Definition sitkEvent.h:32
PixelIDValueEnum
Enumerated values of pixelIDs.