Dylan Freeman
Dylan Freeman

Reputation: 246

Interpreting results of the cv2 method: phaseCorrelate? | python opencv

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)


Upvotes: 4

Views: 5421

Answers (1)

aesari
aesari

Reputation: 217

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.

Upvotes: 2

Related Questions