我当时使用SIFT算法实时检测物体。当我意识到SIFT不再免费之后,我决定使用ORB算法。我将ORB与BFMatcher一起使用。我没有任何错误要检查,但是代码找不到对象。
这是我的代码
import numpy as np
import cv2
MIN_MATCH_COUNT=30
cap = cv2.VideoCapture(0)
img1 = cv2.imread("TrainingData/deneme1.jpg",0)
orb = cv2.ORB_create()
trainKP,trainDesc=orb.detectAndCompute(img1,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)
while(1):
ret,frame= cap.read()
QueryImg=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
queryKP,queryDesc=orb.detectAndCompute(QueryImg,None)
matches = bf.match(queryDesc,trainDesc)
goodMatch=[]
for m in matches:
if(m.distance<0.75):
goodMatch.append(m)
if(len(goodMatch)>MIN_MATCH_COUNT):
tp=[]
qp=[]
for m in goodMatch:
tp.append(trainKP[m.trainIdx].pt)
qp.append(queryKP[m.queryIdx].pt)
tp,qp=np.float32((tp,qp))
H,status=cv2.findHomography(tp,qp,cv2.RANSAC,3.0)
h,w=img1.shape
trainBorder=np.float32([[[0,0],[0,h-1],[w-1,h-1],[w-1,0]]])
queryBorder=cv2.perspectiveTransform(trainBorder,H)
cv2.polylines(frame,[np.int32(queryBorder)],True,(0,255,0),5)
else:
print ("Not Enough match found- ")
cv2.imshow('result',frame)
if cv2.waitKey(10)==ord('q'):
break
cap.release()
cv2.destroyAllWindows()