# 目标跟踪-------卡尔曼滤波

14 篇文章 3 订阅

### deep sort 算法中的卡尔曼滤波

A simple Kalman filter for tracking bounding boxes in image space.

### 1. Kalman filter 类的初始化

1. 创建卡尔曼滤波器模型矩阵

2.相对于当前状态估计值选择运动和观察不确定性。这些权重控制模型中的不确定性量

def __init__(self):
ndim, dt = 4, 1.

# Create Kalman filter model matrices.
self._motion_mat = np.eye(2 * ndim, 2 * ndim)
for i in range(ndim):
self._motion_mat[i, ndim + i] = dt
self._update_mat = np.eye(ndim, 2 * ndim)

# Motion and observation uncertainty are chosen relative to the current
# state estimate. These weights control the amount of uncertainty in
# the model. This is a bit hacky.
self._std_weight_position = 1. / 20
# 可调整参数
self._std_weight_velocity = 1. / 160

_motion_mat

### 2. Create track from unassociated measurement

def _initiate_track(self, detection):
mean, covariance = self.kf.initiate(detection.to_xyah())

Kalman filter 对象的  initiate( ) 函数

Returns the mean vector (8 dimensional) and covariance matrix (8x8 dimensional) of the new track. 未观察到的速度初始化为0均值

def initiate(self, measurement):

mean_pos = measurement
mean_vel = np.zeros_like(mean_pos)
mean = np.r_[mean_pos, mean_vel]

std = [
2 * self._std_weight_position * measurement[3],
2 * self._std_weight_position * measurement[3],
1e-2,
2 * self._std_weight_position * measurement[3],
10 * self._std_weight_velocity * measurement[3],
10 * self._std_weight_velocity * measurement[3],
1e-5,
10 * self._std_weight_velocity * measurement[3]]
covariance = np.diag(np.square(std))
return mean, covariance

### 3. Run Kalman filter prediction step

def predict(self):

for track in self.tracks:
track.predict(self.kf)
def predict(self, kf):

self.mean, self.covariance = kf.predict(self.mean, self.covariance)
self.age += 1
self.time_since_update += 1

Parameters:

mean : ndarray     The 8 dimensional mean vector of the object state at the previous  time step.

covariance : ndarray    The 8x8 dimensional covariance matrix of the object state at the  previous time step.

Returns:

the mean vector and covariance matrix of the predicted  state. Unobserved velocities are initialized to 0 mean.

predict( )

def predict(self, mean, covariance):

std_pos = [
self._std_weight_position * mean[3],
self._std_weight_position * mean[3],
1e-2,
self._std_weight_position * mean[3]]
std_vel = [
self._std_weight_velocity * mean[3],
self._std_weight_velocity * mean[3],
1e-5,
self._std_weight_velocity * mean[3]]
motion_cov = np.diag(np.square(np.r_[std_pos, std_vel]))

mean = np.dot(self._motion_mat, mean)
covariance = np.linalg.multi_dot((
self._motion_mat, covariance, self._motion_mat.T)) + motion_cov

return mean, covariance

### 4. Run Kalman filter correction step.  （校正步骤）

def update(self, detections):
"""Perform measurement update and track management.

Parameters
----------
detections : List[deep_sort.detection.Detection]
A list of detections at the current time step.

"""

matches, unmatched_tracks, unmatched_detections = \
self._match(detections)

# Update track set.
for track_idx, detection_idx in matches:
self.tracks[track_idx].update(
self.kf, detections[detection_idx])

.......
def update(self, kf, detection):
"""Perform Kalman filter measurement update step and update the feature
cache.

Parameters
----------
kf : kalman_filter.KalmanFilter
The Kalman filter.
detection : Detection
The associated detection.

"""
self.mean, self.covariance = kf.update(
self.mean, self.covariance, detection.to_xyah())
self.features.append(detection.feature)

self.hit_streak += 1
self.hits += 1
self.time_since_update = 0
if self.state == TrackState.Tentative and self.hits >= self._n_init:
self.state = TrackState.Confirmed

kf.update( )函数

Parameters：
mean : ndarray     The predicted state's mean vector (8 dimensional).

covariance : ndarray     The state's covariance matrix (8x8 dimensional).

measurement : ndarray       The 4 dimensional measurement vector (x, y, a, h)

Returns       (ndarray, ndarray)
Returns the measurement-corrected state distribution   返回测量校正的状态分布

def update(self, mean, covariance, measurement):

projected_mean, projected_cov = self.project(mean, covariance)

chol_factor, lower = scipy.linalg.cho_factor(
projected_cov, lower=True, check_finite=False)
kalman_gain = scipy.linalg.cho_solve(
(chol_factor, lower), np.dot(covariance, self._update_mat.T).T,
check_finite=False).T
innovation = measurement - projected_mean

new_mean = mean + np.dot(innovation, kalman_gain.T)
new_covariance = covariance - np.linalg.multi_dot((
kalman_gain, projected_cov, kalman_gain.T))
return new_mean, new_covariance

• 2
点赞
• 1
评论
• 22
收藏
• 打赏
• 扫一扫，分享海报

12-12

10-23
04-07
06-30 1万+
01-12 6万+
02-22 1万+
07-19 2万+
03-27 1050
03-27 6496
11-20 3039
01-01 2万+

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。