简体   繁体   中英

OpenCV KalmanFilter Python3 won't work

I try to make use of Kalman filter of open cv in python. It crashes every time I call the "correct" function. I think the code here under is self explanatory.

Is it me? OpenCV ? The python bindings (ie would it work in C++ ) ?

Any help welcome. Dummy code and output are below or on github

#!/usr/bin/env python3
import numpy as np
import cv2, numpy as np
kalman = cv2.KalmanFilter(3,3,3,cv2.CV_64F)     #Declare 3 dynamical         parameter 3 state parameter 3 control parameter
kalman.transitionMatrix=1.0*np.eye(3)           #Transition set to identity only the control and the noise are changing the state
#Dummy measurement matrix,  3 measurement each times !
kalman.measurementMatrix=np.matrix([[1.0,2.2,3.10],
                [1.1,2.2,3.30],
                [1.2,2.4,3.70],
                [1.3,2.430,3.50],
                [1.5,2.50,3.340],
                [1.5,2.60,3.70],
                [1.7,2.0,3.30],
                [1.9,2.30,3.20],
                [1.0,2.40,3.50],
                [1.3,2.60,3.60],
                [1.4,2.20,3.20],
                [1.6,2.0,3.20]])

kalman.measurementNoiseCov=1.1*np.eye(3)
kalman.processNoiseCov=1.2*np.eye(3)
kalman.controlMatrix=1.0*np.eye(3)

uk=np.matrix([[0.0],
    [1.2],
    [1.4]])

print("Now checking")
print("Measurement Matrix")
print(kalman.measurementMatrix)
print("Transition Matrix")
print(kalman.transitionMatrix)
print("MeasurementNoiseCov")
print(kalman.measurementNoiseCov)
print("ProcessNoiseCov")
print(kalman.processNoiseCov)
print("ControlMatrix")
print(kalman.controlMatrix)

print("Now attempted to make use of the underdocumented kalman filter in opencv with python ;)")
predicted=kalman.predict(uk)
print("Predicted:")
print(predicted)
print("Will (try to) correct by feeding directly the predicted to correction")
print("So here OpenCV is telling me that KF can not correct its own prediction even though the KF here has same number of dynamical,state and control parameters ?\n...")
estimated=kalman.correct(predicted)

Output (just the crashing line)

OpenCV Error: Assertion failed (C.type() == type && (((flags&GEMM_3_T) == 0 && C.rows == d_size.height && C.cols == d_size.width) || ((flags&GEMM_3_T) != 0 && C.rows == d_size.width && C.cols == d_size.height))) in gemm, file /io/opencv/modules/core/src/matmul.cpp, line 1588
Traceback (most recent call last):
  File "./opencvhorror.py", line 46, in <module>
estimated=kalman.correct(predicted)
cv2.error: /io/opencv/modules/core/src/matmul.cpp:1588: error: (-215) C.type() == type && (((flags&GEMM_3_T) == 0 && C.rows == d_size.height && C.cols == d_size.width) || ((flags&GEMM_3_T) != 0 && C.rows == d_size.width && C.cols == d_size.height)) in function gemm

好吧,我的愚蠢错误...我混淆了测量矩阵和测量值...。因此,矩阵的大小不正确

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM