当前位置: 首页 > news >正文

目标跟踪——deepsort算法详细阐述

deepsort 算法详解
Unmatched Tracks(未匹配的轨迹)

本质角色:已存在的轨迹在当前帧中“失联”的状态,即预测位置与检测结果不匹配。
生命周期阶段:
已初始化: 轨迹已存在多帧,可能携带历史信息(如外观特征、运动模型)。
未被观测到: 当前帧中未找到对应的检测框,可能因
遮挡、目标消失或检测漏检
导致。
处理逻辑:
未匹配计数增加: 记录连续未匹配的帧数(如time_since_update)。
删除条件: 若未匹配次数超过阈值(如max_age),轨迹被删除。
预测继续: 即使未匹配,算法仍会预测下一帧的位置,以尝试重新匹配。

Unmatched Detections(未匹配的检测框)

本质角色: 是新检测到的候选目标,未被任何现有轨迹“认领”。

生命周期阶段:

  • 新出现的候选: 当前帧的检测结果,尚未被关联到已有轨迹。
  • 可能初始化为新轨迹:若未匹配到任何轨迹,需决定是否将其作为新轨迹的起点。

处理逻辑:

  • 初始化为新轨迹:
    • SORT:直接初始化为新轨迹(无确认机制)。
    • DeepSORT:需满足连续匹配次数阈值(如n_init=3)才能转为“确认态”。
  • 外观特征记录:DeepSORT会保存检测框的外观特征(如深度学习提取的向量),用于后续匹配。
