import numpy as np import cv2 from matplotlib import pyplot as plt cap = cv2.VideoCapture(0) # Define the codec and create VideoWriter object fourcc = cv2.cv.CV_FOURCC(*'XVID') # cv2.VideoWriter_fourcc() does not exist out = cv2.VideoWriter('personasConteo.avi',fourcc, 40.0, (640,480)) img1 = cv2.imread('imagen1.png',0) orb = cv2.ORB() kp1 = orb.detect(img1,None) kp1, des1 = orb.compute(img1,kp1) print type(des1),des1.shape # FLANN parameters FLANN_INDEX_KDTREE = 0 index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) search_params = dict(checks=50) # or pass empty dictionary flann = cv2.FlannBasedMatcher(index_params,search_params) # BFMatcher with default params bf = cv2.BFMatcher() while(cap.isOpened()): ret, img = cap.read() # ret2, frame2 = cap2.read() if ret==True: # find the keypoints with ORB kp = orb.detect(img,None) # compute the descriptors with ORB kp, des2= orb.compute(img, kp) print "des 2",des2.shape matches = bf.knnMatch(des1,des2,k=2) # Apply ratio test good = [] for m,n in matches: if m.distance < 0.75*n.distance: good.append([m]) # cv2.drawMatchesKnn expects list of lists as matches. # img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2) img2 = cv2.drawKeypoints(img,kp,color=(0,255,0), flags=0) img1 = cv2.drawKeypoints(img1,kp1,color=(0,255,0), flags=0) cv2.imshow('matches ORB2',img2) cv2.imshow('matches ORB1',img1) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.imwrite('fast-true-features.png',img) print kp1, kp2 break else: break # Release everything if job is finished cap.release() out.release() cv2.destroyAllWindows()