Question

My main problem is explained here: translation/rotation through phase correlation in python

Specifically, I'd like to know how to find the peak from a correlation matrix, as well as how to interpret the value. From the link the entire problem is explained in full detail for those interested, but the main thing to know is that I'm trying to find out how far one image is translated and rotated relative to another image (which is a slightly modified version of the other image). And I'm trying to use phase correlation to achieve this which gives me a correlation matrix (of the type numpy array) as result. I tried using argmax() on the correlation matrix which gives me a single number (215) which doesn't really hold any meaning to me. I'm expecting two numbers of which one is supposed to indicate the offset in translation and the other the rotation of one image relative to the other.

In short: How do I find the peak in a correlation matrix (in Python)?

import scipy as sp
from scipy import ndimage
from PIL import Image
from math import *
import numpy as np

def logpolar(input,silent=False):
    # This takes a numpy array and returns it in Log-Polar coordinates.

    if not silent: print("Creating log-polar coordinates...")
    # Create a cartesian array which will be used to compute log-polar coordinates.
    coordinates = sp.mgrid[0:max(input.shape)*2,0:360]
    # Compute a normalized logarithmic gradient
    log_r = 10**(coordinates[0,:]/(input.shape[0]*2.)*log10(input.shape[1]))
    # Create a linear gradient going from 0 to 2*Pi
    angle = 2.*pi*(coordinates[1,:]/360.)

    # Using scipy's map_coordinates(), we map the input array on the log-polar 
    # coordinate. Do not forget to center the coordinates!
    if not silent: print("Interpolation...")
    lpinput = ndimage.interpolation.map_coordinates(input,
                                            (log_r*sp.cos(angle)+input.shape[0]/2.,
                                             log_r*sp.sin(angle)+input.shape[1]/2.),
                                            order=3,mode='constant')

    # Returning log-normal...
    return lpinput

def load_image( infilename ) :
    img = Image.open( infilename )
    img.load()
    data = np.asarray( img, dtype="int32" )
    return data

def save_image( npdata, outfilename ) :
    img = Image.fromarray( np.asarray( np.clip(npdata,0,255), dtype="uint8"), "L" )
    img.save( outfilename )

image = load_image("C:/images/testing_image1.jpg")[:,:,0] 
target = load_image("C:/images/testing_otherimage.jpg")[:,:,0] 

# Conversion to log-polar coordinates
lpimage = logpolar(image)
lptarget = logpolar(target)

# Correlation through FFTs
Fcorr = np.fft.fft(lpimage)*np.fft.fft(lptarget)
correlation = np.fft.ifft(Fcorr)

max = np.argmax(correlation)

print max
Was it helpful?

Solution

This script here does what you are asking for: http://www.lfd.uci.edu/~gohlke/code/imreg.py.html. It provides translation, rotation and also scale invariant image co-registration using log-polar images in combination with FFT.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top