如何优化循环

时间:2019-02-10 18:26:31

标签: python performance for-loop optimization

我正在使用for循环从opencv knnFlann匹配器中过滤匹配项,但我需要对其进行优化

代码:

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

某人可以提出一些更有效的方法吗?

1 个答案:

答案 0 :(得分:0)

</tag>设为queried_matches。并使用set而不是索引循环,因为不需要索引。

for m in matches: