# Calculating mutual information of two images

**URL:** <https://discourse.itk.org/t/calculating-mutual-information-of-two-images/4931>\
**Category:** Beginner Questions\
**Tags:** simpleitk, python\
**Created:** [April 22, 2022, 2:27pm UTC](https://discourse.itk.org/t/calculating-mutual-information-of-two-images/4931 "2022-04-22T14:27:31Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Flo](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/b38774/32.png) [@Flo](https://discourse.itk.org/u/Flo)\
**Post date:** [April 22, 2022, 2:27pm UTC](https://discourse.itk.org/t/calculating-mutual-information-of-two-images/4931/1 "2022-04-22T14:27:32Z")

</div>

I have corresponding MRI and CT image volumes. I have registered them using the mattes mutual information metric. The results seem fine but I would like to be able to calculate the metric of two 3D images that are already registered. As of now the only way to calculate the metric I know of is to call the MetricEvaluate method of the ImageRegistrationMethod class. I have written a function to calculate the mutual information given the two image volumes (before resampling) and the transformation found during registration:

```auto
def mutual_info(transform, mr, ct):
    registration_method = sitk.ImageRegistrationMethod()
    registration_method.SetMetricAsMattesMutualInformation(numberOfHistogramBins=50)
    registration_method.SetMetricSamplingStrategy(registration_method.RANDOM)
    registration_method.SetMetricSamplingPercentage(0.01)
    registration_method.SetInterpolator(sitk.sitkLinear)
    registration_method.SetInitialTransform(transform)
    return registration_method.MetricEvaluate(mr, ct)

```

But there are two problems:

1. The resulting value is different from the one achieved at the end of the registration process. Why is this the case?
2. I would like to calculate the mutual information of two images that were already resampled according to the transformation found during registration. And I would also like to calculate the mutual information of only the upper part of the images. Is there a way to simply calculate the mutual information of two images without a transformation?

---

_[View the full topic](https://discourse.itk.org/t/calculating-mutual-information-of-two-images/4931)._
