A SimpleITK example demonstrating image registration using the correlation metric and the center of mass initial transformation estimation method.
(args)
| 38 | |
| 39 | |
| 40 | def main(args): |
| 41 | """ A SimpleITK example demonstrating image registration using the |
| 42 | correlation metric and the center of mass initial transformation |
| 43 | estimation method. """ |
| 44 | if len(args) < 3: |
| 45 | print( |
| 46 | "Usage:", |
| 47 | "ImageRegistrationMethod3" |
| 48 | "<fixedImageFilter> <movingImageFile> <outputTransformFile>", |
| 49 | ) |
| 50 | sys.exit(1) |
| 51 | |
| 52 | fixed = sitk.ReadImage(args[1], sitk.sitkFloat32) |
| 53 | |
| 54 | moving = sitk.ReadImage(args[2], sitk.sitkFloat32) |
| 55 | |
| 56 | R = sitk.ImageRegistrationMethod() |
| 57 | |
| 58 | R.SetMetricAsCorrelation() |
| 59 | |
| 60 | R.SetOptimizerAsRegularStepGradientDescent( |
| 61 | learningRate=2.0, |
| 62 | minStep=1e-4, |
| 63 | numberOfIterations=500, |
| 64 | gradientMagnitudeTolerance=1e-8, |
| 65 | ) |
| 66 | R.SetOptimizerScalesFromIndexShift() |
| 67 | |
| 68 | tx = sitk.CenteredTransformInitializer(fixed, moving, sitk.Similarity2DTransform()) |
| 69 | R.SetInitialTransform(tx) |
| 70 | |
| 71 | R.SetInterpolator(sitk.sitkLinear) |
| 72 | |
| 73 | R.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(R)) |
| 74 | |
| 75 | outTx = R.Execute(fixed, moving) |
| 76 | |
| 77 | print("-------") |
| 78 | print(outTx) |
| 79 | print(f"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}") |
| 80 | print(f" Iteration: {R.GetOptimizerIteration()}") |
| 81 | print(f" Metric value: {R.GetMetricValue()}") |
| 82 | |
| 83 | sitk.WriteTransform(outTx, args[3]) |
| 84 | |
| 85 | resampler = sitk.ResampleImageFilter() |
| 86 | resampler.SetReferenceImage(fixed) |
| 87 | resampler.SetInterpolator(sitk.sitkLinear) |
| 88 | resampler.SetDefaultPixelValue(100) |
| 89 | resampler.SetTransform(outTx) |
| 90 | |
| 91 | out = resampler.Execute(moving) |
| 92 | |
| 93 | simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8) |
| 94 | simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8) |
| 95 | cimg = sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0) |
| 96 | |
| 97 | return {"fixed": fixed, "moving": moving, "composition": cimg} |
no test coverage detected