问题描述
该代码是有关通过使用卡尔曼滤波器来跟踪和预测鼠标的轨迹的。 如果将第27行中的“ last_prediction = prediction.copy()”修改为“ last_prediction = prediction”,则结果将是错误的。 但是我不知道为什么。 我在等你的帮助。非常感谢!
import cv2,numpy
img=numpy.zeros((800,800,3),numpy.uint8)
kalman=cv2.KalmanFilter(4,2)
kalman.measurementMatrix=numpy.array([[1,0],[0,1,0]],numpy.float32)
kalman.transitionMatrix=numpy.array([[1,1],1]],numpy.float32)
kalman.processNoiseCov=numpy.array([[1,numpy.float32)*0.03
last_measurement=None
last_prediction=None
def click_the_mouse(event,x,y,flags,param):
global img,kalman,last_measurement,last_prediction
measurement=numpy.array([[x],[y]],numpy.float32)
if last_prediction is None:
kalman.statePre=numpy.array([[x],[y],[0],[0]],numpy.float32)
kalman.statePost=numpy.array([[x],numpy.float32)
prediction=measurement
else:
corrected_measurement=kalman.correct(measurement)
prediction=kalman.predict()
print('corrected_measurement:\n',corrected_measurement)
print('kalman.statePost:\n',kalman.statePost)
cv2.line(img,(int(last_measurement[0]),int(last_measurement[1])),(int(measurement[0]),int(measurement[1])),(0,255,0))
cv2.line(img,(int(last_prediction[0]),int(last_prediction[1])),(int(prediction[0]),int(prediction[1])),255))
last_measurement=measurement
last_prediction=prediction.copy()
#last_prediction=prediction
cv2.namedWindow('kalman')
cv2.setMouseCallback('kalman',click_the_mouse)
while True:
cv2.imshow('kalman',img)
k=cv2.waitKey(1)
if k==27:
cv2.imwrite('kalman.jpg',img)
break
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)