6

I have written a simple script which returns the phase correlation between two images. To achieve this I call the cv2 method: cv2.phaseCorrelate

I understand that it returns the sub-pixel phase shift between two images, however, I am unclear with the specific details of each component of the return object (method returns a list containing a tuple, and a floating points number).

Any and all help is greatly appreciated, thank you in advance.


import cv2
import math
import time
import numpy as np

class CorrelationCalculator(object):
    'TODO: class description'

    version = '0.1'

    def __init__(self, initial_frame, detection_threshold=4):
        self.initial_frame = np.float32(cv2.cvtColor(initial_frame, cv2.COLOR_BGR2GRAY))
        self.detection_threshold = detection_threshold

    def detect_phase_shift(self, current_frame):    
        'returns detected sub-pixel phase shift between two arrays'
        self.current_frame = np.float32(cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY))
        shift = cv2.phaseCorrelate(self.initial_frame, self.current_frame)
        return shift

# implementation

import cv2

img = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')

obj = CorrelationCalculator(img)
shift = obj.detect_phase_shift(img2)

print(str(shift)

Output:

((4.3597901057868285, -2.8767423065464186), 0.4815432178477446)


1 Answer 1

4

The first tuple returned tells you the amount of shift between img and img2 in x and y coordinates. For example, consider the two images below.

Source Image Target Image

This method is supposed to find the rectangle's shift in pixel values. The other values shows the response value that we get from phase correlation process. You may think it as a measure for the certainty of the calculation. You can find the detailed information on OpenCv documentation under phaseCorrelate title.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi there, aesari. This is an excellent answer, I really appreciate the feedback. I will be sure to reference the documentation. Thank you !
Hi, sometimes I got the response value bigger than 1 although in the documentation it said that the maximum value is 1. Do you know how to interpretate it when the value is bigger than 1? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.