gpt4 book ai didi

python - 卡尔曼滤波器总是预测原点

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:27 25 4
gpt4 key购买 nike

我正在学习卡尔曼滤波器以进行轨迹预测。现在,我能够追踪球。对于我对预测和卡尔曼滤波器的第一次实际尝试,我使用了此处给出的画线示例:

Is there any example of cv2.KalmanFilter implementation?

完整代码如下:

import cv2
import numpy as np
import math
cap = cv2.VideoCapture('videoplayback (1).mp4')
loHue = 0
loSaturation = 50
loValue = 50
high_hue = 0
high_saturation = 255
high_value = 255
flag_for_center = 1
def low_hue(x):
global loHue
loHue = x

#def low_saturation(x):
#global loSaturation
#loSaturation = x

#def low_value(x):
#global loValue
#loValue = x

def upper_hue (x):
global high_hue
high_hue = x

#def upper_saturation(x):
#global high_saturation
#high_saturation= x

#def upper_value(x):
#global high_value
#high_value = x

cv2.namedWindow('Trackbars', flags=cv2.WINDOW_OPENGL)
cv2.resizeWindow('Trackbars', 500, 30)
cv2.moveWindow('Trackbars', 500, 600)
cv2.createTrackbar('loHue', 'Trackbars', 0, 180, low_hue)
#cv2.createTrackbar('loSaturation', 'Trackbars', 0, 255, low_saturation)
#cv2.createTrackbar('lowValue', 'Trackbars', 0, 255, low_value)
cv2.createTrackbar('upperHue', 'Trackbars', 0, 180, upper_hue)
#cv2.createTrackbar('upperSat', 'Trackbars', 0, 255, upper_saturation)
#cv2.createTrackbar('upperValue', 'Trackbars', 0, 255, upper_value)
cv2.setTrackbarPos('loHue', 'Trackbars', 5)
cv2.setTrackbarPos('upperHue', 'Trackbars', 30)

frame_count = 0
measure = []
predicted = []

while(True):




_, image = cap.read()
frame_count = frame_count + 1
image = cv2.GaussianBlur(image, (3, 3), 2)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_limit = np.array([loHue,loSaturation,loValue])
upper_limit = np.array([high_hue,high_saturation,high_value])
mask = cv2.inRange(hsv, lower_limit, upper_limit)
res = cv2.bitwise_and(image, image, mask = mask)
#b,g,r = cv2.split(res)
#b = cv2.adaptiveThreshold(b,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
# cv2.THRESH_BINARY,11,20)
#g = cv2.adaptiveThreshold(g,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
# cv2.THRESH_BINARY, 11,20)
#r = cv2.adaptiveThreshold(r,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
# cv2.THRESH_BINARY,11,20)
#res = cv2.merge((b,g,r))
erode_element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
dilate_element = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
erosion = cv2.erode(mask, erode_element, iterations = 1)
erosion = cv2.erode(erosion, erode_element, iterations = 1)
dilation = cv2.dilate(erosion, dilate_element, iterations = 1)
dilation = cv2.dilate(dilation, dilate_element, iterations = 1)
copy_dilation = dilation.copy()

_, contours, hierarchy = cv2.findContours(copy_dilation, cv2.RETR_CCOMP,
cv2.CHAIN_APPROX_SIMPLE)
center = None

if len(contours) > 0:
c = max(contours, key = cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
x,y = center

measure = np.array([[np.float32(x)],[np.float32(y)]])
#print(measure)
#if (radius>10):
# cv2.circle(image, (int(x), int(y)), int(radius), (0, 255, 255), -2)
# cv2.circle(image, center, 3, (0,0,255),-1)
kalman = cv2.KalmanFilter(4,2)

kalman.transitionMatrix = np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32)
kalman.measurementMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], 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

#while(True):
kalman.correct(measure)
update_predicted_state = kalman.predict()
predicted.append((int(update_predicted_state[0]), int(update_predicted_state[1])))
for i in range(len(predicted)-1):
cv2.imshow('tracking', image)
cv2.moveWindow('tracking', 150, 150)
cv2.imshow('mask', mask)
cv2.moveWindow('mask', 700, 150)
cv2.circle(image, (predicted[i][0], predicted[i + 1][1]), int(radius), (0, 255, 255), -2)
k = cv2.waitKey(20) & 0xFF
if k ==27:
break
cap.release()
cv2.destroyAllWindows()

问题是预测值全为零。结果,我在左上角得到了一个四分之一圆。任何解释?顺便说一句,我运行这个东西的视频在这里: https://www.youtube.com/watch?v=CcFVOzQ1Oqc

跟踪部分运行良好,我能够跟踪球。然而,问题从这一行开始:

卡尔曼校正(测量)

当我尝试打印它时,它全为零

[[0.]
[0.]
[0.]
[0.]]

是不是因为我这里没有考虑控制矩阵?还是只是因为球的奇怪弹跳?

您可能已经猜到了,帧速率非常低。

谢谢。

最佳答案

这里有更好的解释: KalmanFilter always predict 0,0 in first time

opencv 的卡尔曼滤波器实现不允许您设置初始状态。这不直观,缺乏文档使事情变得更糟。

避免这个问题的方法是覆盖kalman.correctkalman.predict 方法。您设置要使用的变量的初始值,每次调用 correct 时,首先减去初始值。当你调用预测时,你必须添加初始值。

我这里有一个实现示例,其中卡尔曼滤波器用于视觉跟踪问题(跟踪被警察追赶的汽车): https://github.com/fredguth/unb-cv-3183/blob/master/p6/r4.py

关于python - 卡尔曼滤波器总是预测原点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50863397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com