Search code examples
pythonopencvkalman-filter

OpenCV Kalman Filter for Python3


I am trying to track the velocity and position of an object using Kalman Tracker.
For that purpose I have 2 detectors that return bounding boxes but no sensor that returns the velocity so I am tracking it indirectly using the state transition matrix.
So the number of dynamic paramters will be 8 (4 coordinates and each has a velocity)
The measurement has 8 coordinates in total (since 2 detectors). Currently I am fabricating the measurement since I am testing the Kalman Filter class.
Each bounding box has format - [x1, y1, x2, y2] which is top left corner, bottom right corner (LTRB)
Here is the code I am using

import numpy as np
import cv2
from scipy.linalg import block_diag

dt = 1.

dynamicParams = 8
measurementParams = 8
transitionMatrix = 1. * np.array([[1., dt, 0, 0, 0, 0, 0, 0],
                           [0, 1., 0, 0, 0, 0, 0, 0],
                           [0, 0, 1., dt, 0, 0, 0, 0],
                           [0, 0, 0, 1., 0, 0, 0, 0],
                           [0, 0, 0, 0, 1., dt, 0, 0],
                           [0, 0, 0, 0, 0, 1., 0, 0],
                           [0, 0, 0, 0, 0, 0, 1., dt],
                           [0, 0, 0, 0, 0, 0, 0, 1.]], dtype = np.float32)
measurementMatrix = 1. * np.array([[1., 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 1., 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 1., 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 1., 0],
                           [1., 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 1., 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 1., 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 1., 0]], dtype = np.float32
                          )
L = 10.0
        # All velocity and positions vectors are completely independant of each other
P = 1. * np.diag(L * np.ones(8))
        # prev_cov is just a temp variable to update self.P, P is the state covariance
prev_cov = P
# Initialize the covariance of the process noise
Q_comp_mat = 1. * np.array([[dt ** 4 / 4., dt ** 3 / 2.],
                            [dt ** 3 / 2., dt ** 2]] , dtype = np.float32)
Q = 1. * block_diag(Q_comp_mat, Q_comp_mat,
                    Q_comp_mat, Q_comp_mat)
R_scaler = 1.0
R_diag_array = 1. * R_scaler * np.array([L, L, L, L, L, L, L, L] , dtype = np.float32)
R = 1. * np.diag(R_diag_array)
processNoiseCov = 1. * Q
measurementNoiseCov = 1. * R
errorCovPost = 1. * np.array([0.])
statePost = 1. * np.array([0.])

tracker = cv2.KalmanFilter(dynamicParams, measurementParams)
tracker.transitionMatrix = 1. * transitionMatrix
tracker.measurementMatrix = 1. * measurementMatrix
tracker.processNoiseCov = 1. * processNoiseCov
tracker.measurementNoiseCov = 1. * measurementNoiseCov
tracker.errorCovPost = errorCovPost
tracker.statePost = statePost
measurement = tracker.measurementNoiseCov * np.random.randn(1, 1)
#measurement = np.array([[1,1,1,1] , [2,2,2,2]])
#pdb.set_trace()
prediction = tracker.predict()
dummy = tracker.correct(measurement)

  

The second last line throws an error saying :cv2.error: OpenCV(4.1.0) ../modules/core/src/matmul.dispatch.cpp:337: error: (-215:Assertion failed) type == B.type() in function 'gemm'
I am unable to debug this using the debugger of PyCharm since there is no code for the function OpenCV version : 4.1.0
Python version : 3.7.4 Please ask for any further clarification that is required


Solution

  • You have to set the proper type for the numpy.arrays errorCovPost and statePost:

    errorCovPost = 1. * np.array([0.])
    statePost = 1. * np.array([0.])

    errorCovPost = 1. * np.array([0.], np.float32)
    statePost = 1. * np.array([0.], np.float32)