简体   繁体   English

Opencv Python Kalman过滤器预测结果查询

[英]Opencv Python Kalman filter prediction outcome query

I've been using Opencv with python, I'm using the kalman filter on a rectangle i've got using background subtraction and MOSSE, then on this I'm going to predict the next location with another rectangle in a different colour. 我一直在使用Opencv和python,我在使用背景减法和MOSSE的矩形上使用卡尔曼滤镜,然后在此我将预测另一个颜色不同的矩形的下一个位置。 The prediction output comes out offset too much. 预测输出偏移太多。

草莓走

I've edited the different results, the main issue is I'm unsure on the kalman implementation in Opencv as the documentation isn't great, its hard to know what it returns. 我已经编辑了不同的结果,主要问题是我不确定Opencv中的kalman实现,因为文档不是很好,很难知道返回的内容。

This is my Kalman method (I've looked at examples, and I understand the basics, but I've assumed this should work to be honest). 这是我的Kalman方法(我看过示例,并且了解基本知识,但是我认为这应该是诚实的)。

kf = cv2.KalmanFilter(4, 2)
kf.measurementMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], np.float32)
kf.transitionMatrix = np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32)

def Estimate(self, coordX, coordY):
    measured = np.array([[np.float32(coordX)], [np.float32(coordY)]])
    self.kf.correct(measured)
    predicted = self.kf.predict()
    return predicted

Then per box found I run this 然后发现每个盒子我运行

kalmanPredict = Kalman().Estimate(newbox[0],newbox[1])

kalmanPredict[0] = kalmanPredict[0] + newbox[0]
kalmanPredict[1] = kalmanPredict[1] + newbox[1]

p = np.asarray(self.centralize((p1,p2), (kalmanPredict[0],kalmanPredict[1])))
p = np.int0(p)

kpt1 = p[0],p[1]
kpt2 = p[2],p[3]

cv2.rectangle(frame, kpt1, kpt2, (255,0,0),2)

centralize is a method I tested online that just centers the points. 集中化是我在网上测试过的一种方法,它只是将点居中。

    def centralize(self, box, c):
        pt1, pt2 = box
        xA, yA = pt1
        xB, yB = pt2
        cx, cy = c
        w = xB - xA
        h = yB - yA
        halfW = int(w/2)
        halfH = int(h/2)
        xA = cx - halfW
        yA = cy - halfH
        xB = xA + w
        yB = yA + h
        return xA, yA, xB, yB

it should take into account position/velocity - but just unsure with these docs, and the resources online. 它应该考虑位置/速度-但只是不确定这些文档以及在线资源。 Any help would be much appreciated, thanks. 任何帮助将不胜感激,谢谢。

I think I have solved it, I believe it isn't the center points it returns, it the xy coords of the top left corner of the new rect - so I just addded width and height to the second bounding box x,y coords. 我想我已经解决了,我相信它不是返回的中心点,而是新矩形左上角的xy坐标-所以我只是将宽度和高度添加到第二个边界框x,y坐标。

kalmanPredict = Kalman().Estimate(newbox[0],newbox[1])

kalx1 = kalmanPredict[0] 
kaly1 = kalmanPredict[1]

kal1 = (kalx1, kaly1)

kalx = kalmanPredict[0] + w
kaly = kalmanPredict[1] + h

kal2 = (kalx, kaly)

cv2.rectangle(frame, kal1, kal2, (255,0,0),2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM