我是OpenCV的新手,我正在尝试使用FLANN与ORB注册图像以匹配关键点,但是当我尝试循环我的匹配以应用Lowe's比率时,我得到以下内容:
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_'
看完这个问题后,我认为这可能是我在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存储关键点的方式有何不同,以及我如何以允许我应用Lowe比率的方式正确使用FLANN与ORB ?
修复只需更改我的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