# How to get metric value for a given set of transform parameters in ITK V4?

**URL:** <https://discourse.itk.org/t/how-to-get-metric-value-for-a-given-set-of-transform-parameters-in-itk-v4/1906>\
**Category:** Algorithms\
**Created:** [May 22, 2019, 6:27pm UTC](https://discourse.itk.org/t/how-to-get-metric-value-for-a-given-set-of-transform-parameters-in-itk-v4/1906 "2019-05-22T18:27:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eewujian](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/839c29/32.png) [@eewujian](https://discourse.itk.org/u/eewujian)\
**Post date:** [May 22, 2019, 6:27pm UTC](https://discourse.itk.org/t/how-to-get-metric-value-for-a-given-set-of-transform-parameters-in-itk-v4/1906/1 "2019-05-22T18:27:50Z")

</div>

I’m trying to plot a profile of cost function (or metric) values for a 3D image registration study. This can be done easily in ITK V3 with the itk::ImageToImageMetric::GetValue(const ParametersType &parameters) function. So I can change the parameters in the argument to probe the cost function in the parametric space. However in ITK V4, only the function itk::ImageToImageMetricv4::GetValue() exists with no argument taken. I’m wondering how to get it done in the new registration frame. Any hints or examples? I’ll appreciate it very much if anyone could 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:** [May 22, 2019, 8:27pm UTC](https://discourse.itk.org/t/how-to-get-metric-value-for-a-given-set-of-transform-parameters-in-itk-v4/1906/2 "2019-05-22T20:27:42Z")

</div>

`ImageToImageMetricv4` inherits [`SetParameters()`](https://itk.org/Doxygen/html/classitk_1_1ObjectToObjectMetric.html#a716c5a2e8c96233b194bd02fe08dd09e). You should call that prior to calling `GetValue()`.

---

<div class="post-metadata">

**Author:** ![Lermontov](https://discourse.itk.org/letter_avatar_proxy/v4/letter/l/bbe5ce/32.png) [@Lermontov](https://discourse.itk.org/u/Lermontov)\
**Post date:** [July 5, 2022, 3:31pm UTC](https://discourse.itk.org/t/how-to-get-metric-value-for-a-given-set-of-transform-parameters-in-itk-v4/1906/3 "2022-07-05T15:31:38Z")

</div>

Thank you very much, I found the answer by myself before reading your message.

Though, there is a problem in my code. My loop stops when the following error is raise :

RuntimeError: /work/ITK-source/ITK/Modules/Registration/Common/include/itkMeanSquaresImageToImageMetric.hxx:126:  
ITK ERROR: MeanSquaresImageToImageMetric(0x5631eee2a330): Too many samples map outside moving image buffer: 0 / 2215440

How can I keep running for other values after one of the tuple (x,y) failed?

---

<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:** [July 6, 2022, 7:12am UTC](https://discourse.itk.org/t/how-to-get-metric-value-for-a-given-set-of-transform-parameters-in-itk-v4/1906/4 "2022-07-06T07:12:08Z")

</div>

Use try/catch syntax to avoid that. Pseudocode:

```nohighlight
for x ...
  for y ...
    try
    {
    metricXY = evaluate(fixedImage, movingImage, x, y);
    metrics[y][x] = metricXY;
    }
    catch(const itk::Exception& exc)
    {
    std::cout<<... // or just ignore it
    }

```
