A basic SimpleITK image registration example.
(args)
| 35 | |
| 36 | |
| 37 | def main(args): |
| 38 | """ A basic SimpleITK image registration example. """ |
| 39 | |
| 40 | if len(args) < 3: |
| 41 | print( |
| 42 | "Usage:", |
| 43 | "ImageRegistrationMethod1", |
| 44 | "<fixedImageFilter> <movingImageFile>", |
| 45 | "<outputTransformFile>", |
| 46 | ) |
| 47 | sys.exit(1) |
| 48 | |
| 49 | fixed = sitk.ReadImage(args[1], sitk.sitkFloat32) |
| 50 | |
| 51 | moving = sitk.ReadImage(args[2], sitk.sitkFloat32) |
| 52 | |
| 53 | R = sitk.ImageRegistrationMethod() |
| 54 | R.SetMetricAsMeanSquares() |
| 55 | R.SetOptimizerAsRegularStepGradientDescent(4.0, 0.01, 200) |
| 56 | R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension())) |
| 57 | R.SetInterpolator(sitk.sitkLinear) |
| 58 | |
| 59 | R.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(R)) |
| 60 | |
| 61 | outTx = R.Execute(fixed, moving) |
| 62 | |
| 63 | print("-------") |
| 64 | print(outTx) |
| 65 | print(f"Optimizer stop condition: {R.GetOptimizerStopConditionDescription()}") |
| 66 | print(f" Iteration: {R.GetOptimizerIteration()}") |
| 67 | print(f" Metric value: {R.GetMetricValue()}") |
| 68 | |
| 69 | sitk.WriteTransform(outTx, args[3]) |
| 70 | |
| 71 | resampler = sitk.ResampleImageFilter() |
| 72 | resampler.SetReferenceImage(fixed) |
| 73 | resampler.SetInterpolator(sitk.sitkLinear) |
| 74 | resampler.SetDefaultPixelValue(100) |
| 75 | resampler.SetTransform(outTx) |
| 76 | |
| 77 | out = resampler.Execute(moving) |
| 78 | simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8) |
| 79 | simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8) |
| 80 | cimg = sitk.Compose(simg1, simg2, simg1 // 2.0 + simg2 // 2.0) |
| 81 | |
| 82 | return_images = {"fixed": fixed, "moving": moving, "composition": cimg} |
| 83 | return return_images |
| 84 | |
| 85 | |
| 86 | if __name__ == "__main__": |
no test coverage detected