python OpenCV kalman鼠标划线案例(修正版)

Tips:

网上大多数这个案例都会在cv2.line哪里报错, 我稍微修复了一下:

import cv2
import numpy as np
import matplotlib.pyplot as plt

frame = np.zeros((800, 800, 3), np.uint8)
last_mes = current_mes = np.array((2, 1), np.float32)
last_pre = current_pre = np.array((2, 1), np.float32)


def mousemove(event, x, y, s, p):
    global frame, current_mes, mes, last_mes, current_pre, last_pre
    last_pre = current_pre
    last_mes = current_mes
    current_mes = np.array([[np.float32(x)], [np.float32(y)]])

    kalman.correct(current_mes)
    current_pre = kalman.predict()

    lmx, lmy = last_mes[0], last_mes[1]
    lpx, lpy = last_pre[0], last_pre[1]
    cmx, cmy = current_mes[0], current_mes[1]
    cpx, cpy = current_pre[0], current_pre[1]
    
    cv2.line(frame, (lmx[0],lmy[0]), (cmx[0], cmy[0]), (0, 200, 0))
    cv2.line(frame, (lpx[0], lpy[0]), (cpx[0], cpy[0]), (0, 0, 200))
    


cv2.namedWindow("Kalman")
cv2.setMouseCallback("Kalman", mousemove)
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.003
kalman.measurementNoiseCov = np.array([[1, 0], [0, 1]], np.float32) * 1

while (True):
    cv2.imshow('Kalman', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值