Example #1
0
 static void optimizer_Iteration(itkObject sender, itkEventArgs e)
 {
     OptimizerType optimizer = sender as OptimizerType;
     String message = "{0}:   {1}";
     String iteration = optimizer.CurrentIteration.ToString("000");
     String position = optimizer.CurrentPosition.ToString();
     Console.WriteLine(String.Format(message, iteration, position));
 }
Example #2
0
    static void Main(string[] args)
    {
        try
        {
            // Read the fixed image from the command line
            itkImageBase imageFixed = ImageType.New();
            imageFixed.Read(args[0]);

            // Create a moving image
            itkImageBase imageMoving = itkImage.New(imageFixed);

            // Create the interpolator
            InterpolatorType interpolator = InterpolatorType.New();

            // Create the transform
            TransformType transform = TransformType.New();
            transform.Translate(new itkVector(7.5, 12.0));
            Console.WriteLine("START: " + transform.Parameters.ToString());

            // Make the moving image by resampling the fixed image
            // with known parameters
            ResampleType filterResample = ResampleType.New();
            filterResample.SetInput(imageFixed);
            filterResample.SetInterpolator(interpolator);
            filterResample.SetTransform(transform);
            filterResample.OutputSize = imageFixed.Size;
            filterResample.OutputSpacing = imageFixed.Spacing;
            filterResample.Update();
            filterResample.GetOutput(imageMoving);
            imageMoving.DisconnectPipeline();
            imageFixed.DisconnectPipeline();

            // Write out the fixed and moving images
            imageFixed.Write(AddSuffixToFileName(args[0], "_FIXED"));
            imageMoving.Write(AddSuffixToFileName(args[0], "_MOVING"));

            // Reset the transform initial parameters
            transform.Translate(new itkVector(0.0, 0.0));

            // Create metric
            MetricType metric = MetricType.New();
            
            // Create optimiser
            OptimizerType optimizer = OptimizerType.New();
            optimizer.Iteration += new itkEventHandler(optimizer_Iteration);
            optimizer.MaximumStepLength = 4.00;
            optimizer.MinimumStepLength = 0.01;
            optimizer.NumberOfIterations = 200;

            // Create registration method
            RegistrationType registration = RegistrationType.New();
            registration.SetFixedImage(imageFixed);
            registration.SetMovingImage(imageMoving);
            registration.SetTransform(transform);
            registration.SetInterpolator(interpolator);
            registration.SetMetric(metric);
            registration.SetOptimizer(optimizer);
            registration.InitialTransformParameters = transform.Parameters;
            registration.StartRegistration();

            // Rotate the moving image with the found parameters
            filterResample.SetInput(imageMoving);
            filterResample.SetInterpolator(interpolator);
            filterResample.SetTransform(transform);
            filterResample.OutputSize = imageMoving.Size;
            filterResample.OutputSpacing = imageMoving.Spacing;
            filterResample.Update();
            filterResample.GetOutput(imageMoving);
            imageMoving.DisconnectPipeline();

            // Write out the results
            Console.WriteLine("END:   " + transform.Parameters.ToString());
            imageMoving.Write(AddSuffixToFileName(args[0], "_REGISTERED"));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }