跳至内容

参考资料 ultralytics/trackers/utils/kalman_filter.py

备注

该文件可在https://github.com/ultralytics/ultralytics/blob/main/ ultralytics/trackers/utils/kalman_filter .py 上获取。如果您发现问题,请通过提交 Pull Request🛠️ 帮助修复。谢谢🙏!



ultralytics.trackers.utils.kalman_filter.KalmanFilterXYAH

用于 bytetrack。用于跟踪图像空间中边界框的简单卡尔曼滤波器

8 维状态空间(x, y, a, h, vx, vy, va, vh)包含边界框中心位置(x, y)、长宽比、高度 h 以及各自的速度。 a、高度 h 以及各自的速度。

物体运动遵循匀速模式。边界框位置(x、y、a、h)被视为对状态空间的直接观测(线性观测模型)。 对状态空间的观测(线性观测模型)。

源代码 ultralytics/trackers/utils/kalman_filter.py
class KalmanFilterXYAH:
    """
    For bytetrack. 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. The bounding box location (x, y, a, h) is taken as direct
    observation of the state space (linear observation model).
    """

    def __init__(self):
        """Initialize Kalman filter model matrices with motion and observation uncertainty weights."""
        ndim, dt = 4, 1.0

        # 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.
        self._std_weight_position = 1.0 / 20
        self._std_weight_velocity = 1.0 / 160

    def initiate(self, measurement: np.ndarray) -> tuple:
        """
        Create track from 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.
        """
        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

    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.
        """
        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(mean, self._motion_mat.T)
        covariance = np.linalg.multi_dot((self._motion_mat, covariance, self._motion_mat.T)) + motion_cov

        return mean, covariance

    def project(self, mean: np.ndarray, covariance: np.ndarray) -> tuple:
        """
        Project state distribution to measurement space.

        Args:
            mean (ndarray): The state's mean vector (8 dimensional array).
            covariance (ndarray): The state's covariance matrix (8x8 dimensional).

        Returns:
            (tuple[ndarray, ndarray]): Returns the projected mean and covariance matrix of the given state estimate.
        """
        std = [
            self._std_weight_position * mean[3],
            self._std_weight_position * mean[3],
            1e-1,
            self._std_weight_position * mean[3],
        ]
        innovation_cov = np.diag(np.square(std))

        mean = np.dot(self._update_mat, mean)
        covariance = np.linalg.multi_dot((self._update_mat, covariance, self._update_mat.T))
        return mean, covariance + innovation_cov

    def multi_predict(self, mean: np.ndarray, covariance: np.ndarray) -> tuple:
        """
        Run Kalman filter prediction step (Vectorized version).

        Args:
            mean (ndarray): The Nx8 dimensional mean matrix of the object states at the previous time step.
            covariance (ndarray): The Nx8x8 covariance matrix of the object states 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.
        """
        std_pos = [
            self._std_weight_position * mean[:, 3],
            self._std_weight_position * mean[:, 3],
            1e-2 * np.ones_like(mean[:, 3]),
            self._std_weight_position * mean[:, 3],
        ]
        std_vel = [
            self._std_weight_velocity * mean[:, 3],
            self._std_weight_velocity * mean[:, 3],
            1e-5 * np.ones_like(mean[:, 3]),
            self._std_weight_velocity * mean[:, 3],
        ]
        sqr = np.square(np.r_[std_pos, std_vel]).T

        motion_cov = [np.diag(sqr[i]) for i in range(len(mean))]
        motion_cov = np.asarray(motion_cov)

        mean = np.dot(mean, self._motion_mat.T)
        left = np.dot(self._motion_mat, covariance).transpose((1, 0, 2))
        covariance = np.dot(left, self._motion_mat.T) + motion_cov

        return mean, covariance

    def update(self, mean: np.ndarray, covariance: np.ndarray, measurement: np.ndarray) -> tuple:
        """
        Run Kalman filter correction step.

        Args:
            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), where (x, y) is the center
                position, a the aspect ratio, and h the height of the bounding box.

        Returns:
            (tuple[ndarray, ndarray]): Returns the measurement-corrected state distribution.
        """
        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

    def gating_distance(
        self,
        mean: np.ndarray,
        covariance: np.ndarray,
        measurements: np.ndarray,
        only_position: bool = False,
        metric: str = "maha",
    ) -> np.ndarray:
        """
        Compute gating distance between state distribution and measurements. A suitable distance threshold can be
        obtained from `chi2inv95`. If `only_position` is False, the chi-square distribution has 4 degrees of freedom,
        otherwise 2.

        Args:
            mean (ndarray): Mean vector over the state distribution (8 dimensional).
            covariance (ndarray): Covariance of the state distribution (8x8 dimensional).
            measurements (ndarray): An Nx4 matrix of N measurements, each in format (x, y, a, h) where (x, y)
                is the bounding box center position, a the aspect ratio, and h the height.
            only_position (bool, optional): If True, distance computation is done with respect to the bounding box
                center position only. Defaults to False.
            metric (str, optional): The metric to use for calculating the distance. Options are 'gaussian' for the
                squared Euclidean distance and 'maha' for the squared Mahalanobis distance. Defaults to 'maha'.

        Returns:
            (np.ndarray): Returns an array of length N, where the i-th element contains the squared distance between
                (mean, covariance) and `measurements[i]`.
        """
        mean, covariance = self.project(mean, covariance)
        if only_position:
            mean, covariance = mean[:2], covariance[:2, :2]
            measurements = measurements[:, :2]

        d = measurements - mean
        if metric == "gaussian":
            return np.sum(d * d, axis=1)
        elif metric == "maha":
            cholesky_factor = np.linalg.cholesky(covariance)
            z = scipy.linalg.solve_triangular(cholesky_factor, d.T, lower=True, check_finite=False, overwrite_b=True)
            return np.sum(z * z, axis=0)  # square maha
        else:
            raise ValueError("Invalid distance metric")

__init__()

用运动和观测不确定性权重初始化卡尔曼滤波模型矩阵。

源代码 ultralytics/trackers/utils/kalman_filter.py
def __init__(self):
    """Initialize Kalman filter model matrices with motion and observation uncertainty weights."""
    ndim, dt = 4, 1.0

    # 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.
    self._std_weight_position = 1.0 / 20
    self._std_weight_velocity = 1.0 / 160

gating_distance(mean, covariance, measurements, only_position=False, metric='maha')

计算状态分布与测量值之间的门控距离。合适的距离阈值可以 从 chi2inv95.如果 only_position 为假,则奇平方分布的自由度为 4、 否则为 2。

参数

名称 类型 说明 默认值
mean ndarray

状态分布的平均向量(8 维)。

所需
covariance ndarray

状态分布的协方差(8x8 维)。

所需
measurements ndarray

一个包含 N 个测量值的 Nx4 矩阵,每个测量值的格式为(x, y, a, h),其中 (x, y) 为边界框中心位置,a 为纵横比,h 为高度。

所需
only_position bool

如果为 True,距离计算将仅根据边界框 中心位置进行距离计算。默认为 "假"。

False
metric str

用于计算距离的度量。选项有 "gaussian "表示平方欧氏距离,"maha "表示平方马氏距离。 欧氏平方距离的 "gaussian "和 Mahalanobis 平方距离的 "maha"。默认为 "maha"。

'maha'

返回:

类型 说明
ndarray

返回长度为 N 的数组,其中第 i 个元素包含 (均值、协方差)和 measurements[i].

源代码 ultralytics/trackers/utils/kalman_filter.py
def gating_distance(
    self,
    mean: np.ndarray,
    covariance: np.ndarray,
    measurements: np.ndarray,
    only_position: bool = False,
    metric: str = "maha",
) -> np.ndarray:
    """
    Compute gating distance between state distribution and measurements. A suitable distance threshold can be
    obtained from `chi2inv95`. If `only_position` is False, the chi-square distribution has 4 degrees of freedom,
    otherwise 2.

    Args:
        mean (ndarray): Mean vector over the state distribution (8 dimensional).
        covariance (ndarray): Covariance of the state distribution (8x8 dimensional).
        measurements (ndarray): An Nx4 matrix of N measurements, each in format (x, y, a, h) where (x, y)
            is the bounding box center position, a the aspect ratio, and h the height.
        only_position (bool, optional): If True, distance computation is done with respect to the bounding box
            center position only. Defaults to False.
        metric (str, optional): The metric to use for calculating the distance. Options are 'gaussian' for the
            squared Euclidean distance and 'maha' for the squared Mahalanobis distance. Defaults to 'maha'.

    Returns:
        (np.ndarray): Returns an array of length N, where the i-th element contains the squared distance between
            (mean, covariance) and `measurements[i]`.
    """
    mean, covariance = self.project(mean, covariance)
    if only_position:
        mean, covariance = mean[:2], covariance[:2, :2]
        measurements = measurements[:, :2]

    d = measurements - mean
    if metric == "gaussian":
        return np.sum(d * d, axis=1)
    elif metric == "maha":
        cholesky_factor = np.linalg.cholesky(covariance)
        z = scipy.linalg.solve_triangular(cholesky_factor, d.T, lower=True, check_finite=False, overwrite_b=True)
        return np.sum(z * z, axis=0)  # square maha
    else:
        raise ValueError("Invalid distance metric")

initiate(measurement)

从无关联的测量中创建轨道。

参数

名称 类型 说明 默认值
measurement ndarray

边界框坐标(x, y, a, h),包含中心位置 (x,y)、长宽比 a、 和高度 h。

所需

返回:

类型 说明
tuple[ndarray, ndarray]

返回新轨迹的均值向量(8 维)和协方差矩阵(8x8 维)。 新轨迹的均值向量(8 维)和协方差矩阵(8x8 维)。未观测到的速度均值初始化为 0。

源代码 ultralytics/trackers/utils/kalman_filter.py
def initiate(self, measurement: np.ndarray) -> tuple:
    """
    Create track from 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.
    """
    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

multi_predict(mean, covariance)

运行卡尔曼滤波预测步骤(矢量化版本)。

参数

名称 类型 说明 默认值
mean ndarray

上一时间步的物体状态的 Nx8 维平均矩阵。

所需
covariance ndarray

上一时间步对象状态的 Nx8x8 协方差矩阵。

所需

返回:

类型 说明
tuple[ndarray, ndarray]

返回预测状态的均值向量和协方差矩阵。未观测到的 平均值初始化为 0。

源代码 ultralytics/trackers/utils/kalman_filter.py
def multi_predict(self, mean: np.ndarray, covariance: np.ndarray) -> tuple:
    """
    Run Kalman filter prediction step (Vectorized version).

    Args:
        mean (ndarray): The Nx8 dimensional mean matrix of the object states at the previous time step.
        covariance (ndarray): The Nx8x8 covariance matrix of the object states 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.
    """
    std_pos = [
        self._std_weight_position * mean[:, 3],
        self._std_weight_position * mean[:, 3],
        1e-2 * np.ones_like(mean[:, 3]),
        self._std_weight_position * mean[:, 3],
    ]
    std_vel = [
        self._std_weight_velocity * mean[:, 3],
        self._std_weight_velocity * mean[:, 3],
        1e-5 * np.ones_like(mean[:, 3]),
        self._std_weight_velocity * mean[:, 3],
    ]
    sqr = np.square(np.r_[std_pos, std_vel]).T

    motion_cov = [np.diag(sqr[i]) for i in range(len(mean))]
    motion_cov = np.asarray(motion_cov)

    mean = np.dot(mean, self._motion_mat.T)
    left = np.dot(self._motion_mat, covariance).transpose((1, 0, 2))
    covariance = np.dot(left, self._motion_mat.T) + motion_cov

    return mean, covariance

predict(mean, covariance)

运行卡尔曼滤波预测步骤。

参数

名称 类型 说明 默认值
mean ndarray

上一时间步的物体状态的 8 维平均矢量。

所需
covariance ndarray

上一时间步对象状态的 8x8 维协方差矩阵。

所需

返回:

类型 说明
tuple[ndarray, ndarray]

返回预测状态的均值向量和协方差矩阵。未观测到的 平均值初始化为 0。

源代码 ultralytics/trackers/utils/kalman_filter.py
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.
    """
    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(mean, self._motion_mat.T)
    covariance = np.linalg.multi_dot((self._motion_mat, covariance, self._motion_mat.T)) + motion_cov

    return mean, covariance

