# How to register two models using Python?

**URL:** <https://discourse.slicer.org/t/how-to-register-two-models-using-python/7233>\
**Category:** Development\
**Tags:** registration\
**Created:** [June 19, 2019, 10:41am UTC](https://discourse.slicer.org/t/how-to-register-two-models-using-python/7233 "2019-06-19T10:41:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![timeanddoctor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/timeanddoctor/32/5839_2.png) [@timeanddoctor](https://discourse.slicer.org/u/timeanddoctor)\
**Post date:** [June 19, 2019, 10:41am UTC](https://discourse.slicer.org/t/how-to-register-two-models-using-python/7233/1 "2019-06-19T10:41:57Z")

</div>

I went to calculater a transform between two models.  
Then I searched and found such module :[https://github.com/SlicerIGT/SlicerIGT/blob/master/ModelRegistration/ModelRegistration.py](https://github.com/SlicerIGT/SlicerIGT/blob/master/ModelRegistration/ModelRegistration.py)

but when I run this code in python cli, I get an error just like the post title.

```auto
def MR(inputSourceModel, inputTargetModel, outputSourceToTargetTransform, transformType=0, numIterations=100 ):    
    
  delayDisplay('Running iterative closest point registration')
  
  icpTransform = vtk.vtkIterativeClosestPointTransform()
  icpTransform.SetSource(inputSourceModel.GetPolyData() )
  icpTransform.SetTarget(inputTargetModel.GetPolyData() )
  icpTransform.GetLandmarkTransform().SetModeToRigidBody()
  if transformType == 1:
    icpTransform.GetLandmarkTransform().SetModeToSimilarity()
  if transformType == 2:
    icpTransform.GetLandmarkTransform().SetModeToAffine()
  icpTransform.SetMaximumNumberOfIterations( numIterations )
  icpTransform.Modified()
  icpTransform.Update()
  
  outputSourceToTargetTransform.SetAndObserveMatrixTransformToParent(vtk.vtkMatrix4x4())
          
  if slicer.app.majorVersion >= 5 or (slicer.app.majorVersion >= 4 and slicer.app.minorVersion >= 11):
    outputSourceToTargetTransform.AddNodeReferenceID(slicer.vtkMRMLTransformNode.GetMovingNodeReferenceRole(), inputSourceModel.GetID())
    outputSourceToTargetTransform.AddNodeReferenceID(slicer.vtkMRMLTransformNode.GetFixedNodeReferenceRole(), inputTargetModel.GetID())
  
  return True

inputSourceModel = getNode("a")
inputTargetModel = getNode("b")
 
MR(inputSourceModel, inputTargetModel, "aTob", transformType=0, numIterations=100)

```

If I went to calculate a transform between two models, and which code is right?

Thanks.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 19, 2019, 3:51pm UTC](https://discourse.slicer.org/t/how-to-register-two-models-using-python/7233/2 "2019-06-19T15:51:57Z")

</div>

A transform node was expected and you passed a string instead. You don’t need to copy-paste code from modules but you can use module logic from another module or from the Python console. For example, this should do everything you need:

```python
# Get input and output nodes
sourceModel = getNode('Segment_1')
targetModel = getNode('Segment_2')
sourceToTargetTransform = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLinearTransformNode")

# Register
import ModelRegistration
mrLogic = ModelRegistration.ModelRegistrationLogic()
mrLogic.run(sourceModel, targetModel, sourceToTargetTransform)

# Show results
sourceModel.SetAndObserveTransformNodeID(sourceToTargetTransform.GetID())
sourceModel.GetDisplayNode().SetOpacity(0.5)
targetModel.GetDisplayNode().SetOpacity(0.5)

```

If you need non-linear (warping) registration of nodes then you can use SegmentRegistration extension.
