OpenCV SIFT+ flann多次匹配单键点

发布于 01-23 16:42 字数 1076 浏览 4 评论 0原文

我正在尝试使用Open CV匹配关键点。 具体来说,我正在使用“ SIFT”检测器和“ Flann”匹配器。我的代码基于 cv2的文档

detector = cv2.SIFT_create()
matcher = cv2.FlannBasedMatcher(indexParams=dict(algorithm=0, trees=5), searchParams=dict(checks=50))
kps1, desc1 = detector.detectAndCompute(img1, None)
kps2, desc2 = detector.detectAndCompute(img2, None)
all_matches = matcher.knnMatch(desc1, desc2, 2)

ratio = 0.7
good_matches = []
for m, n in all_matches:
  if m.distance <= ratio * n.distance:
    good_matches.append(m)

我在good_matches列表,我有一些超过一个匹配的关键点:

extra_matches = dict()
for match in good_matches:
  t_idx = match.trainIdx
  reps = [mch for mch in good_matches if mch.trainIdx == t_idx]
  if len(reps) > 1 and t_idx not in extra_matches.dict():
    extra_matches[t_idx] = reps

print(len(extra_matches))  # not 0

我觉得这很奇怪,因为我认为knnmatch已经产生了2个最佳匹配。在比率宣传比赛之后,为什么我要有超过单个匹配点?

I'm trying to match keypoints using open cv.
Specifically, I'm using the "sift" detector and "flann" matcher. My code is based on cv2's documentation:

detector = cv2.SIFT_create()
matcher = cv2.FlannBasedMatcher(indexParams=dict(algorithm=0, trees=5), searchParams=dict(checks=50))
kps1, desc1 = detector.detectAndCompute(img1, None)
kps2, desc2 = detector.detectAndCompute(img2, None)
all_matches = matcher.knnMatch(desc1, desc2, 2)

ratio = 0.7
good_matches = []
for m, n in all_matches:
  if m.distance <= ratio * n.distance:
    good_matches.append(m)

I noticed than even within the good_matches list, I have some keypoints that have more than a single match:

extra_matches = dict()
for match in good_matches:
  t_idx = match.trainIdx
  reps = [mch for mch in good_matches if mch.trainIdx == t_idx]
  if len(reps) > 1 and t_idx not in extra_matches.dict():
    extra_matches[t_idx] = reps

print(len(extra_matches))  # not 0

I find this weird because I thought that knnMatch already yields the 2 best matches. Why would I have more than a single match per keypoint after ratio-pruning the matches?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2025-01-30 16:42:25

果然,发帖五分钟后,我找到了答案:

flann不进行交叉检查,这意味着我将重复第二个关键点,但对于第1个关键点,我没有重复(在我的代码中验证为出色地)。
如果您需要与Flann进行交叉检查,则最好的做法是实现自己的交叉检查或使用Flann以获取描述符的子集,然后使用bfmatcher < /code>的交叉检查选项。

以下是其他一些信息来源: [1] [2]

Sure enough, five minutes after posting I found the answer:

FLANN does not do cross-checks, which means I will have repeats of the 2nd keypoints but no repeats for the 1st keypoints (verified in my code as well).
The best practice if you need cross-check with FLANN is to implement your own cross-check or use FLANN to get a subset of descriptors and then use BFMatcher's cross-check option.

Here are some other sources of information: [1], [2]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文