Python3 的 OpenCV 卡尔曼滤波器
OpenCV Kalman Filter for Python3
我正在尝试使用 Kalman Tracker 跟踪物体的速度和位置。
为此,我有 2 个 return 边界框的检测器,但没有 return 速度的传感器,因此我使用状态转换矩阵间接跟踪它。
所以动态参数的数量将是 8(4 个坐标,每个都有一个速度)
测量总共有 8 个坐标(因为有 2 个检测器)。目前我正在制作测量,因为我正在测试卡尔曼滤波器 class。
每个边界框都有格式 - [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
请询问是否需要进一步说明
您必须为 numpy.array
s errorCovPost
和 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)
我正在尝试使用 Kalman Tracker 跟踪物体的速度和位置。
为此,我有 2 个 return 边界框的检测器,但没有 return 速度的传感器,因此我使用状态转换矩阵间接跟踪它。
所以动态参数的数量将是 8(4 个坐标,每个都有一个速度)
测量总共有 8 个坐标(因为有 2 个检测器)。目前我正在制作测量,因为我正在测试卡尔曼滤波器 class。
每个边界框都有格式 - [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
请询问是否需要进一步说明
您必须为 numpy.array
s errorCovPost
和 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)