如何优化forloop

2024-09-30 06:31:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用for循环从opencv knnFlann matcher过滤匹配,但是我需要优化它

代码:

def orb_calc_matches(matches, distance_range=0.65):
    good_matches = []
    queried_matches = []

    for i in range(len(matches)):
        if len(matches[i]) == 2:
            if ((matches[i][0].trainIdx not in queried_matches) and (matches[i][0].distance < distance_range * matches[i][1].distance)):
                good_matches.append(matches[i][0])
                queried_matches.append(matches[i][0].trainIdx)
    return good_matches

索姆博迪能提出一些更有效的方法吗?你知道吗


Tags: 代码inforlenifrangematcheropencv
1条回答
网友
1楼 · 发布于 2024-09-30 06:31:37

使queried_matches成为set。使用for m in matches:而不是索引循环,因为不需要索引。你知道吗

def orb_calc_matches(matches, distance_range=0.65):
    good_matches = []
    queried_matches = set()

    for m in matches:
        if len(m) == 2:
            if ((m[0].trainIdx not in queried_matches) and
                (m[0].distance < distance_range * m[1].distance)):
                good_matches.append(m[0])
                queried_matches.add(m[0].trainIdx)
    return good_matches

相关问题 更多 >

    热门问题