上午做一个关于选定对象跟踪的项目作为其中的一部分我必须使用opencv和python检测所选方形区域中的orb功能视频。但是这段代码不起作用。请帮助我并提前感谢
import numpy as np
import cv2
rect = (0,0,0,0)
startPoint = False
endPoint = False
def on_mouse(event,x,y,flags,params):
global rect,startPoint,endPoint
# get mouse click
if event == cv2.EVENT_LBUTTONDOWN:
if startPoint == True and endPoint == True:
startPoint = False
endPoint = False
rect = (0, 0, 0, 0)
if startPoint == False:
rect = (x, y, 0, 0)
startPoint = True
elif endPoint == False:
rect = (rect[0], rect[1], x, y)
endPoint = True
cap = cv2.VideoCapture('demo.mp4')
waitTime = 50
#Reading the first frame
(grabbed, frame) = cap.read()
#taking frames one by one
while(cap.isOpened()):
(grabbed, frame) = cap.read()
cv2.namedWindow('frame')
cv2.setMouseCallback('frame', on_mouse)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
orb = cv2.ORB_create()
kp = orb.detect(gray,None)
img2=frame.copy()
frame=cv2.drawKeypoints(gray,kp,img2,color=(0,255,0),flags=0)
cv2.imshow('frame', img2)
#waitkey
if cv2.waitKey(10)==27:
break
cv2.imshow('Video', img2)
cap.release()
cv2.destroyAllWindows()