SimpleITK  
ImageRegistrationMethodBSpline1/ImageRegistrationMethodBSpline1.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.
*
*=========================================================================*/
using System;
using itk.simple;
namespace itk.simple.examples
{
class IterationUpdate : Command
{
private ImageRegistrationMethod m_Method;
public IterationUpdate(ImageRegistrationMethod m)
{
m_Method = m;
}
public override void Execute()
{
Console.WriteLine("{0,3} = {1,10:F5}",
m_Method.GetMetricValue());
}
}
class ImageRegistrationMethodBSpline1
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Usage: {0} <fixedImageFile> <movingImageFile> <outputTransformFile>",
"ImageRegistrationMethodBSpline1");
return;
}
Image fixedImage = SimpleITK.ReadImage(args[0], PixelIDValueEnum.sitkFloat32);
Image movingImage = SimpleITK.ReadImage(args[1], PixelIDValueEnum.sitkFloat32);
// Create transform domain mesh size vector
VectorUInt32 transformDomainMeshSize = new VectorUInt32();
for (uint i = 0; i < movingImage.GetDimension(); i++)
{
transformDomainMeshSize.Add(8);
}
Transform tx = SimpleITK.BSplineTransformInitializer(fixedImage, transformDomainMeshSize);
Console.WriteLine("Initial Parameters:");
VectorDouble initialParams = tx.GetParameters();
Console.Write("[" + initialParams[0].ToString("F5"));
for (int i = 1; i < initialParams.Count; i++)
{
Console.Write(", " + initialParams[i].ToString("F5"));
}
Console.WriteLine("]");
R.SetOptimizerAsLBFGSB(gradientConvergenceTolerance: 1e-5,
numberOfIterations: 100,
maximumNumberOfCorrections: 5,
maximumNumberOfFunctionEvaluations: 1000,
costFunctionConvergenceFactor: 1e7);
R.SetInitialTransform(tx, true);
IterationUpdate cmd = new IterationUpdate(R);
R.AddCommand(EventEnum.sitkIterationEvent, cmd);
Transform outTx = R.Execute(fixedImage, movingImage);
Console.WriteLine("-------");
Console.WriteLine(outTx.ToString());
Console.WriteLine("Optimizer stop condition: " + R.GetOptimizerStopConditionDescription());
Console.WriteLine(" Iteration: " + R.GetOptimizerIteration());
Console.WriteLine(" Metric value: " + R.GetMetricValue());
SimpleITK.WriteTransform(outTx, args[2]);
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));
if (Environment.GetEnvironmentVariable("SITK_NOSHOW") == null)
{
SimpleITK.Show(cimg, "ImageRegistrationMethodBSpline1 Composition");
}
}
}
}
An implementation of the Command design pattern for callback.
Definition sitkCommand.h:45
An interface method to the modular ITKv4 registration framework.
void SetMetricAsCorrelation()
Use negative normalized cross correlation image metric.
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 SetOptimizerAsLBFGSB(double gradientConvergenceTolerance=1e-5, unsigned int numberOfIterations=500, unsigned int maximumNumberOfCorrections=5, unsigned int maximumNumberOfFunctionEvaluations=2000, double costFunctionConvergenceFactor=1e+7, double lowerBound=std::numeric_limits< double >::min(), double upperBound=std::numeric_limits< double >::max(), bool trace=false)
Limited memory Broyden Fletcher Goldfarb Shannon minimization with simple bounds.
std::string GetOptimizerStopConditionDescription() const
unsigned int GetOptimizerIteration() const
void SetInitialTransform(const Transform &transform)
Set the initial transform and parameters 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
void WriteTransform(const std::string &filename) const
std::vector< double > GetParameters() const
EventEnum
Events which can be observed from ProcessObject.
Definition sitkEvent.h:32
PixelIDValueEnum
Enumerated values of pixelIDs.