# Changes In ITK Mattes Mutual Information Metric

**URL:** <https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353>\
**Category:** Algorithms\
**Tags:** implementation, c, mutual-information\
**Created:** [October 26, 2019, 9:47pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353 "2019-10-26T21:47:19Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 20, 2019, 11:19pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/21 "2019-11-20T23:19:59Z")

</div>

You mean at git hub?

---

<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:** [November 21, 2019, 1:49pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/22 "2019-11-21T13:49:36Z")

</div>

You could look at the files’ blame on your local computer. But yes, GitHub has an integrated blame tool as well.

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 21, 2019, 8:45am UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/23 "2019-11-21T08:45:42Z")

</div>

I’m not familiar with that tool. I looked it over, but did not get the point.  
How can I use blame to help me?  
Thanks again!

---

<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:** [November 21, 2019, 3:26pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/24 "2019-11-21T15:26:19Z")

</div>

Looking at the [blame](https://github.com/InsightSoftwareConsortium/ITK/blame/81be43c4fefd91e83c732835dae0942eb243cd44/Modules/Registration/Metricsv4/include/itkJointHistogramMutualInformationGetValueAndDerivativeThreader.hxx#L99) tells you that @stnava contributed to that class. If you can find more people who wrote the class, they might be able to help you better than me.

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 21, 2019, 3:28pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/25 "2019-11-21T15:28:26Z")

</div>

Auhh, I got it!  
Makes sense!  
Yeah lets see if anyone else can help me!  
Thanks.

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 21, 2019, 10:33pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/26 "2019-11-21T22:33:07Z")

</div>

@dzenanz, can you please confirm the following question:

1- The bool member `(this->m_UseExplicitPDFDerivatives)` means that it will use b-spline functions as the kernel density function when it is TRUE? I’m guessing that because the papers say that b-splines have explicit derivatives.

---

<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:** [November 21, 2019, 11:31pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/27 "2019-11-21T23:31:32Z")

</div>

I don’t know. Somebody else might know 😃

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 21, 2019, 11:34pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/28 "2019-11-21T23:34:02Z")

</div>

That’s ok… Hahah that code is a lot of organized mess!

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 22, 2019, 8:55pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/29 "2019-11-22T20:55:44Z")

</div>

@dzenanz, I’m getting further.  
I need to understand the `this->m_UseExplicitPDFDerivatives`, but it is not so demanding right now.

Now, I need to know some aspects related to C++ and ITK architecture. If you can help me again, I’ll be grateful.

**1** - I need to _**know how to pass an additional variable to the metric class (.hxx) from my main .cpp during the execution**_. The new entropy needs an additional variable x. And I want to send the x value during the execution.

**2** - I created the new .h and .hxx by editing itkMattes[…].h and itkMattes[…].h.  
Now, I include the new .h class in the beggining of the main file (.cpp) and create the objects and so on… everything nice and smooth. But, in the main class, when I set the metric to the registration object, it shows an error:

```
ImageRegistration4.cxx:153: error: no matching function for call to 
‘itk::ImageRegistrationMethodv4<itk::Image<float, 2u>, itk::Image<float, 2u>, 
itk::TranslationTransform<double, 2u> 
>::SetMetric(itk::MachadoMutualInformationImageToImageMetric<itk::Image<float, 2u>, 
itk::Image<float, 2u> >::Pointer&)’
registration->SetMetric(metric);
                              ^

```

Thanks again!

---

<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:** [November 22, 2019, 9:15pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/30 "2019-11-22T21:15:57Z")

</div>

1. make m\_X a member, and add public setter and getter (`SetX` and `GetX`). You can use [macros](https://github.com/InsightSoftwareConsortium/ITK/blob/af89589d1fb9b4fd2277aa9497057657dd1a0b5a/Modules/Core/Common/include/itkMacro.h#L885-L914) for that.
2. your metric must derive from `ObjectToObjectMetricBaseTemplate`. `MattesMutualInformationImageToImageMetricv4` derives from `ImageToImageMetricv4`, which derives from `ObjectToObjectMetric`, which derives from `ObjectToObjectMetricBaseTemplate`.

Beware that `MattesMutualInformationImageToImageMetric` (without `v4` in the name) derives from `ImageToImageMetric`, which derives from `SingleValuedCostFunction`, which derives from `CostFunction`, which derives from `CostFunctionTemplate`, which derives from `Object`. This means that v4 registration framework is largely incompatible with v3 framework.

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 24, 2019, 8:58pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/31 "2019-11-24T20:58:09Z")

</div>

Perfect. I’ll work on those details carefully.  
I thought I was already sure that I was working with V4 versions, but now I have to double check.

_make m\_X a member, and add public setter and getter ( `SetX` and `GetX` ). You can use [macros] for that._

You mean, create a variable in the “.h” class and create getters and setters in both “.h” and “.hxx” classes?

I’ll work on that.  
Thanks!

---