project(mean, covariance)

将状态分布投射到测量空间。

参数

名称 类型 说明 默认值
mean ndarray

状态的平均值向量(8 维数组)。

所需
covariance ndarray

状态协方差矩阵(8x8 维)。

所需

返回:

类型 说明
tuple[ndarray, ndarray]

返回给定状态估计值的预测均值和协方差矩阵。

源代码 ultralytics/trackers/utils/kalman_filter.py
def project(self, mean: np.ndarray, covariance: np.ndarray) -> tuple:
    """
    Project state distribution to measurement space.

    Args:
        mean (ndarray): The state's mean vector (8 dimensional array).
        covariance (ndarray): The state's covariance matrix (8x8 dimensional).

    Returns:
        (tuple[ndarray, ndarray]): Returns the projected mean and covariance matrix of the given state estimate.
    """
    std = [
        self._std_weight_position * mean[3],
        self._std_weight_position * mean[3],
        1e-1,
        self._std_weight_position * mean[3],
    ]
    innovation_cov = np.diag(np.square(std))

    mean = np.dot(self._update_mat, mean)
    covariance = np.linalg.multi_dot((self._update_mat, covariance, self._update_mat.T))
    return mean, covariance + innovation_cov

update(mean, covariance, measurement)

运行卡尔曼滤波校正步骤。

