├── README ├── __init__.py ├── corr.jpg ├── phase_corr.py ├── road.jpg ├── road1.jpg └── road2.jpg /README: -------------------------------------------------------------------------------- 1 | 2 | #=======================================================================# 3 | # Phase Correlation implementation in Python # 4 | # Michael Ting # 5 | # Created 25 June 2013 # 6 | # Updated 19 July 2013 # 7 | #=======================================================================# 8 | 9 | Simple image phase correlation implementation using Python - SciPy and NumPy. 10 | 11 | Images are converted to NumPy arrays which are used in the phase correlation 12 | algorithm. 13 | 14 | #=======# 15 | # Files # 16 | #=======# 17 | 18 | * phase_corr.py - Python implementation of phase correlation algorithm 19 | * road.jpg - the original image test file 20 | * road1.jpg - first image used in phase correlation example, 122x122 px 21 | * road2.jpg - second image used in phase correlation example, 122x122 px 22 | * corr.jpg - the correlation image file produced from the phase 23 | correlation of road1.jpg and road2.jpg, 122x122 px 24 | 25 | The phase correlation program reads input from the command line as follows: 26 | 27 | $ python phase_corr.py in1 in2 out 28 | 29 | * in1 - the name of the first input image 30 | * in2 - the name of the second input image 31 | * out - the name of the output image file with the phase correlation 32 | 33 | NOTE: Both input images must be the same size 34 | 35 | #==============# 36 | # Dependencies # 37 | #==============# 38 | 39 | * argparse - Available in Python >= 2.7 and 3 40 | 41 | * NumPy - Can be found at www.numpy.org 42 | 43 | * SciPy - Can be found at www.scipy.org 44 | 45 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | """ 4 | Phase Correlation implementation in Python 5 | Michael Ting 6 | """ 7 | -------------------------------------------------------------------------------- /corr.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/michaelting/Phase_Correlation/e1fb15321f6ee06d424af126014997c27e83b86d/corr.jpg -------------------------------------------------------------------------------- /phase_corr.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | """ 4 | Phase Correlation implementation in Python 5 | Michael Ting 6 | Created 25 June 2013 7 | Updated 8 July 2013 8 | 9 | Algorithm: 10 | Given two input images A and B: 11 | Apply window function on both A and B to reduce edge effects 12 | Calculate the discrete 2D Fourier transform of both A and B 13 | G_a = F{A} 14 | G_b = F{B} 15 | Calculate the cross-power spectrum by taking the complex conjugate of G_b, 16 | multiplying the Fourier transforms together elementwise, and normalizing 17 | the product elementwise 18 | R = (G_a %*% G_B*) / (|G_a G_b*|) 19 | %*% is the Hadamard (entry-wise) product 20 | Obtain normalized cross-correlation by applying the inverse Fourier transform 21 | r = F^-1{R} 22 | Determine the location of the peak in r: 23 | (del_x, del_y) = argmax over (x,y) of {r} 24 | """ 25 | 26 | import numpy as np 27 | from scipy import misc 28 | from argparse import ArgumentParser 29 | 30 | # a and b are numpy arrays 31 | def phase_correlation(a, b): 32 | G_a = np.fft.fft2(a) 33 | G_b = np.fft.fft2(b) 34 | conj_b = np.ma.conjugate(G_b) 35 | R = G_a*conj_b 36 | R /= np.absolute(R) 37 | r = np.fft.ifft2(R).real 38 | return r 39 | 40 | def main(): 41 | 42 | parser = ArgumentParser(description="Set parameters phase correlation calculation") 43 | 44 | parser.add_argument("infile1", metavar="in1", help="input image 1") 45 | parser.add_argument("infile2", metavar="in2", help="input image 2") 46 | parser.add_argument("outfile", metavar="out", help="output image file name") 47 | 48 | args = parser.parse_args() 49 | 50 | infile1 = open(args.infile1) 51 | infile2 = open(args.infile2) 52 | outfile = args.outfile 53 | newfile = open(outfile, 'w') 54 | 55 | road1 = misc.imread(infile1) 56 | road2 = misc.imread(infile2) 57 | result = phase_correlation(road1, road2) 58 | misc.imsave(newfile, result) 59 | infile1.close() 60 | infile2.close() 61 | newfile.close() 62 | 63 | if __name__=="__main__": 64 | main() 65 | -------------------------------------------------------------------------------- /road.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/michaelting/Phase_Correlation/e1fb15321f6ee06d424af126014997c27e83b86d/road.jpg -------------------------------------------------------------------------------- /road1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/michaelting/Phase_Correlation/e1fb15321f6ee06d424af126014997c27e83b86d/road1.jpg -------------------------------------------------------------------------------- /road2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/michaelting/Phase_Correlation/e1fb15321f6ee06d424af126014997c27e83b86d/road2.jpg --------------------------------------------------------------------------------