# Compute a MSE metric for a fixed and moving image (manual registration)

**URL:** <https://discourse.itk.org/t/compute-a-mse-metric-for-a-fixed-and-moving-image-manual-registration/5161>\
**Category:** Beginner Questions\
**Tags:** registration, python\
**Created:** [July 5, 2022, 2:28pm UTC](https://discourse.itk.org/t/compute-a-mse-metric-for-a-fixed-and-moving-image-manual-registration/5161 "2022-07-05T14:28:20Z")\
**Posts on this page:** 4\
**Page:** 1

<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, 2:28pm UTC](https://discourse.itk.org/t/compute-a-mse-metric-for-a-fixed-and-moving-image-manual-registration/5161/1 "2022-07-05T14:28:20Z")

</div>

Hi,

I’m new to ITK and image registration in general. And I’m facing a problem I barely know how to solve.

Before applying 2D/2D image registration on two images, I want to know if my registration is more likely to converge to the solution. The idea here is to plot a 3d map of the metrics when modifying two parameters (in my case the (x0,y0) origin of the moving image).

I want to build a double four-loop for (x0,y0) and compute the MSE between the superposition area of the two images, I suppose my fixed image origin is (0,0). Is there an easy way to do that with elastix?

I know the spacing of my two images.

This way I could estimate if the good solution of the desired registration is a global minimum and not some local minimum.

I apologize for my broken english, it’s not my native language.

Best Regards

---

<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:07am UTC](https://discourse.itk.org/t/compute-a-mse-metric-for-a-fixed-and-moving-image-manual-registration/5161/2 "2022-07-06T07:07:31Z")

</div>

I don’t know whether that is easy using elastix, but it should not be hard using ITK directly. In your double for loop update the origin of the moving image, then manually evaluate the metric. See some related discussions, such as [this](https://discourse.itk.org/t/similarity-measure/4680) and [this](https://discourse.itk.org/t/registration-metric-for-rgb-image/2439).

---

<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 7, 2022, 1:32pm UTC](https://discourse.itk.org/t/compute-a-mse-metric-for-a-fixed-and-moving-image-manual-registration/5161/3 "2022-07-07T13:32:46Z")

</div>

I use found my way using Itk library on python, though, I have a problem with the setting of the fixed image mask.

> fixed\_image = itk.imread(optical)  
> moving\_image = itk.imread(mri)  
> fixed\_mask = itk.imread(optical\_mask)
> 
> metric = itk.MeanSquaresImageToImageMetric[type(fixed\_image), type(moving\_image)].New()  
> transform = itk.TranslationTransform[itk.D, fixed\_image.GetImageDimension()].New()  
> interpolator = itk.LinearInterpolateImageFunction[type(fixed\_image), itk.D].New()
> 
> metric.SetFixedImage(fixed\_image)  
> metric.SetFixedImageMask(fixed\_mask)  
> metric.SetMovingImage(moving\_image)  
> metric.SetFixedImageRegion(fixed\_image.GetLargestPossibleRegion())  
> metric.SetTransform(transform)  
> metric.SetInterpolator(interpolator)  
> metric.Initialize()
> 
> params = itk.OptimizerParametersitk.D  
> params.SetSize(2)

When I run the code, it returns me the following answer :

> TypeError: in method ‘itkImageToImageMetricIF2IF2\_SetFixedImageMask’, argument 2 of type ‘itkSpatialObject2 \*’  
> Additional information:  
> Wrong number or type of arguments for overloaded function ‘itkImageToImageMetricIF2IF2\_SetFixedImageMask’.  
> Possible C/C++ prototypes are:  
> itkImageToImageMetricIF2IF2::SetFixedImageMask(itkSpatialObject2 \*)  
> itkImageToImageMetricIF2IF2::SetFixedImageMask(itkSpatialObject2 const \*)

Knowing that my mask type is

> \<itk.itkImagePython.itkImageF2; proxy of \<Swig Object of type ‘itkImageF2 \*’ at 0x7f073d400db0\> \>

Is there a way with itk python library to convert the type of the mask? I’m totally beginner with C++.

---

<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 8, 2022, 7:20am UTC](https://discourse.itk.org/t/compute-a-mse-metric-for-a-fixed-and-moving-image-manual-registration/5161/4 "2022-07-08T07:20:10Z")

</div>

You probably need to involve [ImageMaskSpatialObject](https://itk.org/Doxygen/html/classitk_1_1ImageMaskSpatialObject.html). Set image to it, and it to metric.
