Ich möchte Opencv Kalman Filterimplementierung verwenden, um einige Rauschpunkte zu glätten. Also habe ich versucht, einen einfachen Test dafür zu programmieren.cv2.kalman funktioniert nicht richtig
Nehmen wir an, ich habe eine Beobachtung (ein Punkt). Bei jedem Frame erhalte ich eine neue Beobachtung, ich rufe Kalman voraus und Kalman richtig. Der Zustand, der nach dem opencv Kalman Filter korrekt ist, ist "dem Punkt folgend". Allerdings funktioniert der Code nicht, er hat keine Ausgabe, die wie eine Endlosschleife darin aussieht. Also kann jemand ein paar Hinweise geben, danke. Unten ist mein Code:
import cv2
import numpy as np
frame = np.zeros((800,800),np.uint8)
last_measure = current_measure = np.array((2,1),np.float32)
last_predict = current_predict = np.zeros((2,1),np.float32)
predict = []
measure = []
X = np.array([[1,0,0,0],[0,1,0,0]],np.float32)
def move(x,y):
global frame, last_measure, current_measure, measure,
current_predict, last_predict
last_predict = current_predict
last_measure = current_measure
predict.append([int(last_predict[0]),int(last_predict[1])])
measure.append([int(last_measure[0]),int(last_measure[1])])
current_measure = np.array([[np.float32(x)],[np.float32(y)]])
kalman.correct(current_measure)
current_predict = kalman.predict()
lmx,lmy = last_measure[0],last_measure[1]
cmx,cmy = current_measure[0],current_measure[1]
cpx,cpy = current_predict[0],last_predict[1]
lpx,lpy = last_predict[0],last_predict[1]
cv2.line(frame, (lmx,lmy), (cmx,cmy), (0,100,0))
cv2.line(frame, (lpx,lpy), (cpx,cpy), (0,0,200))
print(current_predict)
cv2.namedWindow("kalman")
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
for item in bound:
move(item[0],item[1])
while True:
cv2.imshow("frame",frame)