def initialise(self, im_gray0, tl, br): #Initialise detector, descriptor, matcher self.detector = cv2.FeatureDetector_create(self.DETECTOR) self.descriptor = cv2.DescriptorExtractor_create(self.DESCRIPTOR) self.matcher = cv2.DescriptorMatcher_create(self.MATCHER) #Get initial keypoints in whole image keypoints_cv = self.detector.detect(im_gray0) #Remember keypoints that are in the rectangle as selected keypoints ind = util.in_rect(keypoints_cv, tl, br) selected_keypoints_cv = list(itertools.compress(keypoints_cv, ind)) selected_keypoints_cv, self.selected_features = self.descriptor.compute(im_gray0, selected_keypoints_cv) selected_keypoints = util.keypoints_cv_to_np(selected_keypoints_cv) num_selected_keypoints = len(selected_keypoints_cv) if num_selected_keypoints == 0: raise Exception('No keypoints found in selection') #Remember keypoints that are not in the rectangle as background keypoints background_keypoints_cv = list(itertools.compress(keypoints_cv, ~ind)) background_keypoints_cv, background_features = self.descriptor.compute(im_gray0, background_keypoints_cv) background_keypoints = util.keypoints_cv_to_np(background_keypoints_cv) #Assign each keypoint a class starting from 1, background is 0 self.selected_classes = array(range(num_selected_keypoints)) + 1 background_classes = zeros(len(background_keypoints_cv)) #Stack background features and selected features into database self.features_database = vstack((background_features, self.selected_features)) #Same for classes self.database_classes = hstack((background_classes, self.selected_classes)) #Get all distances between selected keypoints in squareform pdist = scipy.spatial.distance.pdist(selected_keypoints) self.squareform = scipy.spatial.distance.squareform(pdist) #Get all angles between selected keypoints angles = np.empty((num_selected_keypoints, num_selected_keypoints)) for k1,i1 in zip(selected_keypoints, range(num_selected_keypoints)): for k2,i2 in zip(selected_keypoints, range(num_selected_keypoints)): #Compute vector from k1 to k2 v = k2-k1 #Compute angle of this vector with respect to x axis angle = math.atan2(v[1],v[0]) #Store angle angles[i1,i2] = angle self.angles = angles #Find the center of selected keypoints center = np.mean(selected_keypoints, axis=0) #Remember the rectangle coordinates relative to the center self.center_to_tl = np.array(tl) - center self.center_to_tr = np.array([br[0],tl[1]]) - center self.center_to_br = np.array(br) - center self.center_to_bl = np.array([tl[0],br[1]]) - center #Calculate springs of each keypoint self.springs = selected_keypoints - center #Set start image for tracking self.im_prev = im_gray0 #Make keypoints 'active' keypoints self.active_keypoints = np.copy(selected_keypoints) #Attach class information to active keypoints self.active_keypoints = hstack((selected_keypoints,self.selected_classes[:,None])) #Remember number of initial keypoints self.num_initial_keypoints = len(selected_keypoints_cv)
def initialise(self, im_gray0, tl, br): # Initialise detector, descriptor, matcher self.detector = cv2.FeatureDetector_create(self.DETECTOR) self.descriptor = cv2.DescriptorExtractor_create(self.DESCRIPTOR) self.matcher = cv2.DescriptorMatcher_create(self.MATCHER) # Get initial keypoints in whole image keypoints_cv = self.detector.detect(im_gray0) # Remember keypoints that are in the rectangle as selected keypoints ind = util.in_rect(keypoints_cv, tl, br) selected_keypoints_cv = list(itertools.compress(keypoints_cv, ind)) selected_keypoints_cv, self.selected_features = self.descriptor.compute( im_gray0, selected_keypoints_cv) selected_keypoints = util.keypoints_cv_to_np(selected_keypoints_cv) num_selected_keypoints = len(selected_keypoints_cv) if num_selected_keypoints == 0: raise Exception('No keypoints found in selection') # Remember keypoints that are not in the rectangle as background keypoints background_keypoints_cv = list(itertools.compress(keypoints_cv, ~ind)) background_keypoints_cv, background_features = self.descriptor.compute( im_gray0, background_keypoints_cv) _ = util.keypoints_cv_to_np(background_keypoints_cv) # Assign each keypoint a class starting from 1, background is 0 self.selected_classes = array(range(num_selected_keypoints)) + 1 background_classes = zeros(len(background_keypoints_cv)) # Stack background features and selected features into database self.features_database = vstack( (background_features, self.selected_features)) # Same for classes self.database_classes = hstack( (background_classes, self.selected_classes)) # Get all distances between selected keypoints in squareform pdist = scipy.spatial.distance.pdist(selected_keypoints) self.squareform = scipy.spatial.distance.squareform(pdist) # Get all angles between selected keypoints angles = np.empty((num_selected_keypoints, num_selected_keypoints)) for k1, i1 in zip(selected_keypoints, range(num_selected_keypoints)): for k2, i2 in zip(selected_keypoints, range(num_selected_keypoints)): # Compute vector from k1 to k2 v = k2 - k1 # Compute angle of this vector with respect to x axis angle = math.atan2(v[1], v[0]) # Store angle angles[i1, i2] = angle self.angles = angles # Find the center of selected keypoints center = np.mean(selected_keypoints, axis=0) # Remember the rectangle coordinates relative to the center self.center_to_tl = np.array(tl) - center self.center_to_tr = np.array([br[0], tl[1]]) - center self.center_to_br = np.array(br) - center self.center_to_bl = np.array([tl[0], br[1]]) - center # Calculate springs of each keypoint self.springs = selected_keypoints - center # Set start image for tracking self.im_prev = im_gray0 # Make keypoints 'active' keypoints self.active_keypoints = np.copy(selected_keypoints) # Attach class information to active keypoints self.active_keypoints = hstack( (selected_keypoints, self.selected_classes[:, None])) # Remember number of initial keypoints self.num_initial_keypoints = len(selected_keypoints_cv)
def initialise(self, im_gray0, tl, tr, br, bl): # Initialise detector, descriptor, matcher self.detector = cv.BRISK_create() self.descriptor = cv.xfeatures2d.LATCH_create(bytes=64) self.matcher = cv.BFMatcher( cv.NORM_HAMMING, crossCheck=False) # set crossCheck to False to use knnMatch # Get initial keypoints in whole image keypoints_cv = self.detector.detect(im_gray0) # Remember keypoints that are in the rectangle as selected keypoints ind = util.in_square(keypoints_cv, tl, tr, br, bl) self.selected_keypoints_cv = list(itertools.compress( keypoints_cv, ind)) self.selected_keypoints_cv, self.selected_features = self.descriptor.compute( im_gray0, self.selected_keypoints_cv) selected_keypoints = util.keypoints_cv_to_np( self.selected_keypoints_cv) num_selected_keypoints = len(self.selected_keypoints_cv) if num_selected_keypoints == 0: raise Exception('No keypoints found in selection') # Remember keypoints that are not in the rectangle as background keypoints background_keypoints_cv = list(itertools.compress(keypoints_cv, ~ind)) background_keypoints_cv, background_features = self.descriptor.compute( im_gray0, background_keypoints_cv) _ = util.keypoints_cv_to_np(background_keypoints_cv) # Assign each keypoint a class starting from 1, background is 0 self.selected_classes = array(list(range(num_selected_keypoints))) + 1 background_classes = zeros(len(background_keypoints_cv)) # Stack background features and selected features into database self.features_database = vstack( (background_features, self.selected_features)) # Same for classes self.database_classes = hstack( (background_classes, self.selected_classes)) # Get all distances between selected keypoints in squareform pdist = scipy.spatial.distance.pdist(selected_keypoints) self.squareform = scipy.spatial.distance.squareform(pdist) # Get all angles between selected keypoints angles = np.empty((num_selected_keypoints, num_selected_keypoints)) for k1, i1 in zip(selected_keypoints, list(range(num_selected_keypoints))): for k2, i2 in zip(selected_keypoints, list(range(num_selected_keypoints))): # Compute vector from k1 to k2 v = k2 - k1 # Compute angle of this vector with respect to x axis angle = math.atan2(v[1], v[0]) # Store angle angles[i1, i2] = angle self.angles = angles # Find the center of selected keypoints center = np.mean(selected_keypoints, axis=0) # Remember the rectangle coordinates relative to the center self.center_to_tl = np.array(tl) - center self.center_to_tr = np.array(tr) - center self.center_to_br = np.array(br) - center self.center_to_bl = np.array(bl) - center # Calculate springs of each keypoint self.springs = selected_keypoints - center # Set start image for tracking self.im_prev = im_gray0 # Make keypoints 'active' keypoints self.active_keypoints = np.copy(selected_keypoints) # Attach class information to active keypoints self.active_keypoints = hstack( (selected_keypoints, self.selected_classes[:, None])) # Remember number of initial keypoints self.initial_keypoints = np.copy(self.active_keypoints) self.num_initial_keypoints = len(self.selected_keypoints_cv) self.matched_ratio = 0.0 self.tl = util.array_to_float_tuple(tl) self.tr = util.array_to_float_tuple(tr) self.br = util.array_to_float_tuple(br) self.bl = util.array_to_float_tuple(bl) self.is_init = True