1#========================================================================== 2# 3# Copyright Insight Software Consortium 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0.txt 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17#==========================================================================*/ 18 19 20package require InsightToolkit 21package require itkinteraction 22 23 24set registration [ itkImageRegistrationMethodF2F2_New ] 25set imageMetric [ itkMeanSquaresImageToImageMetricF2F2_New ] 26set transform [ itkCenteredRigid2DTransform_New ] 27set optimizer [ itkRegularStepGradientDescentOptimizer_New ] 28set interpolator [ itkLinearInterpolateImageFunctionF2D_New ] 29 30$registration SetOptimizer [ $optimizer GetPointer ] 31$registration SetTransform [ $transform GetPointer ] 32$registration SetInterpolator [ $interpolator GetPointer ] 33$registration SetMetric [ $imageMetric GetPointer ] 34 35set fixedImageReader [ itkImageFileReaderF2_New ] 36set movingImageReader [ itkImageFileReaderF2_New ] 37 38$fixedImageReader SetFileName [lindex $argv 0] 39$movingImageReader SetFileName [lindex $argv 1] 40 41$registration SetFixedImage [ $fixedImageReader GetOutput ] 42$registration SetMovingImage [ $movingImageReader GetOutput ] 43 44$fixedImageReader Update 45$movingImageReader Update 46 47set fixedImage [ $fixedImageReader GetOutput ] 48set movingImage [ $movingImageReader GetOutput ] 49 50$registration SetFixedImageRegion [ $fixedImage GetBufferedRegion ] 51 52$transform SetIdentity 53set initialParameters [ $transform GetParameters ] 54 55$registration SetInitialTransformParameters $initialParameters 56 57 58 59$optimizer SetMaximumStepLength 4.00 60$optimizer SetMinimumStepLength 0.01 61$optimizer SetNumberOfIterations 200 62 63 64set command [itkTclCommand_New] 65$command SetInterpreter [GetInterp] 66$command SetCommandString { 67set currentParameter [$transform GetParameters] 68puts "M: [$optimizer GetValue] P: [$currentParameter GetElement 0 ] [$currentParameter GetElement 1 ] "} 69 70$optimizer AddObserver [itkIterationEvent] [$command GetPointer] 71 72 73 74# Here the registration is done 75$registration Update 76 77 78# Get the final parameters of the transformation 79set finalParameters [$registration GetLastTransformParameters] 80 81 82# Print them out 83puts "Final Registration Parameters " 84puts "Angle in radians = [$finalParameters GetElement 0]" 85puts "Rotation Center X = [$finalParameters GetElement 1]" 86puts "Rotation Center Y = [$finalParameters GetElement 2]" 87puts "Translation in X = [$finalParameters GetElement 3]" 88puts "Translation in Y = [$finalParameters GetElement 4]" 89 90 91# Now, 92# we use the final transform for resampling the 93# moving image. 94set resampler [itkResampleImageFilterF2F2_New ] 95 96$resampler SetTransform [$transform GetPointer] 97$resampler SetInput $movingImage 98 99set region [ $fixedImage GetLargestPossibleRegion ] 100 101$resampler SetSize [ $region GetSize ] 102 103$resampler SetOutputSpacing [ $fixedImage GetSpacing ] 104$resampler SetOutputOrigin [ $fixedImage GetOrigin ] 105$resampler SetDefaultPixelValue 100 106 107set outputCast [itkRescaleIntensityImageFilterF2UC2_New] 108$outputCast SetOutputMinimum 0 109$outputCast SetOutputMaximum 65535 110$outputCast SetInput [$resampler GetOutput] 111 112set writer [ itkImageFileWriterUC2_New ] 113 114$writer SetFileName [lindex $argv 2] 115$writer SetInput [ $outputCast GetOutput ] 116$writer Update 117 118 119 120wm withdraw . 121exit 122