cv2.KalmanFilter.predict throws error: Assertion failed in function 'cv::gemm'

Question:

I am trying to implement a mouse track program using opencv-python. I’m using a kalman filter in cv2.Kalman to predict the point of the mouse, but the function cv2.KalmanFilter.predict() throw an error to me. What’s the problem in my code and how can I fix it. Thanks.

Here is my code:

import numpy as np
import cv2

class MouseInfo():
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __eq__(self, mouse_info):
        return (self.x == mouse_info.x) and (self.y == mouse_info.y)



def getMousePos(event, x, y, flag, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        mouse_info.x = x
        mouse_info.y = y

# def trackMouse():
#     measured = (mouse_info.x, mouse_info.y)
#     kf.update(mouse_info.x, mouse_info.y)
#     estimated = [int(c) for c in kf.getEstimate()]
#     return estimated

state_num = 4
measure_num = 2

kf = cv2.KalmanFilter(state_num, measure_num, 0)
state = np.zeros(state_num, np.float32)
process_noise = np.zeros(state_num, np.float32)
measurement = np.zeros(measure_num, np.float32)

cv2.randn(state, 0, 0.1)
kf.transitionMatrix = np.array([[1, 0, 1, 0],
                                [0, 1, 0, 1],
                                [0, 0, 1, 0],
                                [0, 0, 0, 1]])
cv2.setIdentity(kf.measurementMatrix)
cv2.setIdentity(kf.processNoiseCov, 1e-5)
cv2.setIdentity(kf.measurementNoiseCov, 1e-1)
cv2.setIdentity(kf.errorCovPost, 1)

cv2.randn(kf.statePost, 0, 0.1)

mouse_info = MouseInfo()
old_mouse_info = MouseInfo()
background = np.zeros([800, 600, 3])

cv2.namedWindow('Mouse Track', cv2.WINDOW_AUTOSIZE)
cv2.setMouseCallback('Mouse Track', getMousePos)

while cv2.waitKey(1)&0xff != 27:
    statePt = MouseInfo(kf.statePost[0], kf.statePost[1])
    prediction = kf.predict()
    predictPt = MouseInfo(prediction[0], prediction[1])
    measurement[0] = mouse_info.x
    measurement[1] = mouse_info.y
    kf.correct(measurement)
    cv2.circle(background, (mouse_info.x, mouse_info.y), 5, (0, 255, 0), 1)
    cv2.circle(background, (predictPt.x, predictPt.y), 5, (0, 0, 255), 1)
    cv2.imshow('Mouse Track', background)

My OpenCV version is 3.4.2 and the error information is:

prediction = kf.predict()
    cv2.error: OpenCV(3.4.2) C:projectsopencv- 
    pythonopencvmodulescoresrcmatmul.cpp:1558: error: (-215:Assertion 
    failed) type == B.type(), (type == (((5) & ((1 << 3) - 1)) + (((1)-1) << 
    3)) || type == (((6) & ((1 << 3) - 1)) + (((1)-1) << 3)) || type == 
    (((5) & ((1 << 3) - 1)) + (((2)-1) << 3)) || type == (((6) & ((1 << 3) - 
    1)) + (((2)-1) << 3))) in function 'cv::gemm'
Asked By: 李卓然

||

Answers:

You need to set the right type np.float32to the transitionMatrix to get rid of the error

kf.transitionMatrix = np.array([[1, 0, 1, 0],
                                [0, 1, 0, 1],
                                [0, 0, 1, 0],
                                [0, 0, 0, 1]],np.float32)
kf.measurementMatrix = np.array([[1,0,0,0],
                                 [0,1,0,0]],np.float32)
#cv2.setIdentity(kf.measurementMatrix)

and for some reason I had to set the measurementMatrix in a similar way, even though I your variant should be OK.

Answered By: Claes Rolen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.