<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:** [November 25, 2019, 1:57pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/32 "2019-11-25T13:57:44Z")

</div>

With standard macro approach, you don’t need any code in `.hxx`. A use sample is [here](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Registration/Metricsv4/include/itkImageToImageMetricv4.h#L555-L564) and [here](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Registration/Metricsv4/include/itkImageToImageMetricv4.h#L825-L826).

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 25, 2019, 3:42pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/33 "2019-11-25T15:42:58Z")

</div>

Great. I did it all. No errors, so far. _ **However, I’m not being able to RUN the whole code.** _  
_ **I’m getting stuck in the following statement:** _

```
if( this->m_JointPDFSum < itk::NumericTraits< PDFValueType >::epsilon() )
  {
   itkExceptionMacro("Joint PDF summed to zero");
   }

```

**from the […]Machado[…].hxx class.**

I checked the whole class, back and fourth, and found that `m_JointPDFSum`  
is calculated in the same class, inside the method `GetValueCommonAfterThreadedExecution()`, as follows:

```
// Sum of this threads domain into the this->m_JointPDFSum that covers that part of the domain.
 JointPDFValueType const * pdfPtr = pdfPtrStart;
 CompensatedSummation< PDFValueType > jointPDFSum;
 for( SizeValueType i = 0; i < numberOfVoxels; ++i )
 {
  jointPDFSum += *( pdfPtr++ );
 }
 this->m_JointPDFSum = jointPDFSum.GetSum();

```

**But I looked around and, I think, it is not being calculated at the right execution time. I don’t know. Could you help me by checking it?**

The **ERROR message** I get is:

```
0 -1 [NaN, NaN]
ExceptionObject caught !

itk::ExceptionObject (0x28c4ef0)
Location: "unknown" 
File: /home/leonardo/Desktop/Modulos- Source/Modulos_to_ITK/Registration/MultiModalityMachadoMI/itkMachadoMutualInformationImageToImageMetricv4.hxx
Line: 280
Description: itk::ERROR: MachadoMutualInformationImageToImageMetricv4(0x28d1320): Joint PDF 
summed to zero

07:45:24: /home/leonardo/Desktop/Modulos- 
Source/Modulos_to_ITK/Registration/MultiModalityMachadoMI/build/ImageRegistration4 exited with 
code 1

```

Thanks again!

---

<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:** [November 25, 2019, 4:04pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/34 "2019-11-25T16:04:05Z")

</div>

This usually happens when there is no overlap between the images, which mostly happens when the initial transform is not good. Double check that before any other debugging.

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 25, 2019, 5:04pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/35 "2019-11-25T17:04:53Z")

</div>

Hum… I see.

Well, I’m using the registration example `ImageRegistration4.cpp`  
the same used to test Mattes metric. And actually I did not change any aspect at all, except the metric calling from Mattes metric to Machado Metric.h files…

Those are the images fixed and moving respectively.

```
BrainT1SliceBorder20.png
BrainProtonDensitySliceShifted13x17y.png

```

![BrainT1SliceBorder20](https://discourse.itk.org/uploads/default/original/2X/4/4e4209a109612711623fb866a9a2d786ac62dfa3.png) ![BrainProtonDensitySliceShifted13x17y](https://discourse.itk.org/uploads/default/original/2X/5/5d096af52fcfae356de58047bb599cb8cd74dc24.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:** [November 25, 2019, 6:43pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/36 "2019-11-25T18:43:09Z")

</div>

Maybe some part of the code is not executing at all. Check whether you correctly override the respective functions. Compile your code with highest level of warning messages, examine them etc. Debug it 😃

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 25, 2019, 6:46pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/37 "2019-11-25T18:46:24Z")

</div>

Yeah! I’ll work on it!!  
Thanks again!

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 28, 2019, 6:52pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/38 "2019-11-28T18:52:20Z")

</div>

Guys, I did it!  
Finally, it worked as expected. The last problems I mentioned were related to my bad editions and systematical errors.  
Thanks a lot… @dzenanz! Your help was fundamental for that!  
Now I have to optimize the method but… essentially it’s working!

Many, many Thanks!

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [November 28, 2019, 7:02pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/39 "2019-11-28T19:02:25Z")

</div>

Before (left) and After registration (right):  
 ![CheckerboardBefore](https://discourse.itk.org/uploads/default/original/2X/8/80f1cb9c6493d29b2fe1ba689f183303cb1703f1.png) ![CheckerboardAfter](https://discourse.itk.org/uploads/default/original/2X/4/48b425f89d703b550aeb358a87aeddf03293c350.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:** [November 28, 2019, 7:16pm UTC](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353/40 "2019-11-28T19:16:58Z")

</div>

Good to hear it worked. Are you going to share your code?

[Previous page](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353.md?page=1)

[Next page](https://discourse.itk.org/t/changes-in-itk-mattes-mutual-information-metric/2353.md?page=3)
