import cv2
import numpy as np
# 先建立一个黑色的背景
img = np.zeros((800, 800, 3), np.uint8)
# 初始化卡尔曼滤波器
kalman = cv2.KalmanFilter(4, 2) # 四个输入,需要预测两个
kalman.measurementMatrix = np.array([[1, 0, 0, 0],
[0, 1, 0, 0]], np.float32)
kalman.transitionMatrix = np.array([[1, 0, 1, 0],
[0, 1, 0, 1],
[0, 0, 1, 0],
[0, 0, 0, 1]], np.float32) # 传递矩阵
kalman.processNoiseCov = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], np.float32) * 0.03 # 噪声
# 保存实际测量值与预测的鼠标坐标
last_measurement = None
last_prediction = None
# 回调函数
def mouse_move(event, x, y, flags, param):
# 设置为全局变量
global img, kalman, last_prediction, last_measurement
measurement = np.array([[x],
卡尔曼滤波实现鼠标追踪
最新推荐文章于 2024-07-20 17:12:48 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)