# how to modify a metric in itk

**URL:** https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929
**Category:** Engineering
**Created:** [April 11, 2020, 3:07pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929 "2020-04-11T15:07:49Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 11, 2020, 3:07pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/1 "2020-04-11T15:07:50Z")

</div>

Hello everyone,  
I currently use itk in my project and I need to modify the metric of one of my registration between two 3d images. Is there an efficient way to implement my custom metric (in cpp)?  
Thank you in advance for your help 😏

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 11, 2020, 3:53pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/2 "2020-04-11T15:53:04Z")

</div>

The most efficient would be to take an existing metric and modify it. Inventing new image metrics is not expected to be done frequently.

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 11, 2020, 3:59pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/3 "2020-04-11T15:59:02Z")

</div>

Thank you for your answer. Can you show me which files to modify in order to make this as clean as possible ? I mean, I currently use Correlation metric from itkCorrelationImageToImageMetricv4 header and I do not see where the computation of the correlation metric in this .h file is…

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 11, 2020, 4:13pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/4 "2020-04-11T16:13:44Z")

</div>

There is a whole class hierarchy. Take a look at [this discussion](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353) for modification of mutual information metric.

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 11, 2020, 4:46pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/5 "2020-04-11T16:46:26Z")

</div>

Ok. For now, I am totally lost in the itk architecture, I might come back if I still have questions.

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 13, 2020, 8:07am UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/6 "2020-04-13T08:07:27Z")

</div>

Hi it is me again! I still don’t understand how to implement my own metric (even by modifying an existing one). I tried to modify the correlation metric and when I look into the files related to it (itkCorrelationImageToImageMetricv4\*), I do not see where the computation is performed… Can anyone detail me the procedure to do so ? Thank you

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 24, 2020, 10:03am UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/7 "2020-04-24T10:03:25Z")

</div>

Hello everyone,  
I still get stuck in this home-made metric problem. So I looked deeply into the source code and I tried to understand where exactly mean squares metric is computed.  
I read this .hxx file:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Registration/Metricsv4/include/itkMeanSquaresImageToImageMetricv4GetValueAndDerivativeThreader.hxx#L45-L53>

My question is: where is computed the normalization of the metric (the product of the sum with 1/N)?  
 ![metric_formula](https://discourse.itk.org/uploads/default/original/2X/6/6c05a8af16d0a1e65742a2fbf4eb4379c32d51ac.png)  
Thank you!

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 24, 2020, 1:05pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/8 "2020-04-24T13:05:17Z")

</div>

Probably in the method which invokes `ProcessPoint`. `ProcessPoint` deals only with a single `i`. The for loop you are pointing out is for dealing with vector components of a pixel (e.g. RGB).

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 24, 2020, 2:06pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/9 "2020-04-24T14:06:35Z")

</div>

ok, thank you sir. So, can you show me in which file every i of both images are read?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 24, 2020, 2:55pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/10 "2020-04-24T14:55:54Z")

</div>

Put a break-point in the debugger on line 45, then you will see in the call stack where `ProcessPoint` was invoked from.

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 24, 2020, 4:03pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/12 "2020-04-24T16:03:58Z")

</div>

![itkDomainThreader.hxx](https://discourse.itk.org/uploads/default/original/2X/a/adec5a4cc2650b939aefcabbcbfe1b380f2adf62.png) ![itkImageToImageMetricv4GetValueAndDerivativeThreader.hxx](https://discourse.itk.org/uploads/default/original/2X/f/f79a4a21368c6dcb2d8dcf67513fa82b9bb03902.png) ![itkImageToImageMetricv4GetValueAndDerivativeThreaderBase.hxx](https://discourse.itk.org/uploads/default/original/2X/2/21456db22f212422d6700c81389dad6191a6e7ba.png)

Breakpoint lead me to three differents files. I still do not see where the normalization is computed.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 24, 2020, 10:35pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/13 "2020-04-24T22:35:03Z")

</div>

Probably in _itkImageToImageMetricv4GetValueAndDerivativeThreader.hxx_ line 43 (method `FinalizeThread`).

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 27, 2020, 11:33am UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/15 "2020-04-27T11:33:38Z")

</div>

ok. I managed to understand the computation of the normalization. Again thank you for your help. I have another question regarding metric computation: does the computation of the metric have an influence on the computation of the optimizer or the final transform? I mean, is the metric computed at the end of the whole registration process or the metric is a stop flag which determines where the registration process have to stop?

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 27, 2020, 12:28pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/16 "2020-04-27T12:28:54Z")

</div>

my bad, i am reading the documentation and I saw that you made a detailed scheme of the registration pipeline.

 ![registration_pipeline](https://discourse.itk.org/uploads/default/original/2X/c/c828ee067fac6560e2310c15af875570522b7ae4.png)

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 27, 2020, 1:48pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/17 "2020-04-27T13:48:52Z")

</div>

registration = minimization of the metric by the optimizer

It is done for every iteration. Evaluating the metric is the expensive part of the registration.

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 29, 2020, 4:39pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/18 "2020-04-29T16:39:47Z")

</div>

> registration = minimization of the metric by the optimizer

okay. Can we just pass the optimizer the fitness value? I mean, can we deactivate the computation of the metric derivative?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 29, 2020, 5:52pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/19 "2020-04-29T17:52:30Z")

</div>

Some optimizers require derivatives. Which one are you using? Try [Powell](https://itk.org/Doxygen/html/classitk_1_1PowellOptimizer.html) or [Amoeba](https://itk.org/Doxygen/html/classitk_1_1AmoebaOptimizer.html).

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [April 30, 2020, 9:47am UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/20 "2020-04-30T09:47:11Z")

</div>

ok, I currently use RegularStepGradientDescent. I saw Powell but also the OnePlusOne optimizer. I will try also Amoeba too to see if one of these fits my purpose.

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [May 4, 2020, 2:04pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/21 "2020-05-04T14:04:29Z")

</div>

I was wondering if itk’s developers provided a registration example mixing Powell optimizer, mean square metric, and Euler3DTransform transform type ? I have searched this in vain after a couple of hours

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [May 4, 2020, 2:49pm UTC](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929/22 "2020-05-04T14:49:48Z")

</div>

Optimizers have similar interface, so adapting another example to your choice of optimizer, metric and transform type should not be hard.

[Next page](https://discourse.itk.org/t/how-to-modify-a-metric-in-itk/2929.md?page=2)
