def __init__(self, method='fast', grid=(12,10), max_corners=1200, max_levels=4, subpixel=False, params=fast_params): # Determine detector type that implements detect try: self.detector_ = FeatureDetector.detectors[method](**params) except Exception as e: raise RuntimeError('Unknown detector type: %s! Use from {:}, {:}'.format(list(FeatureDetector.detectors.keys()), e)) # Only support grid and pyramid with gftt and fast if (method == 'gftt' or method == 'fast'): # Establish pyramid if max_levels > 0: self.detector_ = cv2.PyramidAdaptedFeatureDetector( detector=self.detector_, maxLevel=max_levels) # Establish grid if (grid is not None and max_corners): try: self.detector_ = cv2.GridAdaptedFeatureDetector( self.detector_, max_corners, grid[0], grid[1]) except: raise ValueError('FeatureDetector grid is not compatible {:}'.format(grid)) # Check detector self.check_detector() self.params_ = params self.max_levels_ = max_levels self.max_corners_ = max_corners self.grid_ = grid self.subpixel_ = subpixel
def get_sift(img, show=1, maxfeatures=500, xgrid=1, ygrid=1): img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) sift_det = cv2.FeatureDetector_create("SIFT") sift_det_grid = cv2.GridAdaptedFeatureDetector(sift_det, maxfeatures, xgrid, ygrid) sift_desc = cv2.DescriptorExtractor_create("SIFT") fs = sift_det_grid.detect(img2) (keypoints, descriptors) = sift_desc.compute(img2, fs) if show == 1: img_wSIFT = img.copy() d_blue = cv2.cv.RGB(25, 15, 100) l_blue = cv2.cv.RGB(200, 200, 250) for f in fs: cv2.circle(img_wSIFT, (int(f.pt[0]), int(f.pt[1])), int(f.size/2), l_blue, 2, cv2.CV_AA) cv2.circle(img_wSIFT, (int(f.pt[0]), int(f.pt[1])), int(f.size/2), d_blue, 1, cv2.CV_AA) ori = math.radians(f.angle) tx = math.cos(ori) * 0 - math.sin(ori) * (f.size/2) ty = math.sin(ori) * 0 + math.cos(ori) * (f.size/2) tx += f.pt[0] ty += f.pt[1] cv2.line(img_wSIFT, (int(f.pt[0]), int(f.pt[1])), (int(tx), int(ty)), l_blue, 2, cv2.CV_AA) cv2.line(img_wSIFT, (int(f.pt[0]), int(f.pt[1])), (int(tx), int(ty)), d_blue, 1, cv2.CV_AA) plt.imshow(img_wSIFT[:, :, ::-1]) plt.show() return keypoints, descriptors
def __init__(self, grid=(20,20), threshold=20, max_corners=10000): try: from pybot_vision import fast_proposals self.detector_ = cv2.GridAdaptedFeatureDetector( get_dense_detector(step=2, levels=1), max_corners, grid[0], grid[1]) self.detect_edges_ = lambda im: fast_proposals(im, threshold) except: raise ImportError('fast_proposals (pybot_vision) is not available')
def i2t_feature_detector(detector, image): featureDetector = cv2.FeatureDetector_create(detector) gridAdaptedDetector = cv2.GridAdaptedFeatureDetector(featureDetector, 200) descriptorExtractor = cv2.DescriptorExtractor_create(detector) start_time = time.time() feature_points = gridAdaptedDetector.detect(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)) (feature_points, descriptors) = descriptorExtractor.compute(image, feature_points) end_time = time.time() print detector, ": Number of Feature Points is", len(feature_points), "[", end_time - start_time, "]" return (feature_points, descriptors)
def create_map(file_name): """ Creates a feature map by extracting features from the map image and sorting them intro grids. You do not need to edit this method. :param file_name: the image of the map :return: the map_grid_des and map_grid_kp, two dimensional lists of the kp and des from the map which are grouped into cells """ # read image and extract features image = cv2.imread(file_name) # the edgeThreshold and patchSize can be tuned if the gap between cell is too large detector = cv2.ORB(nfeatures=MAP_FEATURES, scoreType=cv2.ORB_FAST_SCORE) max_total_keypoints = 500 * ORB_GRID_SIZE_X * ORB_GRID_SIZE_Y detector_grid = cv2.GridAdaptedFeatureDetector( detector, maxTotalKeypoints=max_total_keypoints, gridCols=ORB_GRID_SIZE_X, gridRows=ORB_GRID_SIZE_Y) kp = detector_grid.detect(image, None) kp, des = detector.compute(image, kp) # rearrange kp and des into grid grid_kp = [[[] for _ in range(MAP_GRID_SIZE_Y)] for _ in range(MAP_GRID_SIZE_X)] grid_des = [[[] for _ in range(MAP_GRID_SIZE_Y)] for _ in range(MAP_GRID_SIZE_X)] for i in range(len(kp)): x = int(kp[i].pt[0] / CELL_X) y = MAP_GRID_SIZE_Y - 1 - int(kp[i].pt[1] / CELL_Y) grid_kp[x][y].append(kp[i]) grid_des[x][y].append(des[i]) # group every 3 by 3 grid, so we can access each center of grid and its 8 neighbors easily map_grid_kp = [[[] for _ in range(MAP_GRID_SIZE_Y)] for _ in range(MAP_GRID_SIZE_X)] map_grid_des = [[[] for _ in range(MAP_GRID_SIZE_Y)] for _ in range(MAP_GRID_SIZE_X)] for i in range(MAP_GRID_SIZE_X): for j in range(MAP_GRID_SIZE_Y): for k in range(-1, 2): for l in range(-1, 2): x = i + k y = j + l if 0 <= x < MAP_GRID_SIZE_X and 0 <= y < MAP_GRID_SIZE_Y: map_grid_kp[i][j].extend(grid_kp[x][y]) map_grid_des[i][j].extend(grid_des[x][y]) map_grid_des[i][j] = np.array(map_grid_des[i][j]) return map_grid_kp, map_grid_des
def create_map(file_name): """ create a feature map, extract features from each bigger cell. :param file_name: the image of map :return: a list of center of bigger cells (kp and des), each bigger cell is a 3 by 3 grid (9 cells). """ # read image and extract features image = cv2.imread(file_name) # the edgeThreshold and patchSize can be tuned if the gap between cell is too large detector = cv2.ORB(nfeatures=500, scoreType=cv2.ORB_FAST_SCORE, scaleFactor=1.4) maxTotalKeypoints = 500 * ORB_GRID_SIZE_X * ORB_GRID_SIZE_Y detector_grid = cv2.GridAdaptedFeatureDetector( detector, maxTotalKeypoints=maxTotalKeypoints, gridCols=ORB_GRID_SIZE_X, gridRows=ORB_GRID_SIZE_Y) kp = detector_grid.detect(image, None) kp, des = detector.compute(image, kp) # rearrange kp and des into grid grid_kp = [[[] for y in range(MAP_GRID_SIZE_Y)] for x in range(MAP_GRID_SIZE_X)] grid_des = [[[] for y in range(MAP_GRID_SIZE_Y)] for x in range(MAP_GRID_SIZE_X)] for i in range(len(kp)): x = int(kp[i].pt[0] / CELL_X) y = MAP_GRID_SIZE_Y - 1 - int(kp[i].pt[1] / CELL_Y) grid_kp[x][y].append(kp[i]) grid_des[x][y].append(des[i]) # group every 3 by 3 grid, so we can access each center of grid and its 8 neighbors easily map_grid_kp = [[[] for y in range(MAP_GRID_SIZE_Y)] for x in range(MAP_GRID_SIZE_X)] map_grid_des = [[[] for y in range(MAP_GRID_SIZE_Y)] for x in range(MAP_GRID_SIZE_X)] for i in range(MAP_GRID_SIZE_X): for j in range(MAP_GRID_SIZE_Y): for k in range(-1, 2): for l in range(-1, 2): x = i + k y = j + l if 0 <= x < MAP_GRID_SIZE_X and 0 <= y < MAP_GRID_SIZE_Y: map_grid_kp[i][j].extend(grid_kp[x][y]) map_grid_des[i][j].extend(grid_des[x][y]) map_grid_des[i][j] = np.array(map_grid_des[i][j]) return map_grid_kp, map_grid_des
def sift_points(img, num_det=500): img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) try: sift = cv2.FeatureDetector_create('SIFT') detector = cv2.GridAdaptedFeatureDetector(sift, num_det) # max number of features fs = detector.detect(img2) points = [] for f in fs: #reference code sample at # http://shambool.com/2012/04/15/sift-and-all-the-other-feature-detectors-in-python/ points.append( {'position': f.pt, 'radius': f.size/2, 'angle': f.angle} ) out_dict = {"name": "sift_points", "value" : points} except: out_dict = {} raise return out_dict
feature_homography.py [<video source>] Keys ---- SPACE - set reference frame ESC - exit ''' import numpy as np import cv2 import video from common import draw_str, clock import sys detector = cv2.FastFeatureDetector(16, True) detector = cv2.GridAdaptedFeatureDetector(detector) extractor = cv2.DescriptorExtractor_create('ORB') FLANN_INDEX_KDTREE = 1 FLANN_INDEX_LSH = 6 flann_params = dict( algorithm=FLANN_INDEX_LSH, table_number=6, # 12 key_size=12, # 20 multi_probe_level=1) #2 matcher = cv2.FlannBasedMatcher(flann_params, {}) # bug : need to pass empty dict (#1329) green, red = (0, 255, 0), (0, 0, 255) if __name__ == '__main__':
# img2 = cv2.resize(img2, (0, 0), fx=0.2, fy=0.2) # # Just estimateRigidTransform # start_time = time.time() # transform = cv2.estimateRigidTransform(img1, img2, False) # print("--- %s seconds ---" % (time.time() - start_time)) # print transform # ORB FLANN knnMatch find object detector = cv2.ORB(nfeatures=200, scoreType=cv2.ORB_FAST_SCORE) detector2 = cv2.ORB(nfeatures=2000, scoreType=cv2.ORB_FAST_SCORE, edgeThreshold=5, scaleFactor=1.2) detector3 = cv2.GridAdaptedFeatureDetector(detector2, maxTotalKeypoints=500000, gridCols=5, gridRows=4) index_params = dict( algorithm=6, table_number=6, # 12 key_size=12, # 20 multi_probe_level=1) # 2 search_params = dict(checks=50) # or pass empty dictionary flann = cv2.FlannBasedMatcher(index_params, search_params) kp2 = detector3.detect(img2, None) kp2, des2 = detector2.compute(img2, kp2) img2 = cv2.drawKeypoints(img2, kp2, color=(0, 255, 0), flags=0) print len(kp2), len(des2)