mot*_*iur 12 python opencv surf orb
我正在使用SURF描述符进行图像匹配.我打算将给定的图像与图像数据库进行匹配.
import cv2
import numpy as np
surf = cv2.xfeatures2d.SURF_create(400)
img1 = cv2.imread('box.png',0)
img2 = cv2.imread('box_in_scene.png',0)
kp1,des1 = surf.detectAndCompute(img1,None)
kp2,des2 = surf.detectAndCompute(img2,None)
bf = cv2.BFMatcher(cv2.NORM_L1,crossCheck=True)
#I am planning to add more descriptors
bf.add(des1)
bf.train()
#This is my test descriptor
bf.match(des2)
Run Code Online (Sandbox Code Playgroud)
问题bf.match
在于我收到以下错误:
OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in batchDistance, file /build/opencv/src/opencv-3.1.0/modules/core/src/stat.cpp, line 3749
Traceback (most recent call last):
File "image_match4.py", line 16, in <module>
bf.match(des2)
cv2.error: /build/opencv/src/opencv-3.1.0/modules/core/src/stat.cpp:3749: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function batchDistance
Run Code Online (Sandbox Code Playgroud)
错误类似于这篇文章.给出的解释是不完整和不充分的.我想知道如何解决这个问题.我已经使用了ORB描述符以及具有NORM_HAMMING
距离的BFMatcher .错误重新出现.任何帮助将不胜感激.
我用过的两张图片是:
box.png
box_in_scene.png
我在linux中使用Python 3.5.2和OpenCV 3.1.x.
要在两个图像的描述符之间搜索,请使用:
img1 = cv2.imread('box.png',0)
img2 = cv2.imread('box_in_scene.png',0)
kp1,des1 = surf.detectAndCompute(img1,None)
kp2,des2 = surf.detectAndCompute(img2,None)
bf = cv2.BFMatcher(cv2.NORM_L1,crossCheck=False)
matches = bf.match(des1,des2)
Run Code Online (Sandbox Code Playgroud)
在多个图像中搜索
该add
方法用于添加多个测试图像的描述符.一旦所有描述符都被索引,就运行train
方法来构建基础数据结构(例如:KdTree将用于在FlannBasedMatcher的情况下进行搜索).然后,您可以运行match
以查找哪个测试图像与哪个查询图像更接近匹配.您可以检查K-d_tree并查看它如何用于搜索多维向量(Surf给出64维向量).
注意: - BruteForceMatcher,顾名思义,没有内部搜索优化数据结构,因此具有空列车方法.
多图像搜索的代码示例
import cv2
import numpy as np
surf = cv2.xfeatures2d.SURF_create(400)
# Read Images
train = cv2.imread('box.png',0)
test = cv2.imread('box_in_scene.png',0)
# Find Descriptors
kp1,trainDes1 = surf.detectAndCompute(train, None)
kp2,testDes2 = surf.detectAndCompute(test, None)
# Create BFMatcher and add cluster of training images. One for now.
bf = cv2.BFMatcher(cv2.NORM_L1,crossCheck=False) # crossCheck not supported by BFMatcher
clusters = np.array([trainDes1])
bf.add(clusters)
# Train: Does nothing for BruteForceMatcher though.
bf.train()
matches = bf.match(testDes2)
matches = sorted(matches, key = lambda x:x.distance)
# Since, we have index of only one training image,
# all matches will have imgIdx set to 0.
for i in range(len(matches)):
print matches[i].imgIdx
Run Code Online (Sandbox Code Playgroud)
有关bf.match的DMatch输出,请参阅docs.
请参阅此处的完整示例:Opencv3.0 docs.
其他信息
操作系统:Mac.
Python:2.7.10.
Opencv:3.0.0-dev [如果没记错的话,使用brew安装].
我遇到了同样的错误。但对我来说,那是因为我是用SIFT与cv2.NORM_HAMMING
指标中cv2.BFMatcher_create
。更改指标以cv2.NORM_L1
解决问题。
引用BFMatcher 的文档:
normType
–NORM_L1
、NORM_L2
、NORM_HAMMING
、 之一NORM_HAMMING2
。L1
andL2
norms 是 SIFT 和 SURF 描述符的优选选择,NORM_HAMMING
应与 ORB、BRISK 和 BriefNORM_HAMMING2
一起使用,应在WTA_K==3
or时与 ORB 一起使用4
(请参阅ORB::ORB
构造函数说明)。
编辑:使用的版本Python 3.6,OpenCV 3.4.1
在根据用户的选择准备使用SIFT或ORB 的程序时,我遇到了很多困难。最后,我可以找到SIFT和ORB的BFMatcher的正确参数
import cv2
import numpy as np
# ask user whether to use SIFT or ORB
detect_by = input("sift or orb")
Run Code Online (Sandbox Code Playgroud)
创建匹配器对象
if detect_by == "sift":
matcher = cv2.BFMatcher(normType=cv2.NORM_L2, crossCheck=False)
elif detect_by is "orb":
matcher = cv2.BFMatcher(normType=cv2.NORM_HAMMING, crossCheck=False)
Run Code Online (Sandbox Code Playgroud)捕获和处理帧时
while there_is_frame_to_process:
if detect_by is "sift":
matches = matcher.knnMatch(np.asarray(gray_des, np.float32), np.asarray(target_des, np.float32), k=2)
elif detect_by is "orb":
matches = matcher.knnMatch(np.asarray(gray_des, np.uint8), np.asarray(target_des, np.uint8), k=2)
Run Code Online (Sandbox Code Playgroud)我发现我遇到了同样的错误。花了一段时间才弄明白 - 我的一些图像有点无特色,因此没有找到关键点,并detectAndCompute
返回None
描述符。None
在传递给 之前,可能值得检查元素的描述符列表BFMatcher.add()
。
归档时间: |
|
查看次数: |
14466 次 |
最近记录: |