2
#=========================================================================
4
# Copyright Insight Software Consortium
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0.txt
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
18
#=========================================================================
20
from __future__ import print_function
22
import SimpleITK as sitk
27
def command_iteration(method) :
28
print("{0:3} = {1:10.5f} : {2}".format(method.GetOptimizerIteration(),
29
method.GetMetricValue(),
30
method.GetOptimizerPosition()))
32
if len ( sys.argv ) < 4:
33
print( "Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile>".format(sys.argv[0]))
37
fixed = sitk.ReadImage(sys.argv[1], sitk.sitkFloat32)
39
moving = sitk.ReadImage(sys.argv[2], sitk.sitkFloat32)
41
R = sitk.ImageRegistrationMethod()
42
R.SetMetricAsMeanSquares()
43
R.SetOptimizerAsRegularStepGradientDescent(4.0, .01, 200 )
44
R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension()))
45
R.SetInterpolator(sitk.sitkLinear)
47
R.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(R) )
49
outTx = R.Execute(fixed, moving)
53
print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
54
print(" Iteration: {0}".format(R.GetOptimizerIteration()))
55
print(" Metric value: {0}".format(R.GetMetricValue()))
57
sitk.WriteTransform(outTx, sys.argv[3])
59
if ( not "SITK_NOSHOW" in os.environ ):
61
resampler = sitk.ResampleImageFilter()
62
resampler.SetReferenceImage(fixed);
63
resampler.SetInterpolator(sitk.sitkLinear)
64
resampler.SetDefaultPixelValue(100)
65
resampler.SetTransform(outTx)
67
out = resampler.Execute(moving)
68
simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
69
simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
70
cimg = sitk.Compose(simg1, simg2, simg1/2.+simg2/2.)
71
sitk.Show( cimg, "ImageRegistration1 Composition" )