# is there a way to set custom metric in itk registration in python?

**URL:** https://discourse.itk.org/t/is-there-a-way-to-set-custom-metric-in-itk-registration-in-python/705
**Category:** Engineering
**Tags:** registration, python
**Created:** [February 23, 2018, 5:09pm UTC](https://discourse.itk.org/t/is-there-a-way-to-set-custom-metric-in-itk-registration-in-python/705 "2018-02-23T17:09:29Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![sedghi](https://discourse.itk.org/user_avatar/discourse.itk.org/sedghi/32/317_2.png) [@sedghi](https://discourse.itk.org/u/sedghi)
#### Post date: [February 23, 2018, 5:09pm UTC](https://discourse.itk.org/t/is-there-a-way-to-set-custom-metric-in-itk-registration-in-python/705/1 "2018-02-23T17:09:30Z")

</div>

I have a similarity metric that I’d like to use in ImageRegistrationMethod in itk python, is there any way to do so?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [February 23, 2018, 7:33pm UTC](https://discourse.itk.org/t/is-there-a-way-to-set-custom-metric-in-itk-registration-in-python/705/2 "2018-02-23T19:33:17Z")

</div>

Hi @sedghi,

Yes. If it is an existing method in ITK, it can be used from Python with the ImageRegistrationMethod.

If it is a new method you created your self, you can quickly wrap it in Python, and create an installable Python Package. More information:

- The [ITK Module Template](https://github.com/InsightSoftwareConsortium/ITKModuleTemplate). This will get you going with a skeleton module in two commands.
- The [ITK Software Guide’s chapter on How to Create A Module](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch9.html#x48-1480009). This includes instruction on how to add wrapping.

Please let us know if you have any questions.

---

<div class="post-metadata">

### Author: ![sedghi](https://discourse.itk.org/user_avatar/discourse.itk.org/sedghi/32/317_2.png) [@sedghi](https://discourse.itk.org/u/sedghi)
#### Post date: [February 23, 2018, 7:39pm UTC](https://discourse.itk.org/t/is-there-a-way-to-set-custom-metric-in-itk-registration-in-python/705/3 "2018-02-23T19:39:33Z")

</div>

Thanks @matt  
actually my custom made metric is implemented in python. Do I still need to create an installable python package?

---

<div class="post-metadata">

### Author: ![sedghi](https://discourse.itk.org/user_avatar/discourse.itk.org/sedghi/32/317_2.png) [@sedghi](https://discourse.itk.org/u/sedghi)
#### Post date: [April 20, 2018, 2:34pm UTC](https://discourse.itk.org/t/is-there-a-way-to-set-custom-metric-in-itk-registration-in-python/705/4 "2018-04-20T14:34:27Z")

</div>

@matt.mccormick  
I was wondering if there is any update on this issue?  
Is there any python callable (function) for the optimizer interface right now?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [April 20, 2018, 6:14pm UTC](https://discourse.itk.org/t/is-there-a-way-to-set-custom-metric-in-itk-registration-in-python/705/5 "2018-04-20T18:14:15Z")

</div>

@sedghi There is not a python callable (function) interface to supply your own method right now, but that is a great idea. @pieper has been thinking about it, too.

I don’t think it is that hard to implement. What do you think about an `itk.PythonImageRegistrationMetricv4` class? This class would be similar to [itk.ImageToImageMetricv4](https://itk.org/Doxygen/html/classitk_1_1ImageToImageMetricv4.html), but it would have additional methods,

- `GetValuePythonCallback`
- `GetDerivativePythonCallback`
- `GetValueAndDerivativePythonCallback`

These methods would be passed Python functions that accept the `ImageToImageMetricv4` as an argument and would need to return required value and derivative values.

---

<div class="post-metadata">

### Author: ![pieper](https://discourse.itk.org/user_avatar/discourse.itk.org/pieper/32/22_2.png) [@pieper](https://discourse.itk.org/u/pieper)
#### Post date: [April 21, 2018, 3:26pm UTC](https://discourse.itk.org/t/is-there-a-way-to-set-custom-metric-in-itk-registration-in-python/705/6 "2018-04-21T15:26:59Z")

</div>

Sounds great @matt.mccormick! Yes, @sedghi and I worked together looking into this so hopefully he can let us know if these methods would be enough to cover the use case.

---

<div class="post-metadata">

### Author: ![kayarre](https://discourse.itk.org/user_avatar/discourse.itk.org/kayarre/32/669_2.png) [@kayarre](https://discourse.itk.org/u/kayarre)
#### Post date: [November 12, 2019, 2:18am UTC](https://discourse.itk.org/t/is-there-a-way-to-set-custom-metric-in-itk-registration-in-python/705/7 "2019-11-12T02:18:40Z")

</div>

Any progress on this front?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [November 12, 2019, 4:40pm UTC](https://discourse.itk.org/t/is-there-a-way-to-set-custom-metric-in-itk-registration-in-python/705/8 "2019-11-12T16:40:56Z")

</div>

I created this issue for tracking:

> <https://github.com/InsightSoftwareConsortium/ITK/issues/1411>

Note that we have similar functionality to define an image filter in Python:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Wrapping/Generators/Python/PyUtils/itkPyImageFilter.h>
