轨迹合并学习笔记

import numpy as np

def find_contiguous_indices(mask_all, max_gap=3):
    # 初始化变量
    segments = []
    n = len(mask_all)
    i = 0

    # 先找到所有连续的 True 段
    while i < n:
        if mask_all[i]:
            start = i
            while i < n and mask_all[i]:
                i += 1
            end = i - 1
            segments.append((start, end))
        else:
            i += 1

    # 合并间隔 < max_gap 的段
    if not segments:
        return np.array([], dtype=int)  # 如果没有 True,返回空数组

    merged_segments = [segments[0]]
    for current in segments[1:]:
        last = merged_segments[-1]
        if current[0] - last[1] - 1 < max_gap:
            merged_segments[-1] = (last[0], current[1])  # 合并
        else:
            merged_segments.append(current)

    # 生成所有 True 的索引列表
    indices = []
    for start, end in merged_segments:
        indices.append([i for i in range(start, end + 1)])  # 添加该段所有索引

    return indices

# 示例 mask_all
mask_all = np.array([
    True, True, False, False, True, False, True, True, True,
    False, False, False, False, True, True, False, True
], dtype=bool)

print("原始 mask_all:", mask_all)
indices = find_contiguous_indices(mask_all, max_gap=3)

for index in indices:
    print(index)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值