# Elastix registration-Jupyter

**URL:** <https://discourse.slicer.org/t/elastix-registration-jupyter/19345>\
**Category:** Support\
**Tags:** registration\
**Created:** [August 24, 2021, 5:33pm UTC](https://discourse.slicer.org/t/elastix-registration-jupyter/19345 "2021-08-24T17:33:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gevindug](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gevindug/32/11885_2.png) [@gevindug](https://discourse.slicer.org/u/gevindug)\
**Post date:** [August 24, 2021, 5:33pm UTC](https://discourse.slicer.org/t/elastix-registration-jupyter/19345/1 "2021-08-24T17:33:49Z")

</div>

Operating system: Windows 10  
Slicer version: 4.11

Elastix registration is not working with the ‘generic rigid’ parameter file. can anyone help me to solve this issue? Actually, I wants to perform the wrist registration.

Current time: Tue Aug 24 22:54:02 2021.  
Reading the elastix parameters from file …

ERROR: when reading the parameter file:

itk::ExceptionObject (000000C375D1F210)  
Location: “unknown”  
File: D:\D\S\S-1-E-b\SlicerElastix-build\elastix\Common\ParameterFileParser\itkParameterFileParser.cxx  
Line: 116  
Description: itk::ERROR: itk::ERROR: ParameterFileParser(00000257A0F2A430): ERROR: the file E:\slicer\Slicer 4.11.20210226\NA-MIC\Extensions-29738\SlicerElastix\lib\Slicer-4.11\qt-scripted-modules\Resources\RegistrationParameters\g does not exist.

ERROR: Something went wrong during initialization of the configuration object.  
ERROR:  
The configuration object has not been initialized.  
Errors occurred!

---

<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:** [August 25, 2021, 5:49am UTC](https://discourse.slicer.org/t/elastix-registration-jupyter/19345/2 "2021-08-25T05:49:52Z")

</div>

It seems that the parameter file got corrupted. Reinstalling the extension should solve the problem.

---

<div class="post-metadata">

**Author:** ![gevindug](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gevindug/32/11885_2.png) [@gevindug](https://discourse.slicer.org/u/gevindug)\
**Post date:** [August 25, 2021, 10:05am UTC](https://discourse.slicer.org/t/elastix-registration-jupyter/19345/3 "2021-08-25T10:05:35Z")

</div>

I reinstall the extension. Still, the same error occurs.  
my code is as follows

```
RegistrationPresets_ParameterFilenames = 2
parameterFilenames = logic.getRegistrationPresets()[24][RegistrationPresets_ParameterFilenames]
outputVolume = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLScalarVolumeNode')
outtrans=slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTransformNode')
slicer.mrmlScene.AddNode(outputVolume)
outputVolume.CreateDefaultDisplayNodes()
outputVolume.SetName(Nodename)
outtrans.SetName(Nodename)
logic.registerVolumes(fixedVolumeNode= fixedVolumeNode,movingVolumeNode=movingVolumeNode, parameterFilenames = parameterFilenames , outputVolumeNode = outputVolume ,outputTransformNode =outtrans, fixedVolumeMaskNode = dialatedmasklabelmapVolumeNode)

```

When using the following combination it works. but not registering properly.

RegistrationPresets\_ParameterFilenames = 5  
parameterFilenames = logic.getRegistrationPresets()[1][RegistrationPresets\_ParameterFilenames]

---

<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:** [August 26, 2021, 5:50am UTC](https://discourse.slicer.org/t/elastix-registration-jupyter/19345/4 "2021-08-26T05:50:53Z")

</div>

OK, so it seems that the previous problem was solved.

The problems that you have now is due to modifying a built-in constant. `Elastix.RegistrationPresets_ParameterFilenames` constant is set to 5 and must be kept that way. This is the index of the parameter filenames in the preset list.

This code works perfectly for me:

```python
import Elastix
import SampleData

fixedVolume = SampleData.downloadSample('MRBrainTumor1')
movingVolume = SampleData.downloadSample('MRBrainTumor2')
outputVolume = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLScalarVolumeNode')
outputTransform = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTransformNode')
presetIndex = 0

elastixLogic = Elastix.ElastixLogic()
parameterFilenames = elastixLogic.getRegistrationPresets()[presetIndex][Elastix.RegistrationPresets_ParameterFilenames]
elastixLogic.registerVolumes(
    fixedVolume, movingVolume,
    outputVolumeNode = outputVolume,
    parameterFilenames = parameterFilenames,
    outputTransformNode = outputTransform,
    forceDisplacementFieldOutputTransform = True)

```

You can change the `presetIndex = 2`, it might give a tiny bit better result, but it also take much longer. In general, I’ve found that the two “generic” presets work very well, but the other presets are hit and miss. They are taken from the Elastix parameter database, so they should work for the specific use cases they developed for, but you may need to experiment with them a bit and tune parameters to work well for your registration tasks.

Also note that Elastix-\>ITK transformation file conversion is not robust ([BSpline output transform is not always correct · Issue #33 · lassoan/SlicerElastix · GitHub](https://github.com/lassoan/SlicerElastix/issues/33)), but the resampled output volume and grid transform output are always correct.

ANTs registration extension has been recently added to Slicer. It works very well, too (default presets should work without parameter tuning). You may give it a try.

---

<div class="post-metadata">

**Author:** ![gevindug](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gevindug/32/11885_2.png) [@gevindug](https://discourse.slicer.org/u/gevindug)\
**Post date:** [August 28, 2021, 8:17pm UTC](https://discourse.slicer.org/t/elastix-registration-jupyter/19345/5 "2021-08-28T20:17:19Z")

</div>

This works for me. Thank you for that. But in some cases the accuracy is questionable. Is there any other way to perform rigid registration.

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/a/4adfd393ce650f931e6db6792605288b205dbab0.png)

I got the results somewhat like this

---

<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:** [August 28, 2021, 10:14pm UTC](https://discourse.slicer.org/t/elastix-registration-jupyter/19345/6 "2021-08-28T22:14:23Z")

</div>

Does the output image (resampled moving image) aligns well with the fixed image?  
If yes, then the misalignment is probably due to the Elastix transform reading bug that I mentioned above (that you can solve by forcing grid transform output).

---

<div class="post-metadata">

**Author:** ![gevindug](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gevindug/32/11885_2.png) [@gevindug](https://discourse.slicer.org/u/gevindug)\
**Post date:** [August 29, 2021, 2:29pm UTC](https://discourse.slicer.org/t/elastix-registration-jupyter/19345/7 "2021-08-29T14:29:55Z")

</div>

It gives good outputs only the displacement of the moving image is small. When it comes to large deformation it is not working properly.  
I was thinking to perform registration again and again until it gives a good result. So Is there any way to register with an initial transform?
