使用列表推导过滤非匹配关键点

问题描述 投票:1回答:1

我有一个使用生成的关键点匹配列表

    bf = cv2.BFMatcher(cv2.NORM_L1,crossCheck=True)
    matches = bf.match(des1,des2)

我想根据匹配从我的kps1,kps2列表中过滤掉不相关的关键点。我尝试用这种方式使用DMatch.trainIdxDMatch.queryIdx字段:

new_kps1 = [kp if idx in match.trainIdx for idx,kp in enumerate(kps1) for match in matches]

我最终这样做了:

ls1 = []
ls2 = []
for m in matches:
    ls1 += [kps1[m.trainIdx]]
    ls2 += [kps2[m.queryIdx]]

这可能以某种方式在列表理解中吗?

python opencv
1个回答
1
投票

您可以使用Python惯用语zip(*x)将元组列表转换为列表元组。这样你最初只能记录元组:

x = [(kps1[m.trainIdx], kps2[m.queryIdx]) for m in matches]
ls1, ls2 = map(list, zip(*x))

您甚至可以将其放在一行中,但代价是可读性:

ls1, ls2 = map(list, zip(*[(kps1[m.trainIdx], kps2[m.queryIdx]) for m in matches]))
© www.soinside.com 2019 - 2024. All rights reserved.