维度 Unmatched Tracks Unmatched Detections
本质 历史存在但暂时未被观测到的目标 新出现但未被关联到任何目标的候选
处理逻辑 删除或保留决策 初始化新轨迹或忽略
算法目标 保持轨迹连续性与鲁棒性 发现新目标与抗干扰
2. 卡尔曼滤波代码分析
2. 1 初始化状态转移矩阵 F F F和观测矩阵 H H H
class KalmanFilterXYAH:
    """
    A KalmanFilterXYAH class for tracking bounding boxes in image space using a Kalman filter.

    Implements a simple Kalman filter for tracking bounding boxes in image space. The 8-dimensional state space
    (x, y, a, h, vx, vy, va, vh) contains the bounding box center position (x, y), aspect ratio a, height h, and their
    respective velocities. Object motion follows a constant velocity model, and bounding box location (x, y, a, h) is
    taken as a direct observation of the state space (linear observation model).

    Attributes:
        _motion_mat (np.ndarray): The motion matrix for the Kalman filter.    F
        _update_mat (np.ndarray): The update matrix for the Kalman filter.    H
        _std_weight_position (float): Standard deviation weight for position.  标准差的权重[x  y  a  h  dx dy da dh ]
        _std_weight_velocity (float): Standard deviation weight for velocity.

    Methods:
        initiate: Creates a track from an unassociated measurement.
        predict: Runs the Kalman filter prediction step.
        project: Projects the state distribution to measurement space.
        multi_predict: Runs the Kalman filter prediction step (vectorized version).
        update: Runs the Kalman filter correction step.
        gating_distance: Computes the gating distance between state distribution and measurements.

    Examples:
        Initialize the Kalman filter and create a track from a measurement
        >>> kf = KalmanFilterXYAH()
        >>> measurement = np.array([100, 200, 1.5, 50])
        >>> mean, covariance = kf.initiate(measurement)
        >>> print(mean)
        >>> print(covariance)
    """

    def __init__(self):
        """
        Initialize Kalman filter model matrices with motion and observation uncertainty weights.

        The Kalman filter is initialized with an 8-dimensional state space (x, y, a, h, vx, vy, va, vh), where (x, y)
        represents the bounding box center position, 'a' is the aspect ratio, 'h' is the height, and their respective
        velocities are (vx, vy, va, vh). The filter uses a constant velocity model for object motion and a linear
        observation model for bounding box location.

        Examples:
            Initialize a Kalman filter for tracking:
            >>> kf = KalmanFilterXYAH()
        """
        
        ndim, dt = 4, 1.0

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

        # Motion and observation uncertainty are chosen relative to the current state estimate. These weights control
        # the amount of uncertainty in the model.
        self._std_weight_position = 1.0 / 20
        self._std_weight_velocity = 1.0 / 160

  • ndim = 4:表示位置相关状态的数量,即 x , y , a , h x, y, a, h x,y,a,h(中心坐标、宽高比、高度)。
  • dt = 1.0:时间步长,默认为1秒。
  • 状态转移矩阵 F F F_motion_mat
  • 观测矩阵 H H H_update_mat),4×8的单位矩阵,仅保留前4行(对应位置和尺寸 x x x, y y y, a a a, h h h
  • std_weight_position:位置噪声权重(默认 0.05)。
  • std_weight_velocity:速度噪声权重(默认 0.00625)
2.2 initiate 方法分析

initiate 方法用于根据初始观测值(未关联的检测框)创建一个新的跟踪轨迹的初始状态(均值向量和协方差矩阵)
输入: 观测值为 z 0 = [ x , y , a , h ] z_0 = [x, y, a, h] z0=[x,y,a,h]
输出:

  • 均值向量 μ 0 \mu_0 μ0: 8维(包含位置和速度的初始估计)
  • 协方差矩阵 P 0 P_0 P0 :8×8维,表示初始状态的不确定性均值向量。
   def initiate(self, measurement: np.ndarray) -> tuple:
        """
        Create a track from an unassociated measurement.

        Args:
            measurement (ndarray): Bounding box coordinates (x, y, a, h) with center position (x, y), aspect ratio a,
                and height h.

        Returns:
            (tuple[ndarray, ndarray]): Returns the mean vector (8-dimensional) and covariance matrix (8x8 dimensional)
                of the new track. Unobserved velocities are initialized to 0 mean.

        Examples:
            >>> kf = KalmanFilterXYAH()
            >>> measurement = np.array([100, 50, 1.5, 200])
            >>> mean, covariance = kf.initiate(measurement)   # x_k,  p_k
        """
        mean_pos = measurement               # [4,] [x, y, a, h]
        mean_vel = np.zeros_like(mean_pos)   # [4,] [0, 0, 0, 0]
        mean = np.r_[mean_pos, mean_vel]     # [8,]  np.r_[] 按行放在一起    [100,50,1.5,200,0,0,0,0]

        std = [
            2 * self._std_weight_position * measurement[3], 	# x 方向
            2 * self._std_weight_position * measurement[3], 	# y 方向
            1e-2,												# a 
            2 * self._std_weight_position * measurement[3], 	# h方向 
            10 * self._std_weight_velocity * measurement[3],	# v_x
            10 * self._std_weight_velocity * measurement[3],	# v_y
            1e-5,												# v_a
            10 * self._std_weight_velocity * measurement[3],	# v_h
        ]   # [20.0, 20.0, 0.01, 20.0, 12.5, 12.5, 1e-05, 12.5]
        covariance = np.diag(np.square(std))    # [8,8] 对角矩阵  [20^2, 20^2 , 0.01^2, 20^2, 12.5^2, 12.5^2, (1e−5)^2, 12.5^2]
        return mean, covariance                 # (8,), (8, 8) 
2.3 predict方法分析

predict方法实现了卡尔曼滤波的预测步骤,通过状态转移矩阵 F \mathbf{F} F 和过程噪声协方差 Q \mathbf{Q} Q 对下一时刻的状态进行预测

  • 计算下一时刻的均值(位置 = 当前位置 + 速度)。
  • 更新协方差矩阵,结合状态转移和过程噪声。
  • 假设宽高比变化极小,速度噪声较低,确保滤波的鲁棒性。
def predict(self, mean: np.ndarray, covariance: np.ndarray) -> tuple:
        """
        Run Kalman filter prediction step.

        Args:
            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:
            (tuple[ndarray, ndarray]): Returns the mean vector and covariance matrix of the predicted state. Unobserved
                velocities are initialized to 0 mean.

        Examples:    x_k+1 = F* x_k  ; P_K+1 = F* p_k * F^T
            >>> kf = KalmanFilterXYAH()
            >>> mean = np.array([0, 0, 1, 1, 0, 0, 0, 0])
            >>> covariance = np.eye(8)
            >>> predicted_mean, predicted_covariance = kf.predict(mean, covariance)
        """
        std_pos = [
            self._std_weight_position * mean[3],
            self._std_weight_position * mean[3],
            1e-2,
            self._std_weight_position * mean[3],
        ]     # [0.05, 0.05, 0.01, 0.05]
        std_vel = [
            self._std_weight_velocity * mean[3],
            self._std_weight_velocity * mean

相关文章:

  • Hbase 命令行语句
  • 贪心算法——思路与例题
  • 【RAG】2410 LightRAG 简单快速的检索增强生成 4:dickens 嵌入和查询
  • 蓝桥杯历届真题 飞机降落 #DFS 解法 详细解释(C++)
  • 计算机三级信息安全技术核心知识点详细定义解析,按章节分类并重点阐述关键概念定义
  • goaccess分析网络流量
  • 如何使用 AI 和技术策略来提高软件测试覆盖率
  • 【零基础学python】python基础语法(一)
  • 如何快速解决 Postman 报错?
  • Python第六章12:序列切片练习题
  • Doris通过时间字段,按照周分组统计的sql
  • Python 编程中函数嵌套的相关解析
  • C++11QT复习 (六)
  • 架构设计之自定义延迟双删缓存注解(下)
  • 【DevOps】Android App工程的QA自动化实践
  • JS—call,apply,bind:1分钟掌握三者的区别
  • 大疆上云api介绍
  • Python基础(正则表达式)
  • 蓝桥杯-符号变反操作(差分)
  • C++ 通过vector理解迭代器的使用方法
  • 淘宝做链接的网站/申请网址怎么申请的
  • 网站建设及推广服务公司/企业网站怎么做
  • 哈尔滨seo整站优化/做企业推广的公司
  • 优秀的网站举例/国际新闻头条
  • 做PPT的网站canva/网络营销做得好的企业有哪些
  • 杭州下沙做网站的论坛/产品推广策划