# Synopse

Computes the phase correlation of two images.

• g = iaphasecorr(f,h)
• OUTPUT
• g: Image. Phase correlation map.
• INPUT
• f: Image. n-dimensional.
• h: Image. n-dimensional.

# Description

Computes the phase correlation of two n-dimensional images. Notice that the input images must have the same dimension and size. The output is an image with same dimension and size of the input image. This output image is a phase correlation map were the point of maximum value corresponds to the translation between the input images.

```1 from numpy import *
2
3 def iaphasecorr(f,h):
4     F = fft.fftn(f)
5     H = fft.fftn(h)
6     T = F*conjugate(H)
7     R = T/abs(T)
8     g = fft.ifftn(R)
9     return g.real
```

# Examples

## Example 1

Show that the point of maximum correlation for two equal images is the origin.

``` 1 from numpy import *
2 import dicom
3 from ia636 import *
4
5 # 2D example
7 g1 = iaphasecorr(f1,f1)
8
9 adshow(ianormalize(f1), "Original 2D image - Cameraman")
10 adshow(ianormalize(g1), "Cameraman - Correlation peak: %s"%str(unravel_index(argmax(g1), g1.shape)))
11
12 # 3D example
13 filename = 'PHILIPS/DICOM/IM_0007'
15 data = dataset.pixel_array.astype(float32)
16 f2 = data[90:120,:,:]
17
18 g2 = iaphasecorr(f2,f2)
19
20 adshow(ianormalize(iamosaic(f2, 8)), "Original 3D image - Brain")
21 adshow(ianormalize(iamosaic(g2, 8)), "Brain - Correlation peak: %s"%str(unravel_index(argmax(g2), g2.shape)))
```

## Example 2

Show how to find the translation values between one image and its translated sample.

``` 1 # 2D example
2 t1 = (-50,27)
3 h1 = iaptrans(f1,t1)
4
5 g3 = iaphasecorr(f1,h1)
6
7 p1 = unravel_index(argmax(g3), g3.shape)
8 g5 = iaptrans(f1,-array(p1))
9 g7 = iaptrans(h1,p1)
10
11 adshow(ianormalize(f1), "Original 2D image - Cameraman")
12 adshow(ianormalize(h1), "Cameraman sample - Translated by %s"%str(t1))
13 adshow(ianormalize(g3), "Cameraman - Correlation peak: %s"%str(p1))
14 adshow(ianormalize(g7), "Cameraman sample - Re-translated by %s"%str(p1))
15
16 # 3D example
17 t2 = (19,-41,23)
18 h2 = iaptrans(f2,t2)
19
20 g4 = iaphasecorr(f2,h2)
21
22 p2 = unravel_index(argmax(g4), g4.shape)
23 g6 = iaptrans(f2,-array(p2))
24 g8 = iaptrans(h2,p2)
25
26 adshow(ianormalize(iamosaic(f2, 6)), "Original 3D image - Brain")
27 adshow(ianormalize(iamosaic(h2, 6)), "Brain sample - Translated by %s"%str(t2))
28 adshow(ianormalize(iamosaic(g4, 6)), "Brain - Correlation peak: %s"%str(p2))
29 adshow(ianormalize(iamosaic(g8, 6)), "Brain sample - Re-translated by %s"%str(p2))
```

## Exemplo 3

Show how to perform Template Matching using phase correlation.

``` 1 # 2D example
2 w1 = f1[27:69,83:147]
3
4 h3 = zeros(f1.shape)
5 h3[:w1.shape[0],:w1.shape[1]] = w1
6 h3 = iaptrans(h3, -array(w1.shape, dtype=int)/2)
7
8 g9 = iaphasecorr(f1,h3)
9
10 p3 = unravel_index(argmax(g9), g9.shape)
11 g11 = iaptrans(h3,p3)
12
13 adshow(ianormalize(f1), "Original 2D image - Cameraman")
15 adshow(ianormalize(g9), "Cameraman - Correlation peak: %s"%str(p3))
16 adshow(ianormalize((g11*2.+f1)/3.), "Template translated mixed with original image")
17
18 # 3D example
19 w2 = f2[17:39,35:91,27:75]
20
21 h4 = zeros(f2.shape)
22 h4[:w2.shape[0],:w2.shape[1],:w2.shape[2]] = w2
23 h4 = iaptrans(h4, -array(w2.shape, dtype=int)/2)
24
25 g10 = iaphasecorr(f2,h4)
26
27 p4 = unravel_index(argmax(g10), g10.shape)
28 g12 = iaptrans(h4,p4)
29
30 adshow(ianormalize(iamosaic(f2, 6)), "Original 3D image - Brain")
32 adshow(ianormalize(iamosaic(g10, 6)), "Brain - Correlation peak: %s"%str(p4))
33 adshow(ianormalize(iamosaic((g12*2.+f2)/3., 6)), "Template translated mixed with original image")
```

# Equation

We calculate the discrete Fourier transform of the input images and :

Next, the following equation compute

Finally, the result is given by applying the inverse discrete Fourier transform to