参数

名称 类型 说明 默认值
mean ndarray

预测状态的平均向量(8 维)。

所需
covariance ndarray

状态协方差矩阵(8x8 维)。

所需
measurement ndarray

四维测量向量(x, y, a, h),其中 (x, y) 是中心位置,a 是长宽比,h 是边界框高度。 是中心位置,a 是长宽比,h 是边界框的高度。

所需

返回:

类型 说明
tuple[ndarray, ndarray]

返回测量校正后的状态分布。

源代码 ultralytics/trackers/utils/kalman_filter.py
def update(self, mean: np.ndarray, covariance: np.ndarray, measurement: np.ndarray) -> tuple:
    """
    Run Kalman filter correction step.

    Args:
        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), where (x, y) is the center
            position, a the aspect ratio, and h the height of the bounding box.

    Returns:
        (tuple[ndarray, ndarray]): Returns the measurement-corrected state distribution.
    """
    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



ultralytics.trackers.utils.kalman_filter.KalmanFilterXYWH

垒球 KalmanFilterXYAH

用于 BoT-SORT。用于跟踪图像空间中边界框的简单卡尔曼滤波器

8 维状态空间(x, y, w, h, vx, vy, vw, vh)包含边界框中心位置(x, y)、宽度 w、高度 h 以及各自的速度。

