在 Python 中将 knnMatch 与 opencv 一起使用时出错
Error using knnMatch with opencv in Python
我正在尝试在实时视频源中使用 SIFT 执行对象检测,并且正在围绕感兴趣的对象绘制轮廓
该程序有时运行良好,但我经常收到以下错误:-
Traceback (most recent call last):
File "/home/user/adam/modify.py", line 19, in <module>
matches=flann.knnMatch(des_img,des_query,k=2)
error: /io/opencv/modules/flann/src/miniflann.cpp:315: error: (-210) type=0
in function buildIndex_
这是什么原因造成的,我该如何解决。
这是我的代码
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
img = cv2.imread("/home/user/Desktop/gre2.jpg",0)
sift=cv2.xfeatures2d.SIFT_create()
kp_img,des_img=sift.detectAndCompute(img,None)
index_params = dict(algorithm = 0 ,trees = 5)
search_params=dict()
flann = cv2.FlannBasedMatcher(index_params,search_params)
while True:
_,frame=cap.read()
grayframe=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
kp_query,des_query=sift.detectAndCompute(grayframe,None)
*matches=flann.knnMatch(des_img,des_query,k=2)*
good_points = []
for m,n in matches:
if m.distance < 0.6*n.distance:
good_points.append(m)
if len(good_points)> 10:
query_pts=np.float32([kp_img[m.queryIdx].pt for m in good_points]).reshape(-1,1,2)
train_pts=np.float32([kp_query[m.trainIdx].pt for m in good_points]).reshape(-1,1,2)
matrix,mask = cv2.findHomography(query_pts,train_pts,cv2.RANSAC,5.0)
matches_mask=mask.ravel().tolist()
h,w=img.shape
pts=np.float32([[0,0],[0,h],[w,h],[w,0]]).reshape(-1,1,2)
dst=cv2.perspectiveTransform(pts,matrix)
homography = cv2.polylines(frame,[np.int32(dst)],True,(255,0,0),3)
cv2.imshow("grayframe",homography)
else:
cv2.imshow("grayframe",frame)
k =cv2.waitKey(1)
if k ==27:
break
cap.release()
cv2.destroyAllWindows()
问题是 des_query 在屏幕变黑或太暗时返回 None 类型,所以我试了一下,除了解决我的错误,这里是工作代码:
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
img = cv2.imread("/home/keshav/Desktop/gre2.jpg",0)
sift=cv2.xfeatures2d.SIFT_create()
kp_img,des_img=sift.detectAndCompute(img,None)
index_params = dict(algorithm = 2 ,trees = 5)
search_params=dict()
flann = cv2.FlannBasedMatcher(index_params,search_params
while True:
_,frame=cap.read()
grayframe=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
kp_query,des_query=sift.detectAndCompute(grayframe,None)
try:
if(des_query==None):
pass
except:
matches=flann.knnMatch(des_img,des_query,k=2)
good_points = []
for m,n in matches:
if m.distance < 0.6*n.distance:
good_points.append(m)
if len(good_points)> 10:
query_pts=np.float32([kp_img[m.queryIdx].pt for m in good_points]).reshape(-1,1,2)
train_pts=np.float32([kp_query[m.trainIdx].pt for m in good_points]).reshape(-1,1,2)
matrix,mask = cv2.findHomography(query_pts,train_pts,cv2.RANSAC,5.0)
matches_mask=mask.ravel().tolist()
h,w=img.shape
pts=np.float32([[0,0],[0,h],[w,h],[w,0]]).reshape(-1,1,2)
print(pts)
dst=cv2.perspectiveTransform(pts,matrix)
homography = cv2.polylines(frame,[np.int32(dst)],True,(255,0,0),3)
cv2.imshow("grayframe",homography)
else:
cv2.imshow("grayframe",frame)
k =cv2.waitKey(1)
if k ==27:
break
cap.release()
cv2.destroyAllWindows()
我正在尝试在实时视频源中使用 SIFT 执行对象检测,并且正在围绕感兴趣的对象绘制轮廓 该程序有时运行良好,但我经常收到以下错误:-
Traceback (most recent call last):
File "/home/user/adam/modify.py", line 19, in <module>
matches=flann.knnMatch(des_img,des_query,k=2)
error: /io/opencv/modules/flann/src/miniflann.cpp:315: error: (-210) type=0
in function buildIndex_
这是什么原因造成的,我该如何解决。
这是我的代码
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
img = cv2.imread("/home/user/Desktop/gre2.jpg",0)
sift=cv2.xfeatures2d.SIFT_create()
kp_img,des_img=sift.detectAndCompute(img,None)
index_params = dict(algorithm = 0 ,trees = 5)
search_params=dict()
flann = cv2.FlannBasedMatcher(index_params,search_params)
while True:
_,frame=cap.read()
grayframe=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
kp_query,des_query=sift.detectAndCompute(grayframe,None)
*matches=flann.knnMatch(des_img,des_query,k=2)*
good_points = []
for m,n in matches:
if m.distance < 0.6*n.distance:
good_points.append(m)
if len(good_points)> 10:
query_pts=np.float32([kp_img[m.queryIdx].pt for m in good_points]).reshape(-1,1,2)
train_pts=np.float32([kp_query[m.trainIdx].pt for m in good_points]).reshape(-1,1,2)
matrix,mask = cv2.findHomography(query_pts,train_pts,cv2.RANSAC,5.0)
matches_mask=mask.ravel().tolist()
h,w=img.shape
pts=np.float32([[0,0],[0,h],[w,h],[w,0]]).reshape(-1,1,2)
dst=cv2.perspectiveTransform(pts,matrix)
homography = cv2.polylines(frame,[np.int32(dst)],True,(255,0,0),3)
cv2.imshow("grayframe",homography)
else:
cv2.imshow("grayframe",frame)
k =cv2.waitKey(1)
if k ==27:
break
cap.release()
cv2.destroyAllWindows()
问题是 des_query 在屏幕变黑或太暗时返回 None 类型,所以我试了一下,除了解决我的错误,这里是工作代码:
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
img = cv2.imread("/home/keshav/Desktop/gre2.jpg",0)
sift=cv2.xfeatures2d.SIFT_create()
kp_img,des_img=sift.detectAndCompute(img,None)
index_params = dict(algorithm = 2 ,trees = 5)
search_params=dict()
flann = cv2.FlannBasedMatcher(index_params,search_params
while True:
_,frame=cap.read()
grayframe=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
kp_query,des_query=sift.detectAndCompute(grayframe,None)
try:
if(des_query==None):
pass
except:
matches=flann.knnMatch(des_img,des_query,k=2)
good_points = []
for m,n in matches:
if m.distance < 0.6*n.distance:
good_points.append(m)
if len(good_points)> 10:
query_pts=np.float32([kp_img[m.queryIdx].pt for m in good_points]).reshape(-1,1,2)
train_pts=np.float32([kp_query[m.trainIdx].pt for m in good_points]).reshape(-1,1,2)
matrix,mask = cv2.findHomography(query_pts,train_pts,cv2.RANSAC,5.0)
matches_mask=mask.ravel().tolist()
h,w=img.shape
pts=np.float32([[0,0],[0,h],[w,h],[w,0]]).reshape(-1,1,2)
print(pts)
dst=cv2.perspectiveTransform(pts,matrix)
homography = cv2.polylines(frame,[np.int32(dst)],True,(255,0,0),3)
cv2.imshow("grayframe",homography)
else:
cv2.imshow("grayframe",frame)
k =cv2.waitKey(1)
if k ==27:
break
cap.release()
cv2.destroyAllWindows()