if k == ord('s'): img1 = img2.copy() cv2.imwrite('campic.png', img1) elif k == 27: break # find the keypoints and descriptors with ORB if k is not None: cv2.destroyWindow('preview') kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) # If nothing match then continue if des2 is None: img3 = img3 = draw_match(img1, kp1, img2, kp2, []) continue des1 = des1.astype(np.uint8, copy=False) # Fix the data type des2 = des2.astype(np.uint8, copy=False) # Now match describers bf = cv2.BFMatcher(cv2.NORM_HAMMING) # matches = bf.match(des1,des2) matches = bf.knnMatch(des1, des2, k=2) # m = matches[0][0] # p1, p2 = np.float32(kp1[m.queryIdx].pt), np.float32(kp2[m.trainIdx].pt) # print m.distance, p1, p2
if len(good)>MIN_MATCH_COUNT: src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2) dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0) matchesMask = mask.ravel().tolist() h,w = img1.shape pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) dst = cv2.perspectiveTransform(pts,M) cv2.polylines(img2,[np.int32(dst)],True,255,3) else: print "Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT) matchesMask = None draw_params = dict(matchColor = (0,255,0), # draw matches in green color singlePointColor = None, matchesMask = matchesMask, # draw only inliers flags = 2) # print [x for x in dir(kp1[0]) if not '__' in x] # print [x for x in dir(good[0]) if not '__' in x] if matchesMask: img3 = draw_match(img1,kp1,img2,kp2,good,None,**draw_params) cv2.imshow('matches', img3)
def find_screen_img(self, cam_img, screen_img=None, debug=False): """ Find screen_img in cam_img. If executed successfully, the function return True. Meanwhile self.recovery_matrix will be computed, which is used to map camera image to top view """ try: MATCH_THRESHOLD = 10 FLANN_INDEX_KDTREE = 0 AREA_THRESHOLD = 1000 if screen_img is None: kp1, des1 = self._screen_features screen_img = self._screen_img else: kp1, des1 = self._detector.detectAndCompute(screen_img, None) kp2, des2 = self._detector.detectAndCompute(cam_img, None) index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) search_params = dict(checks=50) flann = cv2.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(des1, des2, k=2) # Perform Lowe's ratio test to select good points to proceed with. good = [m for m, n in matches if m.distance < 0.7 * n.distance] src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) # check the property of the corners found out there self.screen2cam_matrix, mask = cv2.findHomography( src_pts, dst_pts, cv2.RANSAC, 5.0) matchesMask = mask.ravel().tolist() h, w = self._screen_img.shape[0:2] pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2) self._screen_corners = cv2.perspectiveTransform( pts, self.screen2cam_matrix) if debug: cv2.imshow( 'debug', draw_match(screen_img, kp1, self.draw_screen_boundary(cam_img), kp2, good, matchesMask=matchesMask)) else: cv2.destroyWindow('debug') if False in [ cv2.isContourConvex(self._screen_corners), cv2.contourArea(self._screen_corners) > AREA_THRESHOLD, sum(matchesMask) > MATCH_THRESHOLD ]: self.screen2cam_matrix = None self._screen_corners = None return False self.cam2screen_matrix, _ = cv2.findHomography( dst_pts, src_pts, cv2.RANSAC, 5.0) return True except cv2.error: self.screen2cam_matrix = None self._screen_corners = None return False
elif k== 27: break # find the keypoints and descriptors with ORB if k is not None: cv2.destroyWindow('preview') kp1, des1 = orb.detectAndCompute(img1,None) kp2, des2 = orb.detectAndCompute(img2,None) # If nothing match then continue if des2 is None: img3 = img3 = draw_match(img1,kp1,img2,kp2,[]) continue des1 = des1.astype(np.uint8, copy=False) # Fix the data type des2 = des2.astype(np.uint8, copy=False) # Now match describers bf = cv2.BFMatcher(cv2.NORM_HAMMING) # matches = bf.match(des1,des2) matches = bf.knnMatch(des1,des2, k=2) # m = matches[0][0] # p1, p2 = np.float32(kp1[m.queryIdx].pt), np.float32(kp2[m.trainIdx].pt) # print m.distance, p1, p2
for m in good]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) matchesMask = mask.ravel().tolist() h, w = img1.shape pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2) dst = cv2.perspectiveTransform(pts, M) cv2.polylines(img2, [np.int32(dst)], True, 255, 3) else: print "Not enough matches are found - %d/%d" % (len(good), MIN_MATCH_COUNT) matchesMask = None draw_params = dict( matchColor=(0, 255, 0), # draw matches in green color singlePointColor=None, matchesMask=matchesMask, # draw only inliers flags=2) # print [x for x in dir(kp1[0]) if not '__' in x] # print [x for x in dir(good[0]) if not '__' in x] img3 = draw_match(img1, kp1, img2, kp2, good, None, **draw_params) cv2.imshow('matches', img3)
def find_screen_img(self, cam_img, screen_img=None, debug=False): """ Find screen_img in cam_img. If executed successfully, the function return True. Meanwhile self.recovery_matrix will be computed, which is used to map camera image to top view """ try: MATCH_THRESHOLD = 10 FLANN_INDEX_KDTREE = 0 AREA_THRESHOLD = 1000 if screen_img is None: kp1, des1 = self._screen_features screen_img = self._screen_img else: kp1, des1 = self._detector.detectAndCompute(screen_img,None) kp2, des2 = self._detector.detectAndCompute(cam_img,None) index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) search_params = dict(checks = 50) flann = cv2.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(des1,des2,k=2) # Perform Lowe's ratio test to select good points to proceed with. good = [m for m,n in matches if m.distance < 0.7*n.distance] src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1,1,2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1,1,2) # check the property of the corners found out there self.screen2cam_matrix, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0) matchesMask=mask.ravel().tolist() h,w = self._screen_img.shape[0:2] pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) self._screen_corners = cv2.perspectiveTransform(pts, self.screen2cam_matrix) if debug: cv2.imshow('debug', draw_match(screen_img, kp1, self.draw_screen_boundary(cam_img), kp2, good, matchesMask=matchesMask)) else: cv2.destroyWindow('debug') if False in [cv2.isContourConvex(self._screen_corners), cv2.contourArea(self._screen_corners) > AREA_THRESHOLD, sum(matchesMask) > MATCH_THRESHOLD]: self.screen2cam_matrix = None self._screen_corners = None print "Couldn't find screen image" return False self.cam2screen_matrix, _ = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC,5.0) return True except cv2.error: self.screen2cam_matrix = None self._screen_corners = None return False