【问题标题】:OpenCV Kalman Filter for Python3用于 Python3 的 OpenCV 卡尔曼滤波器
【发布时间】:2020-07-03 12:09:40
【问题描述】:

我正在尝试使用 Kalman Tracker 跟踪对象的速度和位置。
为此,我有 2 个返回边界框的检测器,但没有返回速度的传感器,因此我使用状态转换矩阵间接跟踪它。
所以动态参数的数量将是 8 个(4 个坐标,每个坐标都有一个速度)
测量总共有 8 个坐标(因为有 2 个检测器)。目前我正在制作测量,因为我正在测试卡尔曼滤波器类。
每个边界框都有格式 - [x1, y1, x2, y2],即左上角、右下角 (LTRB)
这是我正在使用的代码

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)

  

倒数第二行抛出错误:cv2.error: OpenCV(4.1.0) ../modules/core/src/matmul.dispatch.cpp:337: error: (-215:Assertion failed) type == B.type() in function 'gemm'
我无法使用 PyCharm 的调试器对此进行调试,因为该函数没有代码 OpenCV 版本:4.1.0
Python版本:3.7.4 如有需要请进一步澄清

【问题讨论】:

    标签: python opencv cv2 kalman-filter


    【解决方案1】:

    您必须为numpy.arrays errorCovPoststatePost 设置正确的类型:

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

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

    【讨论】:

      猜你喜欢
      • 2011-04-14
      • 2017-08-11
      • 2012-05-14
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 2017-09-19
      相关资源
      最近更新 更多