opencv 如何将Lowe比率检验与ORB和flann.knnMatch()结合使用?

wz3gfoph  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(174)

我是OpenCV的新手,我尝试使用FLANN将图像配准到ORB以匹配关键点,但当我尝试循环匹配以应用Lowe比率时,我得到以下结果:

import cv2 as cv

# Read images
src_img = cv.imread('pop_src.jpg')
dst_img = cv.imread('pop_dst.jpg')

# Create the ORB instance
orb = cv.ORB_create()

# Find keypoints and compute descriptors
src_kpts, src_desc = orb.detectAndCompute(src_img, None)
dst_kpts, dst_desc = orb.detectAndCompute(dst_img, None)

# Use FLANN to Find the Best Keypoint Matches
FLANN_INDEX_LSH = 6

index_params= dict(algorithm = FLANN_INDEX_LSH,
                   table_number = 12,
                   key_size = 20,
                   multi_probe_level = 2)
search_params = {}

flann = cv.FlannBasedMatcher(index_params, search_params)

matches = flann.knnMatch(src_desc, dst_desc, k = 2)

# Store the Keypoint Matches that Pass Lowe's Ratio Test
good_matches = []
for m, n in matches:
    if m.distance < 0.7*n.distance:
        good_matches.append(m)

matched_img = cv.drawMatches(src_img, src_kpts, dst_img, dst_kpts, good_matches, src_img, flags = 2)

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
/tmp/ipykernel_22575/771507388.py in <module>
     24 flann = cv.FlannBasedMatcher(index_params, search_params)
     25 
---> 26 matches = flann.knnMatch(src_desc, dst_desc, k = 2)
     27 
     28 # Store the Keypoint Matches that Pass Lowe's Ratio Test

error: OpenCV(4.7.0) /io/opencv/modules/flann/src/miniflann.cpp:338: error: (-5:Bad argument) Only continuous arrays are supported in function 'buildIndex_'

在阅读this Q&A之后,我认为这可能是flann.knnMatch()中的k值的结果,但这只是导致了一个不同的问题:

import cv2 as cv

# Read images
src_img = cv.imread('pop_src.jpg')
dst_img = cv.imread('pop_dest.jpg')

# Create the ORB instance
orb = cv.ORB_create()

# Find keypoints and compute descriptors
src_kpts, src_desc = orb.detectAndCompute(src_img, None)
dst_kpts, dst_desc = orb.detectAndCompute(dst_img, None)

# Use FLANN to Find the Best Keypoint Matches
FLANN_INDEX_LSH = 6

index_params= dict(algorithm = FLANN_INDEX_LSH,
                   table_number = 12,
                   key_size = 20,
                   multi_probe_level = 2)
search_params = {}

flann = cv.FlannBasedMatcher(index_params, search_params)

# Added this per the Q & A
if(src_desc is not None and len(src_desc) > 2 and dst_desc is not None and len(dst_desc) > 2):
    matches = flann.knnMatch(src_desc, dst_desc, k = 2)

# Store the Keypoint Matches that Pass Lowe's Ratio Test
good_matches = []
for m, n in matches:
    if m.distance < 0.7*n.distance:
        good_matches.append(m)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_22575/3672753189.py in <module>
     29 # Store the Keypoint Matches that Pass Lowe's Ratio Test
     30 good_matches = []
---> 31 for m, n in matches:
     32     if m.distance < 0.7*n.distance:
     33         good_matches.append(m)

ValueError: not enough values to unpack (expected 2, got 0)

我有一个非常类似的脚本使用SIFT,它运行没有问题。ORB是如何存储关键点的,它与SIFT存储关键点的方式有何不同,以及如何正确地使用FLANN和ORB,以允许我应用Lowe比率?

dkqlctbz

dkqlctbz1#

修复只是简单地更改了index_params字典中的值,脚本现在可以按预期运行了,但我很想知道为什么这样做可以修复它。

index_params= dict(algorithm = FLANN_INDEX_LSH,
                   table_number = 6,         # was 12
                   key_size = 12,            # was 20
                   multi_probe_level = 1)    # was 2

相关问题