# Porting https://itk.org/Wiki/ITK/Examples/Metrics/MeanSquaresImageToImageMetric from C++ to Python

**URL:** <https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670>\
**Category:** Beginner Questions\
**Created:** [March 15, 2019, 8:30am UTC](https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670 "2019-03-15T08:30:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicill](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/d26b3c/32.png) [@nicill](https://discourse.itk.org/u/nicill)\
**Post date:** [March 15, 2019, 8:30am UTC](https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670/1 "2019-03-15T08:30:24Z")

</div>

Hi all, this is my first question here, I hope I understood the rules.

I have been using itk (C++ version) for many years now, so I am quite familiar with it. Recently I have been exploring the possibility to use it also in Python. So far with not a lot of luck. Here is an specific question but any link to itk or simple itk resources only will be most appreciated (so far I have found quite a few tutorials but after reading those I feel I do not yet understand the way itk works in Python).

The example:

I want to port the code from this example:

[https://itk.org/Wiki/ITK/Examples/Metrics/MeanSquaresImageToImageMetric](https://itk.org/Wiki/ITK/Examples/Metrics/MeanSquaresImageToImageMetric)

to Python. As you will see, I am using both itk and simpleitk (I am not sure that is a good idea actually).

So far I have tried this:

```
import SimpleITK as sitk
import itk

def computeMetrics(nameFixed,nameMoving):

    pixelType = sitk.sitkFloat32

    fixedImage = sitk.ReadImage(nameFixed, pixelType)

    movingImage = sitk.ReadImage(nameMoving, pixelType)

    metric=itk.MeanSquaresImageToImageMetric()
    interpolator=itk.LinearInterpolateImageFunction()
    transform=itk.TranslationTransform()

    interpolator.SetInputImage( fixedImage )

    metric.SetFixedImage( fixedImage )
    metric.SetMovingImage( movingImage )
    metric.SetFixedImageRegion( fixedImage.GetLargestPossibleRegion() )
    metric.SetTransform( transform )
    metric.SetInterpolator( interpolator )

```

…

However, whenever I try to set the input images for the metric (both fixed and moving) I get the following error:

Traceback (most recent call last):  
File “./computeImageMetric.py”, line 67, in   
computeMetrics(sys.argv[1],sys.argv[2])  
File “./computeImageMetric.py”, line 26, in computeMetrics  
interpolator.SetInputImage( fixedImage )  
TypeError: in method ‘itkImageFunctionISS2DD\_SetInputImage’, argument 2 of type ‘itkImageSS2 const \*’  
I do not understand the error and am also having a hard time finding documentation (in python) for the classes involved. Any help will be most appreciated.

---

<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:** [March 15, 2019, 1:24pm UTC](https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670/2 "2019-03-15T13:24:48Z")

</div>

The short answer is: you cannot mix ITK and SimpleITK like that. `metric` expects and `ITKImage`, and SimpleITK image is similar but different class. I guess you will have to pick either ITK or SimpleITK.

@blowekamp @zivy Correct me if I am wrong.

---

<div class="post-metadata">

**Author:** ![nicill](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/d26b3c/32.png) [@nicill](https://discourse.itk.org/u/nicill)\
**Post date:** [March 15, 2019, 1:32pm UTC](https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670/3 "2019-03-15T13:32:34Z")

</div>

Thanks a lot for the answer.

I did worry a little bit about that and tried loading the images with itk (and not simple itk), for the following code:

def computeMetrics(nameFixed,nameMoving):

```
    pixelType = sitk.sitkFloat32

    #fixedImage = sitk.ReadImage(nameFixed, pixelType)
    fixedImage =itk.ImageFileReader.New(FileName=nameFixed).GetOutput()

    movingImage =itk.ImageFileReader.New(FileName=nameMoving).GetOutput()
    #movingImage = sitk.ReadImage(nameMoving, pixelType)

    metric=itk.MeanSquaresImageToImageMetric()
    interpolator=itk.LinearInterpolateImageFunction()
    transform=itk.TranslationTransform()

    interpolator.SetInputImage( fixedImage )

    metric.SetFixedImage( fixedImage )
    metric.SetMovingImage( movingImage )
    metric.SetFixedImageRegion( fixedImage.GetLargestPossibleRegion() )
    metric.SetTransform( transform )
    metric.SetInterpolator( interpolator )

```

The error seems to persist, though

Traceback (most recent call last):  
File “./computeImageMetric.py”, line 68, in   
computeMetrics(sys.argv[1],sys.argv[2])  
File “./computeImageMetric.py”, line 27, in computeMetrics  
interpolator.SetInputImage( fixedImage )  
TypeError: in method ‘itkImageFunctionISS2DD\_SetInputImage’, argument 2 of type ‘itkImageSS2 const \*’

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [March 15, 2019, 1:40pm UTC](https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670/4 "2019-03-15T13:40:56Z")

</div>

Hello,

You may find this SimpleITK Example useful: [https://simpleitk.readthedocs.io/en/master/Examples/ImageRegistrationMethodExhaustive/Documentation.html](https://simpleitk.readthedocs.io/en/master/Examples/ImageRegistrationMethodExhaustive/Documentation.html)

It utilizes an exhaustive optimizer to evaluate the metric on a grid pattern. Additionally, you may be interested in the [sitk::ImageRegistrationMethod::MetricEvaluate](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1ImageRegistrationMethod.html#a9b1f96dc287a747c1705f8185f5abe0a) method.

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [March 15, 2019, 8:34pm UTC](https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670/5 "2019-03-15T20:34:09Z")

</div>

Hello @nicill,

I wouldn’t recommend mixing and matching ITK and SimpleITK, but it should be easy to do via their numpy array import export facilities:

```auto
import SimpleITK as sitk
import itk

in_file_name = 'my_image.jpg'

def itk2sitk(img):
    return sitk.GetImageFromArray(itk.GetArrayViewFromImage(img), img.GetNumberOfComponentsPerPixel()>1)

def sitk2itk(img):
    return itk.GetImageFromArray(sitk.GetArrayViewFromImage(img), img.GetNumberOfComponentsPerPixel()>1)

# start with ITK, do some SimpleITK stuff, go back to ITK
img_itk = itk.imread(in_file_name)
img_sitk = sitk.Median(itk2sitk(img_itk))
itk.imwrite(sitk2itk(img_sitk), 'itk2sitk2itk.jpg')

# start with SimpleITK, do some ITK stuff, go back to SimpleITK
img_sitk = sitk.ReadImage(in_file_name)
median_filter = itk.MedianImageFilter.New(sitk2itk(img_sitk))
median_filter.Update()
img_itk = median_filter.GetOutput()
sitk.WriteImage(itk2sitk(img_itk), 'sitk2itk2sitk.jpg')

```

This code works for grayscale images, but I believe I uncovered a [bug in ITK Python](https://github.com/InsightSoftwareConsortium/ITK/issues/592) with respect to vector images. So, if nothing else, you have helped improve the toolkits.

---

<div class="post-metadata">

**Author:** ![jhlegarreta](https://discourse.itk.org/user_avatar/discourse.itk.org/jhlegarreta/32/476_2.png) [@jhlegarreta](https://discourse.itk.org/u/jhlegarreta)\
**Post date:** [March 17, 2019, 2:51am UTC](https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670/6 "2019-03-17T02:51:19Z")

</div>

As a side note, porting the example to Python and contributing to the ITK and/or SimpleITK Examples would be a great contribution:  
[https://itk.org/ITKExamples/Documentation/Contribute/index.html](https://itk.org/ITKExamples/Documentation/Contribute/index.html)  
[https://simpleitk.readthedocs.io/en/master/Examples/index.html](https://simpleitk.readthedocs.io/en/master/Examples/index.html)

Both toolkits are built in a large part thanks to the great contributions from the community. And this would definitely be such one 💯.

---

<div class="post-metadata">

**Author:** ![nicill](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/d26b3c/32.png) [@nicill](https://discourse.itk.org/u/nicill)\
**Post date:** [March 18, 2019, 4:46am UTC](https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670/7 "2019-03-18T04:46:17Z")

</div>

Thanks for your answer, it helped a lot.

I am not sure that is what you meant but your post made me think of computing the metric by setting up a registration scenario and then getting the metric (with the [sitk::ImageRegistrationMethod::MetricEvaluate](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1ImageRegistrationMethod.html#a9b1f96dc287a747c1705f8185f5abe0a) method that you mention by calling it before the registration is executed.

It looks like this:

```
def computeMetric(fixed,moving,mode):

# First, set up "phony" registration
R = sitk.ImageRegistrationMethod()
R.SetOptimizerAsGradientDescentLineSearch(learningRate=1.0,numberOfIterations=1,convergenceMinimumValue=1e-5,convergenceWindowSize=5)
R.SetInitialTransform(sitk.Transform(2,sitk.sitkIdentity)) # Transformation deliberately not using any initializer
R.SetInterpolator(sitk.sitkLinear)

#second, choose metric
if(mode==0): R.SetMetricAsJointHistogramMutualInformation()
elif(mode==1): R.SetMetricAsMattesMutualInformation(numberOfHistogramBins = 50)
elif(mode==2): R.SetMetricAsMeanSquares()
elif(mode==3): R.SetMetricAsCorrelation()
else:
    print("Error in computeMetric, unrecognized metric")
    sys.exit ( 1 )

#third, get the metric value
print(R.MetricEvaluate(fixed, moving))

```

I have been testing a few things (metric with same image, when the image order changes, after some registration methods) and I am getting some weird values from the mutual information metrics (modes 0 and 1) but least squares and correlation seem to work fine.

---

<div class="post-metadata">

**Author:** ![nicill](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/d26b3c/32.png) [@nicill](https://discourse.itk.org/u/nicill)\
**Post date:** [March 18, 2019, 5:03am UTC](https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670/8 "2019-03-18T05:03:58Z")

</div>

Thanks for the tip about working with itk and simpleItk, at this moment I can do all I need to do with only simpleItk but I will definetely keep it mind.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [March 18, 2019, 7:03pm UTC](https://discourse.itk.org/t/porting-https-itk-org-wiki-itk-examples-metrics-meansquaresimagetoimagemetric-from-c-to-python/1670/9 "2019-03-18T19:03:24Z")

</div>

Very nice!

That is how the “MetricEvaluate” method is designed to be used. It is not so much a “phony” registration as to evaluate the metric the interpolator and transforms are needed. Only the optimizer is not used since the registration method is just being evaluated at one set of transform parameters.