物体运动遵循匀速模式。边界框位置(x、y、w、h)被视为对状态空间的直接观测(线性观测模型)。 对状态空间的观测(线性观测模型)。

源代码 ultralytics/trackers/utils/kalman_filter.py
class KalmanFilterXYWH(KalmanFilterXYAH):
    """
    For BoT-SORT. A simple Kalman filter for tracking bounding boxes in image space.

    The 8-dimensional state space (x, y, w, h, vx, vy, vw, vh) contains the bounding box center position (x, y), width
    w, height h, and their respective velocities.

    Object motion follows a constant velocity model. The bounding box location (x, y, w, h) is taken as direct
    observation of the state space (linear observation model).
    """

    def initiate(self, measurement: np.ndarray) -> tuple:
        """
        Create track from unassociated measurement.

        Args:
            measurement (ndarray): Bounding box coordinates (x, y, w, h) with center position (x, y), width, and height.

        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.
        """
        mean_pos = measurement
        mean_vel = np.zeros_like(mean_pos)
        mean = np.r_[mean_pos, mean_vel]

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

    def predict(self, mean, covariance) -> 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.
        """
        std_pos = [
            self._std_weight_position * mean[2],
            self._std_weight_position * mean[3],
            self._std_weight_position * mean[2],
            self._std_weight_position * mean[3],
        ]
        std_vel = [
            self._std_weight_velocity * mean[2],
            self._std_weight_velocity * mean[3],
            self._std_weight_velocity * mean[2],
            self._std_weight_velocity * mean[3],
        ]
        motion_cov = np.diag(np.square(np.r_[std_pos, std_vel]))

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

        return mean, covariance

    def project(self, mean, covariance) -> tuple:
        """
        Project state distribution to measurement space.

        Args:
            mean (ndarray): The state's mean vector (8 dimensional array).
            covariance (ndarray): The state's covariance matrix (8x8 dimensional).

        Returns:
            (tuple[ndarray, ndarray]): Returns the projected mean and covariance matrix of the given state estimate.
        """
        std = [
            self._std_weight_position * mean[2],
            self._std_weight_position * mean[3],
            self._std_weight_position * mean[2],
            self._std_weight_position * mean[3],
        ]
        innovation_cov = np.diag(np.square(std))

        mean = np.dot(self._update_mat, mean)
        covariance = np.linalg.multi_dot((self._update_mat, covariance, self._update_mat.T))
        return mean, covariance + innovation_cov

    def multi_predict(self, mean, covariance) -> tuple:
        """
        Run Kalman filter prediction step (Vectorized version).

        Args:
            mean (ndarray): The Nx8 dimensional mean matrix of the object states at the previous time step.
            covariance (ndarray): The Nx8x8 covariance matrix of the object states 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.
        """
        std_pos = [
            self._std_weight_position * mean[:, 2],
            self._std_weight_position * mean[:, 3],
            self._std_weight_position * mean[:, 2],
            self._std_weight_position * mean[:, 3],
        ]
        std_vel = [
            self._std_weight_velocity * mean[:, 2],
            self._std_weight_velocity * mean[:, 3],
            self._std_weight_velocity * mean[:, 2],
            self._std_weight_velocity * mean[:, 3],
        ]
        sqr = np.square(np.r_[std_pos, std_vel]).T

        motion_cov = [np.diag(sqr[i]) for i in range(len(mean))]
        motion_cov = np.asarray(motion_cov)

        mean = np.dot(mean, self._motion_mat.T)
        left = np.dot(self._motion_mat, covariance).transpose((1, 0, 2))
        covariance = np.dot(left, self._motion_mat.T) + motion_cov

        return mean, covariance

    def update(self, mean, covariance, measurement) -> tuple:
        """
        Run Kalman filter correction step.

        Args:
            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, w, h), where (x, y) is the center
                position, w the width, and h the height of the bounding box.

        Returns:
            (tuple[ndarray, ndarray]): Returns the measurement-corrected state distribution.
        """
        return super().update(mean, covariance, measurement)

initiate(measurement)

从无关联的测量中创建轨道。

参数

名称 类型 说明 默认值
measurement ndarray

边界框坐标(x、y、w、h),包含中心位置(x、y)、宽度和高度。

所需

返回:

类型 说明
tuple[ndarray, ndarray]

返回新轨迹的均值向量(8 维)和协方差矩阵(8x8 维)。 新轨迹的均值向量(8 维)和协方差矩阵(8x8 维)。未观测到的速度均值初始化为 0。

源代码 ultralytics/trackers/utils/kalman_filter.py
def initiate(self, measurement: np.ndarray) -> tuple:
    """
    Create track from unassociated measurement.

    Args:
        measurement (ndarray): Bounding box coordinates (x, y, w, h) with center position (x, y), width, and height.

    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.
    """
    mean_pos = measurement
    mean_vel = np.zeros_like(mean_pos)
    mean = np.r_[mean_pos, mean_vel]

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

multi_predict(mean, covariance)

运行卡尔曼滤波预测步骤(矢量化版本)。

参数

名称 类型 说明 默认值
mean ndarray

上一时间步的物体状态的 Nx8 维平均矩阵。

所需
covariance ndarray

上一时间步对象状态的 Nx8x8 协方差矩阵。

所需

返回:

类型 说明
tuple[ndarray, ndarray]

返回预测状态的均值向量和协方差矩阵。未观测到的 平均值初始化为 0。

源代码 ultralytics/trackers/utils/kalman_filter.py
def multi_predict(self, mean, covariance) -> tuple:
    """
    Run Kalman filter prediction step (Vectorized version).

    Args:
        mean (ndarray): The Nx8 dimensional mean matrix of the object states at the previous time step.
        covariance (ndarray): The Nx8x8 covariance matrix of the object states 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.
    """
    std_pos = [
        self._std_weight_position * mean[:, 2],
        self._std_weight_position * mean[:, 3],
        self._std_weight_position * mean[:, 2],
        self._std_weight_position * mean[:, 3],
    ]
    std_vel = [
        self._std_weight_velocity * mean[:, 2],
        self._std_weight_velocity * mean[:, 3],
        self._std_weight_velocity * mean[:, 2],
        self._std_weight_velocity * mean[:, 3],
    ]
    sqr = np.square(np.r_[std_pos, std_vel]).T

    motion_cov = [np.diag(sqr[i]) for i in range(len(mean))]
    motion_cov = np.asarray(motion_cov)

    mean = np.dot(mean, self._motion_mat.T)
    left = np.dot(self._motion_mat, covariance).transpose((1, 0, 2))
    covariance = np.dot(left, self._motion_mat.T) + motion_cov

    return mean, covariance

predict(mean, covariance)

运行卡尔曼滤波预测步骤。

参数

名称 类型 说明 默认值
mean ndarray

上一时间步的物体状态的 8 维平均矢量。

所需
covariance ndarray

上一时间步对象状态的 8x8 维协方差矩阵。

所需

返回:

类型 说明
tuple[ndarray, ndarray]

返回预测状态的均值向量和协方差矩阵。未观测到的 平均值初始化为 0。

源代码 ultralytics/trackers/utils/kalman_filter.py
def predict(self, mean, covariance) -> 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.
    """
    std_pos = [
        self._std_weight_position * mean[2],
        self._std_weight_position * mean[3],
        self._std_weight_position * mean[2],
        self._std_weight_position * mean[3],
    ]
    std_vel = [
        self._std_weight_velocity * mean[2],
        self._std_weight_velocity * mean[3],
        self._std_weight_velocity * mean[2],
        self._std_weight_velocity * mean[3],
    ]
    motion_cov = np.diag(np.square(np.r_[std_pos, std_vel]))

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

    return mean, covariance

