def detectKp(self, img, mask): if self.equalnum == True: # Star detector star = cv2.StarDetector(16, 0, 10, 8, 5) best_kp = star.detect(img) if len(best_kp) >= self.numpatch: best_kp = random.sample(best_kp,self.numpatch) else: star = cv2.StarDetector(30, 0, 10, 8, 5) best_kp = star.detect(img) #remove keypoints from the border of the image to be able to use the binary descriptors border = 40 #size of the border height, width = img.shape[:2] n = len(best_kp) best_kp_no_border = [] for i in range(0,n): kp = best_kp[i] if kp.pt[0] < border or kp.pt[0] > (width - border) or kp.pt[1] < border or kp.pt[1] > (height - border): True else: best_kp_no_border.append(kp) best_kp = best_kp_no_border # draw the keypoints #img3 = cv2.drawKeypoints(img,best_kp_no_border, color=(255,0,0)) #cv2.imshow('image2',img3) #cv2.waitKey(0) #cv2.destroyAllWindows() return best_kp
def make_detector(self, dparams): detector = None if dparams['detector'] == 'SIFT': max_features = int(dparams['sift-max-features']) detector = cv2.SIFT(nfeatures=max_features) elif dparams['detector'] == 'SURF': threshold = float(dparams['surf-hessian-threshold']) nOctaves = int(dparams['surf-noctaves']) detector = cv2.SURF(hessianThreshold=threshold, nOctaves=nOctaves) elif dparams['detector'] == 'ORB': max_features = int(dparams['orb-max-features']) grid_size = int(dparams['grid-detect']) cells = grid_size * grid_size max_cell_features = int(max_features / cells) detector = cv2.ORB_create(max_cell_features) elif dparams['detector'] == 'Star': maxSize = int(dparams['star-max-size']) responseThreshold = int(dparams['star-response-threshold']) lineThresholdProjected = int( dparams['star-line-threshold-projected']) lineThresholdBinarized = int( dparams['star-line-threshold-binarized']) suppressNonmaxSize = int(dparams['star-suppress-nonmax-size']) detector = cv2.StarDetector(maxSize, responseThreshold, lineThresholdProjected, lineThresholdBinarized, suppressNonmaxSize) return detector
def updateDetector(self, args): maxSize = params['maxSize'] responseThreshold = cv2.getTrackbarPos("Response threshold", processed_win) lineThresholdProjected = cv2.getTrackbarPos("Projected line threshold", processed_win) lineThresholdBinarized = cv2.getTrackbarPos("Binarized line threshold", processed_win) self.detector = cv2.StarDetector(maxSize, responseThreshold, lineThresholdProjected, lineThresholdBinarized)
def brief_descriptor_generator(data, nfeatures): """ :param data: numpy array grayscale image for getting histo or local descriptors for an images :param feature_point_quantity: MAX feature point quantity :return: keypoint_list, descriptor_list """ # nfeatures can not be controlled in STAR # Initiate STAR detector star = cv2.StarDetector() # Initiate BRIEF extractor brief = cv2.DescriptorExtractor_create("BRIEF") # find the keypoints with STAR kp = star.detect(data) # compute the descriptors with BRIEF kp, des = brief.compute(data, kp) return kp, des
import numpy as np import cv2 from matplotlib import pyplot as plt img = cv2.imread('demo.png', 0) # Initiate STAR detector #star = cv2.FeatureDetector_create("STAR") maxSize = 16 star = cv2.StarDetector() #for opencv3 #star = cv2.xfeatures2D.StarDetector_create() #star.detect(img) # Initiate BRIEF extractor brief = cv2.DescriptorExtractor_create("BRIEF") # find the keypoints with STAR kp = star.detect(img) #kp = star.detect(img,None) # compute the descriptors with BRIEF kp, des = brief.compute(img, kp) print brief.getInt('bytes') print des.shape img2 = cv2.drawKeypoints(img, kp, None, color=(0, 255, 0), flags=0) plt.imshow(img2), plt.show() cv2.imwrite("demo_brief" + '.png', img2)
def __init__(self, choice=1): """ This constructor determines OpenCV version being used and accordingly initialises feature detector and descriptors. User defined input choice determines type of detecor and descriptor used for stitching two frames together. Args: self (Stitcher) : Reference to the current Object choice (int) : Choice of detector and descriptor """ # Determine OpenCV version ver = int(cv2.__version__.split('.')[0]) if ver == 3: self.version3 = True else: self.version3 = False self.choice = choice # Initialize feature detectors and Descriptors based on choice parameter #Menu # Choice Feature detector Feature Descriptor Note # 1 SIFT SIFT - # 2 SURF SURF Without Upright # 3 SURF SURF With Upright # 4 STAR BRIEF - # 5 ORB ORB - if self.choice == 1: print 'Using SIFT...' if self.version3: self.sift = cv2.xfeatures2d.SIFT_create() else: self.detector = cv2.FeatureDetector_create("SIFT") self.extractor = cv2.DescriptorExtractor_create("SIFT") if self.choice == 2: print 'Using SURF without Upright...' if self.version3: self.surf = cv2.xfeatures2d.SURF_create(400) else: self.detector = cv2.FeatureDetector_create("SURF") self.extractor = cv2.DescriptorExtractor_create("SURF") if self.choice == 3: print 'Using SURF with Upright...' if self.version3: self.surf = cv2.xfeatures2d.SURF_create(400) self.surf.setUpright(True) else: self.detector = cv2.SURF(hessianThreshold=400, upright=1) self.extractor = cv2.DescriptorExtractor_create("SURF") if self.choice == 4: print 'Using STAR detector with BRIEF descriptor...' if self.version3: self.star = cv2.xfeatures2d.StarDetector_create() self.brief = cv2.xfeatures2d.BriefDescriptorExtractor_create() else: self.detector = cv2.StarDetector() self.extractor = cv2.DescriptorExtractor_create("BRIEF") if self.choice == 5: print 'Using ORB...' if self.version3: self.orb = cv2.ORB_create() else: self.orb = cv2.ORB() return
def get_feature_detector_descriptor_extractor( feature_detector_name=str(), descriptor_extractor_name=None, feature_detector_params=None, descriptor_extractor_params=None): """ :param feature_detector_name: :param descriptor_extractor_name: :param feature_detector_params: dict(nfeatures=1000) for ORB :param descriptor_extractor_params: :return: """ assert len(feature_detector_name) != 0 if feature_detector_params == None: feature_detector_params = dict() if descriptor_extractor_params == None: descriptor_extractor_params = dict() feature_detector_name = feature_detector_name.upper() normType = cv2.NORM_L2 if feature_detector_name == "ORB" or feature_detector_name == "BRIEF" or feature_detector_name == "BRISK": normType = cv2.NORM_HAMMING feature_detector = descriptor_extractor = None if feature_detector_name == "ORB": assert descriptor_extractor_name is None and len( descriptor_extractor_params) == 0 if imutils.is_cv2(): feature_detector = descriptor_extractor = cv2.ORB( **feature_detector_params) else: feature_detector = descriptor_extractor = cv2.ORB_create( **feature_detector_params) elif feature_detector_name == "BRIEF": assert descriptor_extractor_name is None and len( descriptor_extractor_params) == 0 if imutils.is_cv2(): feature_detector = cv2.StarDetector(**feature_detector_params) #descriptor_extractor = cv2.BriefDescriptorExtractor(**descriptor_extractor_params) # seems not working descriptor_extractor = cv2.DescriptorExtractor_create("BRIEF") else: feature_detector = cv2.xfeatures2d.StarDetector_create( **feature_detector_params) descriptor_extractor = cv2.xfeatures2d.BriefDescriptorExtractor_create( **descriptor_extractor_params) elif feature_detector_name == "BRISK": assert descriptor_extractor_name is None and len( descriptor_extractor_params) == 0 if imutils.is_cv2(): feature_detector = descriptor_extractor = cv2.BRISK( **feature_detector_params) else: feature_detector = descriptor_extractor = cv2.BRISK_create( **feature_detector_params) elif feature_detector_name == "SURF": assert descriptor_extractor_name is None and len( descriptor_extractor_params) == 0 if imutils.is_cv2(): feature_detector = descriptor_extractor = cv2.SURF( **feature_detector_params) else: feature_detector = descriptor_extractor = cv2.xfeatures2d.SURF_create( **feature_detector_params) elif feature_detector_params == "SIFT": assert descriptor_extractor_name is None and len( descriptor_extractor_params) == 0 if imutils.is_cv2(): feature_detector = descriptor_extractor = cv2.SIFT( **feature_detector_params) else: feature_detector = descriptor_extractor = cv2.xfeatures2d.SIFT_create( **feature_detector_params) else: print( "Seems we have not predefined the target feature_detector and descriptor_extractor" ) return feature_detector, descriptor_extractor, normType
def __init__(self, method, point, color): self.cv_wait = params['rate'] self.gamma = params['gamma']/100.0 cv2.createTrackbar("gamma", processed_win, params['gamma'], params['gamma_max'], self.updateGamma) cv2.createTrackbar("threshold 1", edge_win, params['thresh1'], params['thresh_max'], nothing) cv2.createTrackbar("threshold 2", edge_win, params['thresh2'], params['thresh_max'], nothing) cv2.createTrackbar("blur", processed_win, params['blur'], params['blur_max'], nothing) self.houghArgs = [params['rho'], params['theta'], params['threshold'], params['minLineLength'], params['maxLineGap']] if method == 'edge': self.detectFunction = self.edgeDetect elif method == 'color': cv2.createTrackbar("radius", processed_win, params['radius'], params['radius_max'], nothing) cv2.createTrackbar("open", processed_win, params['open'], params['open_max'], nothing) self.detectFunction = self.colorDetect self.color = color if self.color is not None: print "Got picked color:", self.color if point is not None: self.point = point elif point is None and color is None: raise Exception("Not enough information given to object_finder.py") elif method == 'star': self.color = None cv2.createTrackbar("radius", processed_win, params['radius'], params['radius_max'], nothing) cv2.createTrackbar("open", processed_win, params['open'], params['open_max'], nothing) cv2.createTrackbar("Response threshold", processed_win, params['response'], params['response_max'], self.updateDetector) cv2.createTrackbar("Projected line threshold", processed_win, params['projected'], params['projected_max'], self.updateDetector) cv2.createTrackbar("Binarized line threshold", processed_win, params['binarized'], params['binarized_max'], self.updateDetector) self.detector = cv2.StarDetector(params['maxSize'], params['response'], params['projected'], params['binarized']) self.detectFunction = self.starDetect elif method == 'watershed': self.detectFunction = self.watershedDetect cv2.createTrackbar("blur", processed_win, params['blur'], params['blur_max'], nothing) self.centroids = [] self.axes = [] #self.prev_axis = None self.prev_img = None self.processed = None self.canny = None