0
# Geometric Transforms
1
2
Comprehensive framework for spatial transformations including rigid, affine, and non-linear deformations used in image registration, resampling, and geometric operations. ITK's transform framework provides a unified interface for all types of spatial mappings with support for composition, inversion, and parameter optimization.
3
4
## Capabilities
5
6
### Base Transform Classes
7
8
Foundation classes that define the transform interface and common functionality.
9
10
```python { .api }
11
class Transform[TParametersValueType, NInputDimensions, NOutputDimensions]:
12
"""
13
Base class for all spatial transformations.
14
15
Template Parameters:
16
- TParametersValueType: Type for transform parameters (typically float or double)
17
- NInputDimensions: Dimensionality of input space
18
- NOutputDimensions: Dimensionality of output space
19
"""
20
def TransformPoint(self, point: Point[TParametersValueType, NInputDimensions]) -> Point[TParametersValueType, NOutputDimensions]: ...
21
def TransformVector(self, vector: Vector[TParametersValueType, NInputDimensions]) -> Vector[TParametersValueType, NOutputDimensions]: ...
22
def TransformCovariantVector(self, vector: CovariantVector[TParametersValueType, NInputDimensions]) -> CovariantVector[TParametersValueType, NOutputDimensions]: ...
23
def GetParameters(self) -> ParametersType: ...
24
def SetParameters(self, parameters: ParametersType) -> None: ...
25
def GetFixedParameters(self) -> FixedParametersType: ...
26
def SetFixedParameters(self, parameters: FixedParametersType) -> None: ...
27
def GetNumberOfParameters(self) -> int: ...
28
def GetTransformCategory(self) -> TransformCategoryEnum: ...
29
def ComputeJacobianWithRespectToParameters(self, point: Point[TParametersValueType, NInputDimensions]) -> JacobianType: ...
30
def IsLinear(self) -> bool: ...
31
def GetInverse(self) -> InverseTransformBasePointer: ...
32
33
class MatrixOffsetTransformBase[TParametersValueType, NInputDimensions, NOutputDimensions]:
34
"""Base class for linear transformations using matrix and offset representation."""
35
def GetMatrix(self) -> Matrix[TParametersValueType, NInputDimensions, NOutputDimensions]: ...
36
def SetMatrix(self, matrix: Matrix[TParametersValueType, NInputDimensions, NOutputDimensions]) -> None: ...
37
def GetOffset(self) -> OutputVectorType: ...
38
def SetOffset(self, offset: OutputVectorType) -> None: ...
39
def GetTranslation(self) -> OutputVectorType: ...
40
def SetTranslation(self, translation: OutputVectorType) -> None: ...
41
def GetCenter(self) -> InputPointType: ...
42
def SetCenter(self, center: InputPointType) -> None: ...
43
def Compose(self, other: MatrixOffsetTransformBase) -> None: ...
44
def GetInverseMatrix(self) -> InverseMatrixType: ...
45
46
class CompositeTransform[TParametersValueType, NDimensions]:
47
"""Composition of multiple transforms applied in sequence."""
48
def AddTransform(self, transform: TransformType) -> None: ...
49
def RemoveTransform(self) -> None: ...
50
def GetNumberOfTransforms(self) -> int: ...
51
def GetNthTransform(self, n: int) -> TransformType: ...
52
def SetNthTransform(self, n: int, transform: TransformType) -> None: ...
53
def ClearTransformQueue(self) -> None: ...
54
def IsTransformQueueEmpty(self) -> bool: ...
55
```
56
57
### Translation and Scaling
58
59
Basic geometric transformations for translation and scaling operations.
60
61
```python { .api }
62
class TranslationTransform[TParametersValueType, NDimensions]:
63
"""Pure translation transformation."""
64
def New() -> TranslationTransform[TParametersValueType, NDimensions]: ...
65
def GetOffset(self) -> OutputVectorType: ...
66
def SetOffset(self, offset: OutputVectorType) -> None: ...
67
def Translate(self, offset: OutputVectorType) -> None: ...
68
def GetInverse(self) -> InverseTransformBasePointer: ...
69
70
class ScaleTransform[TParametersValueType, NDimensions]:
71
"""Non-uniform scaling transformation."""
72
def New() -> ScaleTransform[TParametersValueType, NDimensions]: ...
73
def GetScale(self) -> ParametersType: ...
74
def SetScale(self, scale: ParametersType) -> None: ...
75
def GetCenter(self) -> InputPointType: ...
76
def SetCenter(self, center: InputPointType) -> None: ...
77
78
class ScaleSkewVersor3DTransform[TParametersValueType]:
79
"""3D transformation with rotation, translation, scaling, and skew."""
80
def New() -> ScaleSkewVersor3DTransform[TParametersValueType]: ...
81
def SetScale(self, scale: ScaleVectorType) -> None: ...
82
def GetScale(self) -> ScaleVectorType: ...
83
def SetSkew(self, skew: SkewVectorType) -> None: ...
84
def GetSkew(self) -> SkewVectorType: ...
85
def SetVersor(self, versor: VersorType) -> None: ...
86
def GetVersor(self) -> VersorType: ...
87
```
88
89
### Rigid Transformations
90
91
Rotation and translation transformations that preserve distances and angles.
92
93
```python { .api }
94
class Euler2DTransform[TParametersValueType]:
95
"""2D rotation and translation using Euler angle representation."""
96
def New() -> Euler2DTransform[TParametersValueType]: ...
97
def SetAngle(self, angle: TParametersValueType) -> None: ...
98
def GetAngle(self) -> TParametersValueType: ...
99
def SetRotation(self, angle: TParametersValueType) -> None: ...
100
def SetTranslation(self, translation: OutputVectorType) -> None: ...
101
def GetTranslation(self) -> OutputVectorType: ...
102
def SetCenter(self, center: InputPointType) -> None: ...
103
def GetCenter(self) -> InputPointType: ...
104
105
class Euler3DTransform[TParametersValueType]:
106
"""3D rotation and translation using Euler angles."""
107
def New() -> Euler3DTransform[TParametersValueType]: ...
108
def SetRotation(self, angleX: TParametersValueType, angleY: TParametersValueType, angleZ: TParametersValueType) -> None: ...
109
def SetRotationAboutX(self, angle: TParametersValueType) -> None: ...
110
def SetRotationAboutY(self, angle: TParametersValueType) -> None: ...
111
def SetRotationAboutZ(self, angle: TParametersValueType) -> None: ...
112
def GetAngleX(self) -> TParametersValueType: ...
113
def GetAngleY(self) -> TParametersValueType: ...
114
def GetAngleZ(self) -> TParametersValueType: ...
115
def SetComputeZYX(self, flag: bool) -> None: ...
116
117
class Rigid2DTransform[TParametersValueType]:
118
"""2D rigid transformation (rotation + translation)."""
119
def New() -> Rigid2DTransform[TParametersValueType]: ...
120
def SetAngle(self, angle: TParametersValueType) -> None: ...
121
def GetAngle(self) -> TParametersValueType: ...
122
def SetMatrix(self, matrix: MatrixType) -> None: ...
123
def GetMatrix(self) -> MatrixType: ...
124
125
class Rigid3DTransform[TParametersValueType]:
126
"""3D rigid transformation (rotation + translation)."""
127
def New() -> Rigid3DTransform[TParametersValueType]: ...
128
def SetMatrix(self, matrix: MatrixType) -> None: ...
129
def GetMatrix(self) -> MatrixType: ...
130
def SetRotation(self, matrix: MatrixType) -> None: ...
131
def GetRotation(self) -> MatrixType: ...
132
```
133
134
### Similarity Transformations
135
136
Transformations that preserve shape but allow uniform scaling.
137
138
```python { .api }
139
class Similarity2DTransform[TParametersValueType]:
140
"""2D similarity transformation (rotation + translation + uniform scaling)."""
141
def New() -> Similarity2DTransform[TParametersValueType]: ...
142
def SetScale(self, scale: TParametersValueType) -> None: ...
143
def GetScale(self) -> TParametersValueType: ...
144
def SetAngle(self, angle: TParametersValueType) -> None: ...
145
def GetAngle(self) -> TParametersValueType: ...
146
def SetTranslation(self, translation: OutputVectorType) -> None: ...
147
def GetTranslation(self) -> OutputVectorType: ...
148
149
class Similarity3DTransform[TParametersValueType]:
150
"""3D similarity transformation (rotation + translation + uniform scaling)."""
151
def New() -> Similarity3DTransform[TParametersValueType]: ...
152
def SetScale(self, scale: TParametersValueType) -> None: ...
153
def GetScale(self) -> TParametersValueType: ...
154
def SetMatrix(self, matrix: MatrixType) -> None: ...
155
def GetMatrix(self) -> MatrixType: ...
156
def SetRotation(self, matrix: MatrixType) -> None: ...
157
def GetRotation(self) -> MatrixType: ...
158
```
159
160
### Affine Transformations
161
162
General linear transformations including rotation, translation, scaling, and shearing.
163
164
```python { .api }
165
class AffineTransform[TParametersValueType, NDimensions]:
166
"""General affine transformation supporting all linear operations."""
167
def New() -> AffineTransform[TParametersValueType, NDimensions]: ...
168
def GetMatrix(self) -> MatrixType: ...
169
def SetMatrix(self, matrix: MatrixType) -> None: ...
170
def GetOffset(self) -> OutputVectorType: ...
171
def SetOffset(self, offset: OutputVectorType) -> None: ...
172
def SetTranslation(self, translation: OutputVectorType) -> None: ...
173
def GetTranslation(self) -> OutputVectorType: ...
174
def SetCenter(self, center: InputPointType) -> None: ...
175
def GetCenter(self) -> InputPointType: ...
176
def Scale(self, factor: TParametersValueType, pre: bool = False) -> None: ...
177
def Scale(self, factor: OutputVectorType, pre: bool = False) -> None: ...
178
def Rotate2D(self, angle: TParametersValueType, pre: bool = False) -> None: ...
179
def Rotate3D(self, axis: OutputVectorType, angle: TParametersValueType, pre: bool = False) -> None: ...
180
def Shear(self, axis1: int, axis2: int, coeff: TParametersValueType, pre: bool = False) -> None: ...
181
def GetInverse(self) -> InverseTransformBasePointer: ...
182
183
class CenteredAffineTransform[TParametersValueType, NDimensions]:
184
"""Affine transformation with a specified center of rotation."""
185
def New() -> CenteredAffineTransform[TParametersValueType, NDimensions]: ...
186
def GetCenter(self) -> InputPointType: ...
187
def SetCenter(self, center: InputPointType) -> None: ...
188
def GetMatrix(self) -> MatrixType: ...
189
def SetMatrix(self, matrix: MatrixType) -> None: ...
190
def GetTranslation(self) -> OutputVectorType: ...
191
def SetTranslation(self, translation: OutputVectorType) -> None: ...
192
```
193
194
### Quaternion and Versor Transforms
195
196
Rotation representations using quaternions and unit quaternions (versors).
197
198
```python { .api }
199
class QuaternionRigidTransform[TParametersValueType]:
200
"""3D rigid transformation using quaternion representation."""
201
def New() -> QuaternionRigidTransform[TParametersValueType]: ...
202
def SetRotation(self, quaternion: VnlQuaternionType) -> None: ...
203
def GetRotation(self) -> VnlQuaternionType: ...
204
def SetTranslation(self, translation: OutputVectorType) -> None: ...
205
def GetTranslation(self) -> OutputVectorType: ...
206
207
class VersorTransform[TParametersValueType]:
208
"""3D rotation using versor (unit quaternion) representation."""
209
def New() -> VersorTransform[TParametersValueType]: ...
210
def SetVersor(self, versor: VersorType) -> None: ...
211
def GetVersor(self) -> VersorType: ...
212
def SetRotation(self, axis: VectorType, angle: TParametersValueType) -> None: ...
213
def SetCenter(self, center: InputPointType) -> None: ...
214
def GetCenter(self) -> InputPointType: ...
215
216
class VersorRigid3DTransform[TParametersValueType]:
217
"""3D rigid transformation using versor for rotation."""
218
def New() -> VersorRigid3DTransform[TParametersValueType]: ...
219
def SetVersor(self, versor: VersorType) -> None: ...
220
def GetVersor(self) -> VersorType: ...
221
def SetTranslation(self, translation: OutputVectorType) -> None: ...
222
def GetTranslation(self) -> OutputVectorType: ...
223
def SetRotation(self, axis: VectorType, angle: TParametersValueType) -> None: ...
224
def SetCenter(self, center: InputPointType) -> None: ...
225
def GetCenter(self) -> InputPointType: ...
226
```
227
228
### Non-linear Transforms
229
230
Deformable transformations for modeling complex spatial deformations.
231
232
```python { .api }
233
class BSplineTransform[TParametersValueType, NDimensions, VSplineOrder]:
234
"""B-spline deformable transformation."""
235
def New() -> BSplineTransform[TParametersValueType, NDimensions, VSplineOrder]: ...
236
def SetTransformDomainOrigin(self, origin: OriginType) -> None: ...
237
def GetTransformDomainOrigin(self) -> OriginType: ...
238
def SetTransformDomainPhysicalDimensions(self, dims: PhysicalDimensionsType) -> None: ...
239
def GetTransformDomainPhysicalDimensions(self) -> PhysicalDimensionsType: ...
240
def SetTransformDomainMeshSize(self, meshSize: MeshSizeType) -> None: ...
241
def GetTransformDomainMeshSize(self) -> MeshSizeType: ...
242
def SetTransformDomainDirection(self, direction: DirectionType) -> None: ...
243
def GetTransformDomainDirection(self) -> DirectionType: ...
244
def SetCoefficientImages(self, images: list) -> None: ...
245
def GetCoefficientImages(self) -> list: ...
246
def GetNumberOfWeights(self) -> int: ...
247
248
class ThinPlateSplineKernelTransform[TParametersValueType, NDimensions]:
249
"""Thin-plate spline transformation for landmark-based registration."""
250
def New() -> ThinPlateSplineKernelTransform[TParametersValueType, NDimensions]: ...
251
def GetSourceLandmarks(self) -> PointSetType: ...
252
def SetSourceLandmarks(self, landmarks: PointSetType) -> None: ...
253
def GetTargetLandmarks(self) -> PointSetType: ...
254
def SetTargetLandmarks(self, landmarks: PointSetType) -> None: ...
255
def ComputeWMatrix(self) -> None: ...
256
def GetStiffness(self) -> TParametersValueType: ...
257
def SetStiffness(self, stiffness: TParametersValueType) -> None: ...
258
259
class ElasticBodyReciprocalTransform[TParametersValueType, NDimensions]:
260
"""Elastic body reciprocal transformation."""
261
def New() -> ElasticBodyReciprocalTransform[TParametersValueType, NDimensions]: ...
262
def GetAlpha(self) -> TParametersValueType: ...
263
def SetAlpha(self, alpha: TParametersValueType) -> None: ...
264
265
class ElasticBodySplineKernelTransform[TParametersValueType, NDimensions]:
266
"""Elastic body spline kernel transformation."""
267
def New() -> ElasticBodySplineKernelTransform[TParametersValueType, NDimensions]: ...
268
def GetAlpha(self) -> TParametersValueType: ...
269
def SetAlpha(self, alpha: TParametersValueType) -> None: ...
270
```
271
272
### Transform Utilities
273
274
Helper classes for transform management and I/O operations.
275
276
```python { .api }
277
class TransformFactory[T]:
278
"""Factory for creating transform instances."""
279
def CreateTransform(transform_name: str) -> TransformBaseType: ...
280
def RegisterTransform(transform_name: str, create_function: callable) -> None: ...
281
282
class TransformFileReader:
283
"""Read transforms from files."""
284
def New() -> TransformFileReader: ...
285
def SetFileName(self, filename: str) -> None: ...
286
def GetFileName(self) -> str: ...
287
def Update(self) -> None: ...
288
def GetTransformList(self) -> TransformListType: ...
289
290
class TransformFileWriter:
291
"""Write transforms to files."""
292
def New() -> TransformFileWriter: ...
293
def SetFileName(self, filename: str) -> None: ...
294
def AddTransform(self, transform: TransformType) -> None: ...
295
def SetInput(self, transform: TransformType) -> None: ...
296
def Update(self) -> None: ...
297
def SetAppendMode(self, mode: bool) -> None: ...
298
```
299
300
## Usage Examples
301
302
### Basic Rigid Transformation
303
304
```python
305
import itk
306
import numpy as np
307
308
# Create a 3D rigid transform
309
transform = itk.Euler3DTransform[itk.D].New()
310
311
# Set rotation angles (in radians)
312
transform.SetRotation(np.pi/4, 0, np.pi/6) # 45° around X, 0° around Y, 30° around Z
313
314
# Set translation
315
translation = itk.Vector[itk.D, 3]([10.0, 20.0, 30.0])
316
transform.SetTranslation(translation)
317
318
# Set center of rotation
319
center = itk.Point[itk.D, 3]([50.0, 50.0, 25.0])
320
transform.SetCenter(center)
321
322
# Transform points
323
input_point = itk.Point[itk.D, 3]([100.0, 100.0, 50.0])
324
output_point = transform.TransformPoint(input_point)
325
print(f"Transformed point: {output_point}")
326
327
# Get transform parameters
328
parameters = transform.GetParameters()
329
print(f"Transform parameters: {parameters}")
330
```
331
332
### Affine Transformation with Scaling and Shearing
333
334
```python
335
import itk
336
import numpy as np
337
338
# Create 3D affine transform
339
transform = itk.AffineTransform[itk.D, 3].New()
340
341
# Set center
342
center = itk.Point[itk.D, 3]([0.0, 0.0, 0.0])
343
transform.SetCenter(center)
344
345
# Apply scaling
346
scale_factor = itk.Vector[itk.D, 3]([1.5, 1.2, 0.8])
347
transform.Scale(scale_factor)
348
349
# Apply rotation around Z-axis
350
axis = itk.Vector[itk.D, 3]([0.0, 0.0, 1.0])
351
transform.Rotate3D(axis, np.pi/4) # 45 degrees
352
353
# Apply shearing in XY plane
354
transform.Shear(0, 1, 0.2) # Shear X with respect to Y
355
356
# Apply translation
357
translation = itk.Vector[itk.D, 3]([10.0, 5.0, -2.0])
358
transform.SetTranslation(translation)
359
360
# Test the transformation
361
test_point = itk.Point[itk.D, 3]([1.0, 1.0, 1.0])
362
transformed_point = transform.TransformPoint(test_point)
363
print(f"Original: {test_point}")
364
print(f"Transformed: {transformed_point}")
365
366
# Get the transformation matrix
367
matrix = transform.GetMatrix()
368
offset = transform.GetOffset()
369
print(f"Matrix:\n{matrix}")
370
print(f"Offset: {offset}")
371
```
372
373
### B-spline Deformable Transform
374
375
```python
376
import itk
377
378
# Create B-spline transform
379
spline_order = 3
380
transform = itk.BSplineTransform[itk.D, 3, spline_order].New()
381
382
# Define transform domain
383
origin = itk.Point[itk.D, 3]([0.0, 0.0, 0.0])
384
physical_dimensions = itk.Vector[itk.D, 3]([100.0, 100.0, 50.0])
385
mesh_size = itk.Size[3]([10, 10, 5]) # Control point grid
386
387
transform.SetTransformDomainOrigin(origin)
388
transform.SetTransformDomainPhysicalDimensions(physical_dimensions)
389
transform.SetTransformDomainMeshSize(mesh_size)
390
391
# Initialize parameters (all zeros for identity)
392
num_parameters = transform.GetNumberOfParameters()
393
parameters = itk.Array[itk.D](num_parameters)
394
parameters.Fill(0.0)
395
396
# Add some deformation to a few control points
397
# Each control point has 3 components (x, y, z displacement)
398
parameters.SetElement(0, 2.0) # X displacement for first control point
399
parameters.SetElement(1, 1.0) # Y displacement for first control point
400
parameters.SetElement(2, -0.5) # Z displacement for first control point
401
402
transform.SetParameters(parameters)
403
404
# Test deformation
405
test_point = itk.Point[itk.D, 3]([25.0, 25.0, 12.5])
406
deformed_point = transform.TransformPoint(test_point)
407
print(f"Original point: {test_point}")
408
print(f"Deformed point: {deformed_point}")
409
```
410
411
### Transform Composition
412
413
```python
414
import itk
415
import numpy as np
416
417
# Create composite transform
418
composite = itk.CompositeTransform[itk.D, 3].New()
419
420
# Add translation
421
translation_transform = itk.TranslationTransform[itk.D, 3].New()
422
translation = itk.Vector[itk.D, 3]([10.0, 20.0, 30.0])
423
translation_transform.SetOffset(translation)
424
composite.AddTransform(translation_transform)
425
426
# Add rotation
427
rotation_transform = itk.Euler3DTransform[itk.D].New()
428
rotation_transform.SetRotation(np.pi/4, 0, np.pi/6)
429
center = itk.Point[itk.D, 3]([0.0, 0.0, 0.0])
430
rotation_transform.SetCenter(center)
431
composite.AddTransform(rotation_transform)
432
433
# Add scaling
434
scale_transform = itk.ScaleTransform[itk.D, 3].New()
435
scale = itk.Vector[itk.D, 3]([2.0, 1.5, 0.8])
436
scale_transform.SetScale(scale)
437
scale_transform.SetCenter(center)
438
composite.AddTransform(scale_transform)
439
440
# Test composite transformation
441
test_point = itk.Point[itk.D, 3]([1.0, 1.0, 1.0])
442
result_point = composite.TransformPoint(test_point)
443
print(f"Composite transform result: {result_point}")
444
445
# Get number of transforms in composition
446
num_transforms = composite.GetNumberOfTransforms()
447
print(f"Number of transforms: {num_transforms}")
448
```
449
450
### Transform I/O Operations
451
452
```python
453
import itk
454
455
# Create and configure a transform
456
transform = itk.AffineTransform[itk.D, 3].New()
457
transform.Scale(2.0)
458
transform.Rotate3D(itk.Vector[itk.D, 3]([0, 0, 1]), 0.5)
459
transform.SetTranslation(itk.Vector[itk.D, 3]([10, 20, 30]))
460
461
# Write transform to file
462
writer = itk.TransformFileWriter.New()
463
writer.SetFileName("my_transform.tfm")
464
writer.SetInput(transform)
465
writer.Update()
466
467
# Read transform from file
468
reader = itk.TransformFileReader.New()
469
reader.SetFileName("my_transform.tfm")
470
reader.Update()
471
472
# Get the loaded transform
473
transform_list = reader.GetTransformList()
474
loaded_transform = transform_list.front()
475
476
# Test that transforms are equivalent
477
test_point = itk.Point[itk.D, 3]([1, 2, 3])
478
original_result = transform.TransformPoint(test_point)
479
loaded_result = loaded_transform.TransformPoint(test_point)
480
print(f"Original result: {original_result}")
481
print(f"Loaded result: {loaded_result}")
482
```
483
484
### Working with Transform Parameters
485
486
```python
487
import itk
488
import numpy as np
489
490
# Create a versor rigid transform (good for optimization)
491
transform = itk.VersorRigid3DTransform[itk.D].New()
492
493
# Set center of rotation
494
center = itk.Point[itk.D, 3]([50.0, 50.0, 25.0])
495
transform.SetCenter(center)
496
497
# Set initial rotation using axis-angle
498
axis = itk.Vector[itk.D, 3]([0.0, 0.0, 1.0])
499
angle = np.pi / 6 # 30 degrees
500
transform.SetRotation(axis, angle)
501
502
# Set translation
503
translation = itk.Vector[itk.D, 3]([5.0, 10.0, -2.0])
504
transform.SetTranslation(translation)
505
506
# Get all parameters (useful for optimization)
507
parameters = transform.GetParameters()
508
print(f"Parameters: {parameters}")
509
print(f"Number of parameters: {transform.GetNumberOfParameters()}")
510
511
# Modify parameters directly
512
new_parameters = itk.Array[itk.D](parameters.Size())
513
for i in range(parameters.Size()):
514
new_parameters.SetElement(i, parameters.GetElement(i))
515
516
# Slightly modify rotation (first 3 parameters are versor components)
517
new_parameters.SetElement(0, parameters.GetElement(0) + 0.01)
518
new_parameters.SetElement(1, parameters.GetElement(1) + 0.01)
519
new_parameters.SetElement(2, parameters.GetElement(2) + 0.01)
520
521
# Set modified parameters
522
transform.SetParameters(new_parameters)
523
524
# Get transform properties
525
print(f"Center: {transform.GetCenter()}")
526
print(f"Translation: {transform.GetTranslation()}")
527
print(f"Versor: {transform.GetVersor()}")
528
```