project(mean, covariance)

将状态分布投射到测量空间。

参数

名称 类型 说明 默认值
mean ndarray

状态的平均值向量(8 维数组)。

所需
covariance ndarray

状态协方差矩阵(8x8 维)。

所需

返回:

类型 说明
tuple[ndarray, ndarray]

返回给定状态估计值的预测均值和协方差矩阵。

源代码 ultralytics/trackers/utils/kalman_filter.py
def project(self, mean, covariance) -> tuple:
    """
    Project state distribution to measurement space.

    Args:
        mean (ndarray): The state's mean vector (8 dimensional array).
        covariance (ndarray): The state's covariance matrix (8x8 dimensional).

    Returns:
        (tuple[ndarray, ndarray]): Returns the projected mean and covariance matrix of the given state estimate.
    """
    std = [
        self._std_weight_position * mean[2],
        self._std_weight_position * mean[3],
        self._std_weight_position * mean[2],
        self._std_weight_position * mean[3],
    ]
    innovation_cov = np.diag(np.square(std))

    mean = np.dot(self._update_mat, mean)
    covariance = np.linalg.multi_dot((self._update_mat, covariance, self._update_mat.T))
    return mean, covariance + innovation_cov

update(mean, covariance, measurement)

运行卡尔曼滤波校正步骤。

参数

名称 类型 说明 默认值
mean ndarray

预测状态的平均向量(8 维)。

所需
covariance ndarray

状态协方差矩阵(8x8 维)。

所需
measurement ndarray

四维测量向量(x, y, w, h),其中 (x, y) 是中心位置,w 是宽度,h 是高度。 为边界框的中心位置,w 为边界框的宽度,h 为边界框的高度。

所需

返回:

类型 说明
tuple[ndarray, ndarray]

返回测量校正后的状态分布。

源代码 ultralytics/trackers/utils/kalman_filter.py
def update(self, mean, covariance, measurement) -> tuple:
    """
    Run Kalman filter correction step.

    Args:
        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, w, h), where (x, y) is the center
            position, w the width, and h the height of the bounding box.

    Returns:
        (tuple[ndarray, ndarray]): Returns the measurement-corrected state distribution.
    """
    return super().update(mean, covariance, measurement)





创建于 2023-11-12,更新于 2023-11-25
作者:glenn-jocher(3)