有cv2.KalmanFilter实现的示例吗? [英] Is there any example of cv2.KalmanFilter implementation?
问题描述
我正在尝试使用适用于OpenCV(cv2)的python包装器为2D对象构建简单的追踪器.
我只注意到3个功能:
- KalmanFilter(构造函数)
- .predict()
- .correct(测量)
我的想法是创建一个代码来检查kalman是否像这样工作:
kf = cv2.KalmanFilter(...)
# set initial position
cv2.predict()
corrected_position = cv2.correct([measurement_x, measurement_y])
我找到了一些使用cv包装器的示例,但是没有使用cv2 ...
提前谢谢!
如果您使用的是opencv2.4,那么这是个坏消息:KalmanFilter不可用,因为您无法设置过渡(或任何其他)矩阵. /p>
对于opencv3.0,它可以正常运行,如下所示:
import cv2, numpy as np
meas=[]
pred=[]
frame = np.zeros((400,400,3), np.uint8) # drawing canvas
mp = np.array((2,1), np.float32) # measurement
tp = np.zeros((2,1), np.float32) # tracked / prediction
def onmouse(k,x,y,s,p):
global mp,meas
mp = np.array([[np.float32(x)],[np.float32(y)]])
meas.append((x,y))
def paint():
global frame,meas,pred
for i in range(len(meas)-1): cv2.line(frame,meas[i],meas[i+1],(0,100,0))
for i in range(len(pred)-1): cv2.line(frame,pred[i],pred[i+1],(0,0,200))
def reset():
global meas,pred,frame
meas=[]
pred=[]
frame = np.zeros((400,400,3), np.uint8)
cv2.namedWindow("kalman")
cv2.setMouseCallback("kalman",onmouse);
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
#kalman.measurementNoiseCov = np.array([[1,0],[0,1]],np.float32) * 0.00003
while True:
kalman.correct(mp)
tp = kalman.predict()
pred.append((int(tp[0]),int(tp[1])))
paint()
cv2.imshow("kalman",frame)
k = cv2.waitKey(30) &0xFF
if k == 27: break
if k == 32: reset()
I'm trying to build a veeery simple tracker for 2D objects using python wrapper for OpenCV (cv2).
I've only noticed 3 functions:
- KalmanFilter (constructor)
- .predict()
- .correct(measurement)
My idea is to create a code to check if kalman is working like this:
kf = cv2.KalmanFilter(...)
# set initial position
cv2.predict()
corrected_position = cv2.correct([measurement_x, measurement_y])
I've found some examples using the cv wrapper but not the cv2...
Thanks in advance!
if you're using opencv2.4, then it's bad news: the KalmanFilter is unusable, since you cannot set the transition (or any other) Matrix.
for opencv3.0 it works correctly, like this:
import cv2, numpy as np
meas=[]
pred=[]
frame = np.zeros((400,400,3), np.uint8) # drawing canvas
mp = np.array((2,1), np.float32) # measurement
tp = np.zeros((2,1), np.float32) # tracked / prediction
def onmouse(k,x,y,s,p):
global mp,meas
mp = np.array([[np.float32(x)],[np.float32(y)]])
meas.append((x,y))
def paint():
global frame,meas,pred
for i in range(len(meas)-1): cv2.line(frame,meas[i],meas[i+1],(0,100,0))
for i in range(len(pred)-1): cv2.line(frame,pred[i],pred[i+1],(0,0,200))
def reset():
global meas,pred,frame
meas=[]
pred=[]
frame = np.zeros((400,400,3), np.uint8)
cv2.namedWindow("kalman")
cv2.setMouseCallback("kalman",onmouse);
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
#kalman.measurementNoiseCov = np.array([[1,0],[0,1]],np.float32) * 0.00003
while True:
kalman.correct(mp)
tp = kalman.predict()
pred.append((int(tp[0]),int(tp[1])))
paint()
cv2.imshow("kalman",frame)
k = cv2.waitKey(30) &0xFF
if k == 27: break
if k == 32: reset()
这篇关于有cv2.KalmanFilter实现的示例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!