I am trying to perform object detection using SIFT in a live video feed and i am drawing a contour around the object of interest The program works fine at times but i often get the following error:-
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_
what is causing this and how do i fix it.
Here is my code
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()
The problem was the des_query was returning None type whenever the screen was black or too dark,so i used a try and except to solve my error here is the working code:
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()