ICP (Iterative Closest Point)
Pure Python implementation of Iterative Closest Point registration.
Class Reference
- class monai_physio.RegisterModelsICP(fixed_model, log_level=20)[source]
Bases:
RegisterModelsBaseRegister anatomical models using Iterative Closest Point (ICP) algorithm.
This class provides ICP-based alignment of 3D surface models with support for both rigid and affine transformation modes. The registration pipeline uses centroid alignment for initialization followed by VTK’s ICP algorithm.
- Registration Pipelines:
Rigid: Centroid alignment → Rigid ICP
Similarity: Centroid alignment → bounding-box scaling → Similarity ICP
Affine: Centroid alignment → bounding-box scaling → Similarity ICP → Affine ICP
The bounding-box scaling estimates the size ratio between the models from their bounding-box diagonals before ICP starts. It is skipped for ‘Rigid’, whose transform has no scale degree of freedom.
- Transform Convention:
These are POINT transforms, applied with TransformPoint (e.g. via ProcessTransforms.transform_pvcontour); see docs/developer/transform_conventions:
moving_to_fixed_transform: maps moving points -> fixed points; use it to warp the moving model/landmarks onto the fixed model.
fixed_to_moving_transform: maps fixed points -> moving points.
- moving_model
Surface model to be aligned
- Type:
pv.PolyData
- fixed_model
Target surface model
- Type:
pv.PolyData
- transform_tools
Transform utility instance
- Type:
- moving_to_fixed_transform
Optimized moving-to-fixed transform
- Type:
itk.AffineTransform
- fixed_to_moving_transform
Optimized fixed-to-moving transform
- Type:
itk.AffineTransform
- registered_model
Aligned moving model
- Type:
pv.PolyData
Example
>>> # Initialize with model >>> registrar = RegisterModelsICP(fixed_model=patient_surface) >>> >>> # Run rigid registration >>> registrar.set_transform_type('Rigid') >>> result = registrar.register(model_surface) >>> >>> # Get aligned model and transforms >>> aligned_model = result['registered_model'] >>> moving_to_fixed_transform = result['moving_to_fixed_transform']
- __init__(fixed_model, log_level=20)[source]
Initialize ICP-based model registration.
- Parameters:
Note
The moving model, passed to
register(), is typically extracted from a VTU model using model.extract_surface(algorithm=”dataset_surface”).
- set_transform_type(transform_type)[source]
Choose the registration transform: ‘Rigid’, ‘Similarity’ or ‘Affine’.
- Parameters:
transform_type (
str) – Transform type. Default: ‘Affine’- Raises:
ValueError – If transform_type is not ‘Rigid’, ‘Similarity’ or ‘Affine’
- Return type:
- set_max_iterations(max_iterations)[source]
Set the maximum number of ICP iterations per stage.
- Parameters:
max_iterations (
int) – Maximum iterations per stage. Default: 2000- Raises:
ValueError – If max_iterations is less than 1.
- Return type:
- registration_method(moving_model)[source]
Perform ICP alignment of moving model to fixed model.
The
register()hook; it uses the transform type and iteration limit set byset_transform_type()andset_max_iterations().- Rigid transform type (rotation + translation):
Centroid alignment: Translate moving model to align mass centers
Rigid ICP: Refine with rigid-body transformation
- Similarity transform type (rotation + translation + one uniform scale):
Centroid alignment: Translate moving model to align mass centers
- Bounding-box scaling: Scale isotropically about the fixed centroid
so the two bounding-box diagonals match, which keeps ICP’s closest-point search from locking onto a size mismatch
Similarity ICP: Refine rotation, translation and uniform scale
- Affine transform type (adds anisotropic scale and shear):
Centroid alignment: Translate moving model to align mass centers
Bounding-box scaling: As above
Similarity ICP: Refine rotation, translation and uniform scale
Affine ICP: Further refine with affine transformation
‘Rigid’ skips the bounding-box scaling so its result stays a pure rigid-body transform; use ‘Similarity’ when the models differ in size but the shape should not be distorted.
- Parameters:
moving_model (
DataSet) – PyVista surface model to be aligned to fixed model- Returns:
‘registered_model’: Aligned moving model (PyVista PolyData)
- ’moving_to_fixed_transform’: Moving-to-fixed transform
(ITK AffineTransform)
- ’fixed_to_moving_transform’: Fixed-to-moving transform
(ITK AffineTransform)
- Return type:
Dictionary containing
- classmethod get_log_classes()
Get the list of classes currently showing logs.
- Return type:
- Returns:
List of class names that are allowed to show logs. Empty list if filter is disabled (all classes shown).
Example
>>> classes = MONAIPhysioBase.get_log_classes() >>> print(classes) ['RegisterModelsPCA', 'WorkflowFitStatisticalModelToPatient']
- log_critical(message, *args)
Log a critical message with optional %-style formatting.
- Parameters:
- Return type:
Example
>>> self.log_critical('System failure at %s', timestamp) >>> self.log_critical('Critical error: %(msg)s', {'msg': 'Out of memory'})
- log_debug(message, *args)
Log a debug message with optional %-style formatting.
- Parameters:
- Return type:
Example
>>> self.log_debug('Processing %s with %d items', filename, count) >>> self.log_debug('Value is %(value)d', {'value': 42})
- log_error(message, *args)
Log an error message with optional %-style formatting.
- Parameters:
- Return type:
Example
>>> self.log_error('Failed to load %s: %s', filename, error_msg) >>> self.log_error('Error code: %(code)d', {'code': 404})
- log_info(message, *args)
Log an info message with optional %-style formatting.
- Parameters:
- Return type:
Example
>>> self.log_info('Loading file: %s', filepath) >>> self.log_info('Iteration %(iter)d of %(total)d', {'iter': 5, 'total': 10})
- log_progress(current, total, prefix='Progress', *, log=True)
Log progress information and report it to the progress callback.
current / totalis also reported, within the current range, to the callback set byset_progress_callback().- Parameters:
- Return type:
Example
>>> for i in range(100): ... self.log_progress(i + 1, 100) >>> self.log_progress(5, 10, prefix='Processing')
Note
For custom formatted progress messages, use log_info() directly: >>> self.log_info(‘Loading %s: %d/%d’, filename, current, total)
- log_section(title, *args, width=70, char='=')
Log a formatted section header with optional %-style formatting.
Useful for visually separating major sections of output.
- Parameters:
- Return type:
Example
>>> self.log_section('Stage 1: Initialization') >>> self.log_section('Processing file: %s', filename) >>> self.log_section('Stage %(num)d: %(name)s', {'num': 2, 'name': 'Analysis'}) # Outputs: # ====================================================================== # Stage 2: Analysis # ======================================================================
- log_warning(message, *args)
Log a warning message with optional %-style formatting.
- Parameters:
- Return type:
Example
>>> self.log_warning('Memory usage at %d%%', usage_percent) >>> self.log_warning('Parameter %(name)s out of range', {'name': 'threshold'})
- register(moving_model)
Register
moving_modelto the fixed model.Calls
registration_method()and keeps its standard results on the instance.- Parameters:
moving_model (
DataSet) – Model to align to the fixed model.- Returns:
registered_model,moving_to_fixed_transform,fixed_to_moving_transformand any keys the subclass adds.- Return type:
The hook’s dictionary
- set_fixed_model(fixed_model)
Replace the target model and forget the previous results.
- Parameters:
fixed_model (
DataSet) – New target model.- Return type:
- classmethod set_log_all_classes()
Enable logging output from all MONAI Physio classes.
Disables the class filter so all classes show their logs.
Example
>>> MONAIPhysioBase.set_log_all_classes() >>> # Now all classes will show their logs
- Return type:
- classmethod set_log_classes(class_names)
Set which classes should show their logging output.
Only log messages from the specified classes will be displayed. All other classes will have their logs hidden.
- Parameters:
class_names (
list[str]) – List of class names to show logs from. Example: [“RegisterModelsPCA”, “WorkflowFitStatisticalModelToPatient”]- Return type:
Example
>>> MONAIPhysioBase.set_log_classes(['RegisterModelsPCA']) >>> # Now only RegisterModelsPCA logs will be shown
- classmethod set_log_level(log_level)
Set a floor on the logging level of every MONAI Physio object.
Each object logs at its own
log_level; messages below this floor are dropped whatever that level is. Lowering the floor does not make an object log below its ownlog_level.- Parameters:
log_level (
int|str) – Logging level. Can be an integer (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL) or a string (‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’).- Return type:
Example
>>> import logging >>> MONAIPhysioBase.set_log_level(logging.DEBUG) >>> # or >>> MONAIPhysioBase.set_log_level('DEBUG')
- classmethod set_progress_callback(progress_callback)
Set the callback that receives progress from all MONAI Physio classes.
Like the log level, this is shared: set it once and every workflow and registrar reports to it, including those a workflow runs internally.
Each long-running method reports a fraction in
[0, 1]and a short message. A workflow divides that range among its steps once, as if every optional step ran; a step that is disabled is skipped, so the fraction jumps over its share. The fraction never decreases during a run and reaches 1.0 when the run completes. Progress from runs in concurrent threads is not kept apart.- Parameters:
progress_callback (
Optional[Callable[[float,str],None]]) – Receives each fraction and message, orNoneto stop reporting.- Return type:
Example
>>> def show(fraction: float, message: str) -> None: ... print(f"{fraction:6.1%} {message}") >>> MONAIPhysioBase.set_progress_callback(show) >>> result = workflow.process()
Navigation
Model Registration Modules | ICP with ITK Backend | Distance Map Registration