# FFTNormalizedCrossCorrelation: Get location in image

**URL:** https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789
**Category:** Uncategorized
**Created:** [March 8, 2023, 11:35pm UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789 "2023-03-08T23:35:08Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![elapins](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@elapins](https://discourse.itk.org/u/elapins)
#### Post date: [March 8, 2023, 11:35pm UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789/1 "2023-03-08T23:35:08Z")

</div>

Hello,

I have written the following code, and obtained the correct maximum value from the FFT Normalized Cross Correlation tools in SimpleITK.  
I am now wondering, how I can get this maximum value **location(x,y)** within the image?

Thank you

> sitk::Image moving = sitk::ReadImage(“SUB.jpg”, sitk::sitkFloat32);  
> sitk::Image fixed = sitk::ReadImage(“IMG.jpg”, sitk::sitkFloat32);
> 
> sitk::Image out = sitk::FFTNormalizedCorrelation(fixed,moving);  
> out = sitk::SmoothingRecursiveGaussian(out);
> 
> sitk::MinimumMaximumImageFilter minMax;  
> minMax.Execute(out);
> 
> double max = minMax.GetMaximum();

---

<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 9, 2023, 3:08pm UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789/2 "2023-03-09T15:08:26Z")

</div>

The get the translation, you need the location of the peak. Take a look at how this is done in ITKMontage:

> <https://github.com/InsightSoftwareConsortium/ITKMontage/blob/v0.8.1/include/itkPhaseCorrelationOptimizer.hxx#L220-L640>

---

<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 10, 2023, 9:54pm UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789/3 "2023-03-10T21:54:01Z")

</div>

Hello,

Please find here a functioning version of FFT based translation initialization, based on the code I had previously shared:

> <https://github.com/SimpleITK/SimpleITKUtilities/pull/11/files#diff-79290db30abde9fe26c4b80e50efa7d4def594f35f9f5016093b7e6124b4b73b>
>
> Based on previous work to efficient initialize registraion of large micrscopy im…ages ( the images were reduced in resolition before the fft correlation was done).

It uses the uses a sequence of the RegionalMaxima, ConnectedComponents and LabelStatistics image filter to locate the maximum in the correlation image, and then produces a translation matrix.

Hope this helps,  
Brad

---

<div class="post-metadata">

### Author: ![elapins](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@elapins](https://discourse.itk.org/u/elapins)
#### Post date: [March 23, 2023, 4:37am UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789/4 "2023-03-23T04:37:27Z")

</div>

Hi @blowekamp

I was able to successfully get this to work, but can a few questions about the FFT output/results.

Are the calculated peak locations w.r.t. the FFT output image or the fixed image?  
(ie. if the peak location is 25,25 in space, would that be in regard to the output image or the fixed?)

---

<div class="post-metadata">

### Author: ![elapins](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@elapins](https://discourse.itk.org/u/elapins)
#### Post date: [March 23, 2023, 4:40am UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789/5 "2023-03-23T04:40:38Z")

</div>

As well, I am working with the following problem:

 ![image](https://discourse.itk.org/uploads/default/original/2X/4/459cf8007da6721a40e3fdf2a12f5d6e6183a24c.jpeg)

The moving image is a small section of an undeformed image. I would like to calculate the difference between its original location in the undeformed image and its new “matched” location in the deformed/fixed image.

However, I am not sure how to do this in sitk because I am still confused about the different spatial domains that are used.

Will I need to somehow resample/resize the smaller undeformed full image into the deformed image space, and find the original subimage center point in the newly resized image, then calculate the difference between that point and the FFT peak point results?

Thank you!

---

<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 23, 2023, 1:08pm UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789/6 "2023-03-23T13:08:42Z")

</div>

In the code I liked to above, a translation transform is returned which maps physical points from the fixed image to the moving image. The details of the calculation are performed in the code.

If you have any specific questions about that code please let me know.

If you are confused about the spacial domains, I suggest reading:

[https://simpleitk.readthedocs.io/en/master/fundamentalConcepts.html](https://simpleitk.readthedocs.io/en/master/fundamentalConcepts.html)  
[https://simpleitk.readthedocs.io/en/master/registrationOverview.html](https://simpleitk.readthedocs.io/en/master/registrationOverview.html)

---

<div class="post-metadata">

### Author: ![elapins](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@elapins](https://discourse.itk.org/u/elapins)
#### Post date: [March 23, 2023, 1:39pm UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789/7 "2023-03-23T13:39:47Z")

</div>

So, for example, in a 2D case, if the translation calculated by FFT is [-50,80], the translation transform being returned is just applying this singular translation pair to any point (as if it is linearly changing within the image)?

---

<div class="post-metadata">

### Author: ![elapins](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@elapins](https://discourse.itk.org/u/elapins)
#### Post date: [April 14, 2023, 1:38am UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789/8 "2023-04-14T01:38:33Z")

</div>

Hi Brad,

I have been using some similar code as you attached but for 3D Image data.

The code you referenced finds the peak point using the bounding box location and converting it to physical space. How would this be extendable to 3 dimensions?

This is what I have right now, however, the resulting peak locations are being calculated as negative values which would not make sense in this case.

Thank you in advance!

Code:

> sitk::Image out = sitk::FFTNormalizedCorrelation(fft\_fixed, fft\_moving);
> 
> out = sitk::SmoothingRecursiveGaussian(out);
> 
> //Find maximum  
> sitk::MinimumMaximumImageFilter minMax;  
> minMax.Execute(out);  
> double max = minMax.GetMaximum();
> 
> //Index max location  
> sitk::Image cc = sitk::ConnectedComponent(sitk::RegionalMaxima(out, 0.0, 1.0, true));  
> sitk::LabelStatisticsImageFilter stats;  
> stats.Execute(out, cc);  
> auto labels = stats.GetLabels();
> 
> std::vector\<std::pair\<double, int64\_t\>\> pairs;
> 
> for (int i = 0; i \< labels.size(); i++)  
> {  
> pairs.push\_back(std::make\_pair(stats.GetMean(labels[i]), labels[i]));  
> }
> 
> std::sort(pairs.begin(), pairs.end());
> 
> auto bb = stats.GetBoundingBox(pairs[pairs.size() - 1].second);
> 
> //determine peak point  
> double peak\_x = bb[0];  
> double peak\_y = bb[1];  
> double peak\_z = bb[2];
> 
> std::vector peak\_pt;  
> peak\_pt.push\_back(peak\_x);  
> peak\_pt.push\_back(peak\_y);  
> peak\_pt.push\_back(peak\_z);
> 
> auto peak\_phys = out.TransformContinuousIndexToPhysicalPoint(peak\_pt);

---

<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: [April 14, 2023, 1:25pm UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789/9 "2023-04-14T13:25:22Z")

</div>

I would suggest you break this code down into separate testable steps. At each step the output can be visually inspected, and the quatitative results verified.

For example can procedurally create a set of label and intensity image to test different conditions. This will verify that the computation is correct and as you expect.

---

<div class="post-metadata">

### Author: ![elapins](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@elapins](https://discourse.itk.org/u/elapins)
#### Post date: [April 14, 2023, 1:29pm UTC](https://discourse.itk.org/t/fftnormalizedcrosscorrelation-get-location-in-image/5789/10 "2023-04-14T13:29:40Z")

</div>

I guess I am just mainly wondering if my specific bounding box code makes sense for this part of determining the peak points?

Thank you for your help! I will definitely keep troubleshooting in this way.
