Ejemplo n.º 1
0
def feature_matching(images,base_dir):
    for j in range(len(images)):
        filenames = images[j][50]
        featurename1 = base_dir+ '/features/' + str(j) + '_' + filenames.split('/')[-1] 
        
        p1, f1, c1= load_features(config,featurename1+'.npz')
        index1 = cv2.flann_Index()# if context.OPENCV3 else cv2.flann_Index()
        index1.load(f1,featurename1+'.flann')
        im1_matches = {}

        for match in range(len(images)):
            if match == j:
                continue
            image_name = str(match) + '_' + filenames.split('/')[-1]
            featurename2 = base_dir+ '/features/' + str(match) + '_' + filenames.split('/')[-1] 
            p2, f2, c2= load_features(config,featurename2+'.npz')
            index2 = cv2.flann_Index()# if context.OPENCV3 else cv2.flann_Index()
            index2.load(f2,featurename2+'.flann')
            matches = matching.match_symmetric(f1, index1, f2, index2, config)
            rmatches = matching.robust_match_fundamental(p1, p2, matches,config)
            im1_matches[image_name] = rmatches

        matches_path = base_dir+ '/matches/'
        io.mkdir_p(matches_path)
        with gzip.open(os.path.join(matches_path, '{}_matches.pkl.gz'.format(str(j) + '_' + filenames.split('/')[-1] )), 'wb') as fout:
            pickle.dump(im1_matches, fout)   
Ejemplo n.º 2
0
def findKeyPoints(img, template, distance=200):
    """ find key points in image """
    
    # SIFT
    FEATURE_DETECTOR = "SIFT" #"SURF" # "SIFT"
    detector = cv2.FeatureDetector_create(FEATURE_DETECTOR)
    descriptor = cv2.DescriptorExtractor_create(FEATURE_DETECTOR)
    
    #print(dir(descriptor))
    #print descriptor.paramType()
    skp = detector.detect(img)
    skp, sd = descriptor.compute(img, skp)

    if sd == None:
            return None, None

    tkp = detector.detect(template)
    tkp, td = descriptor.compute(template, tkp)

    flann_params = dict(algorithm=1, trees=4)
    
    
        
    flann = cv2.flann_Index(sd, flann_params)
    idx, dist = flann.knnSearch(td, 1, params={})

    del flann


    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    skp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            skp_final.append(skp[i])

    flann = cv2.flann_Index(td, flann_params)
    idx, dist = flann.knnSearch(sd, 1, params={})
    del flann
    
    #print descriptor.getParams()

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    tkp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            tkp_final.append(tkp[i])

    return skp_final, tkp_final
Ejemplo n.º 3
0
def findKeyPoints(img, template, distance=200):
    """ find key points in image """

    # SIFT
    FEATURE_DETECTOR = "SIFT"  #"SURF" # "SIFT"
    detector = cv2.FeatureDetector_create(FEATURE_DETECTOR)
    descriptor = cv2.DescriptorExtractor_create(FEATURE_DETECTOR)

    #print(dir(descriptor))
    #print descriptor.paramType()
    skp = detector.detect(img)
    skp, sd = descriptor.compute(img, skp)

    if sd == None:
        return None, None

    tkp = detector.detect(template)
    tkp, td = descriptor.compute(template, tkp)

    flann_params = dict(algorithm=1, trees=4)

    flann = cv2.flann_Index(sd, flann_params)
    idx, dist = flann.knnSearch(td, 1, params={})

    del flann

    dist = dist[:, 0] / 2500.0
    dist = dist.reshape(-1, ).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    skp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            skp_final.append(skp[i])

    flann = cv2.flann_Index(td, flann_params)
    idx, dist = flann.knnSearch(sd, 1, params={})
    del flann

    #print descriptor.getParams()

    dist = dist[:, 0] / 2500.0
    dist = dist.reshape(-1, ).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    tkp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            tkp_final.append(tkp[i])

    return skp_final, tkp_final
Ejemplo n.º 4
0
def findKeyPoints(img, template, distance=20):
    detector = cv2.FeatureDetector_create("SIFT")
    descriptor = cv2.DescriptorExtractor_create("SIFT")

    skp = detector.detect(img)
    skp, sd = descriptor.compute(img, skp)

    tkp = detector.detect(template)
    tkp, td = descriptor.compute(template, tkp)

    '''
    gray1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
    gray2 = cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)   
      
    #SIFT  
    detector = cv2.SIFT(1000) 
    skp, sd = detector.detectAndCompute(gray1,None)
    tkp, td = detector.detectAndCompute(gray2,None)
    '''
    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(sd, flann_params)
    idx, dist = flann.knnSearch(td, 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    skp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            skp_final.append(skp[i])

    flann = cv2.flann_Index(td, flann_params)
    idx, dist = flann.knnSearch(sd, 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    tkp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            tkp_final.append(tkp[i])

    return skp_final, tkp_final
Ejemplo n.º 5
0
def findKeyPoints(img, template, distance=20):
    detector = cv2.FeatureDetector_create("SIFT")
    descriptor = cv2.DescriptorExtractor_create("SIFT")

    skp = detector.detect(img)
    skp, sd = descriptor.compute(img, skp)

    tkp = detector.detect(template)
    tkp, td = descriptor.compute(template, tkp)
    '''
    gray1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
    gray2 = cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)   
      
    #SIFT  
    detector = cv2.SIFT(1000) 
    skp, sd = detector.detectAndCompute(gray1,None)
    tkp, td = detector.detectAndCompute(gray2,None)
    '''
    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(sd, flann_params)
    idx, dist = flann.knnSearch(td, 1, params={})
    del flann

    dist = dist[:, 0] / 2500.0
    dist = dist.reshape(-1, ).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    skp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            skp_final.append(skp[i])

    flann = cv2.flann_Index(td, flann_params)
    idx, dist = flann.knnSearch(sd, 1, params={})
    del flann

    dist = dist[:, 0] / 2500.0
    dist = dist.reshape(-1, ).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    tkp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            tkp_final.append(tkp[i])

    return skp_final, tkp_final
Ejemplo n.º 6
0
def findKeyPoints(img, template, maxdist=200):
    import cv2
    import numpy as np
    import itertools
    import sys

    detector = cv2.FeatureDetector_create("FAST")
    descriptor = cv2.DescriptorExtractor_create("SIFT")

    skp = detector.detect(img)
    skp, sd = descriptor.compute(img, skp)

    tkp = detector.detect(template)
    tkp, td = descriptor.compute(template, tkp)

    flann_params = dict(algorithm=1, trees=4)
    
    flann = cv2.flann_Index(sd, flann_params)
    idx, dist = flann.knnSearch(td, 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    skp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < maxdist:
            skp_final.append(skp[i])

    flann = cv2.flann_Index(td, flann_params)
    idx, dist = flann.knnSearch(sd, 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    tkp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < maxdist:
            tkp_final.append(tkp[i])

    return skp_final, tkp_final
Ejemplo n.º 7
0
def match_flann(desc1, desc2, r_threshold = 0.6):
    flann = cv2.flann_Index(desc2, flann_params)
    idx2, dist = flann.knnSearch(desc1, 2, params = {}) # bug: need to provide empty dict
    mask = dist[:,0] / dist[:,1] < r_threshold
    idx1 = np.arange(len(desc1))
    pairs = np.int32( zip(idx1, idx2[:,0]) )
    return pairs[mask]
Ejemplo n.º 8
0
def match_flann(desc1, desc2, r_threshold = 0.6):
    flann = cv2.flann_Index(desc2, flann_params)
    idx2, dist = flann.knnSearch(desc1, 2, params = {}) # bug: need to provide empty dict
    mask = dist[:,0] / dist[:,1] < r_threshold
    idx1 = np.arange(len(desc1))
    pairs = np.int32( zip(idx1, idx2[:,0]) )
    return pairs[mask]
Ejemplo n.º 9
0
def match_lowe(feature1,feature2):
  FLANN_INDEX_KDTREE = 1
  flann_params = dict(algorithm=FLANN_INDEX_KDTREE, tree=4)
  flann = cv2.flann_Index(feature1, flann_params)
  idx, dist = flann.knnSearch(feature2, 2, params={})
  good1 = dist[:, 0] < dist[:, 1] * 0.8 * 0.8
  matches_good1 = zip(idx[good1, 0], good1.nonzero()[0])
  #(a, b) = np.array(matches_good, dtype=int)
  flann = cv2.flann_Index(feature2, flann_params)
  idx, dist = flann.knnSearch(feature1, 2, params={})
  good2 = dist[:, 0] < dist[:, 1] * 0.8 * 0.8
  matches_good2 = zip(idx[good2, 0], good2.nonzero()[0])
  matches1 = [(a, b) for a, b in np.array(matches_good1, dtype=int)]
  matches2 = [(b, a) for a, b in np.array(matches_good2, dtype=int)]
  matches = set(matches1).intersection(matches2)
  return np.array(list(matches),dtype=int)
Ejemplo n.º 10
0
def match_flann(desc1, desc2, r_threshold=0.5):
    'Finds strong corresponding features in the two given vectors.'
    ## Adapted from <http://stackoverflow.com/a/8311498/72470>.

    if len(desc1) == 0 or len(desc2) == 0:
        print "No features passed into match_flann"
        return []

    ## Build a kd-tree from the second feature vector.
    FLANN_INDEX_KDTREE = 1  # bug: flann enums are missing
    flann = cv2.flann_Index(desc2, {
        'algorithm': FLANN_INDEX_KDTREE,
        'trees': 4
    })

    ## For each feature in desc1, find the two closest ones in desc2.
    (idx2, dist) = flann.knnSearch(desc1, 2, params={})  # bug: need empty {}

    ## Create a mask that indicates if the first-found item is sufficiently
    ## closer than the second-found, to check if the match is robust.
    mask = dist[:, 0] / dist[:, 1] < r_threshold

    ## Only return robust feature pairs.
    idx1 = np.arange(len(desc1))
    pairs = np.int32(zip(idx1, idx2[:, 0]))
    return [(i, j) for (i, j) in pairs[mask]]
Ejemplo n.º 11
0
    def build(self, image_names, cv_train_images):
        self.flann_index = cv2.flann_Index()
        self.train_images_names = []
        self.train_images_descriptors = None
        self.index_list = []
        for idx in range(len(image_names)):
            train_image = cv_train_images[idx]
            train_keypoints = self.feature_algorithm.detect(train_image, None)
            if not train_keypoints:
                print("Image: \"" + image_names[idx] +
                      "\" is too simple for the FLANN index.")
                continue
            (train_keypoints,
             train_descriptors) = self.feature_algorithm.compute(
                 train_image, train_keypoints)

            self.train_images_names.append(image_names[idx])

            if self.train_images_descriptors is not None:
                self.train_images_descriptors = np.concatenate(
                    (self.train_images_descriptors, train_descriptors))
            else:
                self.train_images_descriptors = train_descriptors

            descriptors_size = train_descriptors.shape[0]
            self.index_list.extend([idx] * descriptors_size)

        self.flann_index.build(self.train_images_descriptors,
                               params=self.flann_index_params)
Ejemplo n.º 12
0
 def load_feature_index(self, image, features):
     try:
         index = cv2.flann_Index()
     except:
         index = cv2.flann.Index()
     index.load(features, self.__feature_index_file(image))
     return index
Ejemplo n.º 13
0
def kmSIFTMAtches(descriptors_dst, descriptors_src, knn=5):
    """
    **Definition**: kmSIFTMAtches(descriptors_dst, descriptors_src, knn = 5)
    
    Computes the matches between two different sets of SIFT descriptors. Specifically, \
    it computes the matches from *descriptors_src* to *descriptors_dst* and find the *knn* \
    best matches.
    
    **Inputs**:
            * descriptors_dst: set of descriptors **to** match.
            * descriptors_src: set of descriptors **from** which the matching process is performed.
            * knn (optional): default *5*. *knn* denotes how many matches betweent the \
            two sets of descriptors will be computed.
        
    **Outputs**: 
            * idx: an *number_of_descriptors_src x knn* matrix. The i-th row contains \
            the *knn* nearest neigbors for descriptor i of descriptors_src set.
            * dist: an *number_of_descriptors_src x knn* matrix. The i-th row contains \
            the distances to the *knn* nearest neigbors for descriptor i of descriptors_src set.
    """
    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(descriptors_dst, flann_params)
    idx, dist = flann.knnSearch(descriptors_src, knn, params={})

    del flann

    return idx, dist
Ejemplo n.º 14
0
def grid_match(points, directions=4):
    """returns index to neighbors for each point in grid"""
    flann = cv2.flann_Index()
    flann.build(points, {'algorithm': 1, 'trees': 1})
    best_score, best_result = -float("inf"), None
    for m in range(len(points)):
        origin = points[m:m + 1, :]
        # neighbors[m, i] = n means that neighbor of points[m] in directions[i] is points[n]
        # last row in the result array represents a "no match" point
        result = -np.ones((points.shape[0] + 1, directions), np.int32)
        score = 0
        for i, n in enumerate(
                flann.knnSearch(origin, directions + 1)[0].T[1:]):
            target = points[n]
            cv2.arrowedLine(img, (round(origin[0, 0]), round(origin[0, 1])),
                            (round(target[0, 0]), round(target[0, 1])),
                            (50, 50, 200))
            shifted = points + (target - origin)
            indices, distances = flann.knnSearch(shifted, 1)
            indices[distances > scale] = -1
            result[indices[:, 0], i] = np.arange(len(indices))
            score += sum(indices >= 0)
        score -= sum(
            sum(result[:, i] == result[:, j]) for i in range(1, 4)
            for j in range(i))
        if score > best_score:
            best_score, best_result = score, result
    return best_result[:-1]
def findKeyPointsDist(tkp, td, skp, sd, maxdist=200):
    detector = cv2.FeatureDetector_create("FAST")
    descriptor = cv2.DescriptorExtractor_create("SIFT")

    flann_params = dict(algorithm=1, trees=4)
    
    flann = cv2.flann_Index(sd, flann_params)
    idx, dist = flann.knnSearch(td, 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    skp_final = []
    skp_final_dist = []
    for i, dis in itertools.izip(idx, dist):
        if dis <= maxdist:
            skp_final.append(skp[i])
            skp_final_dist.append(dis)

    return skp_final, skp_final_dist
Ejemplo n.º 16
0
def match_flann(desc1, desc2, r_threshold = 0.6):
    FLANN_INDEX_KDTREE = 1 
    flann = cv2.flann_Index(desc2, dict(algorithm = FLANN_INDEX_KDTREE, trees = 4))
    idx2, dist = flann.knnSearch(desc1, 2, params = {}) # bug: need to provide empty dict
    mask = dist[:,0] / dist[:,1] < r_threshold
    idx1 = np.arange(len(desc1))
    pairs = np.int32( zip(idx1, idx2[:,0]) )
    return pairs[mask]
def findKeyPoints(img, template, distance=200):
    detector = cv2.FeatureDetector_create("SIFT")
    descriptor = cv2.DescriptorExtractor_create("SIFT")

    skp = detector.detect(img)
    skp, sd = descriptor.compute(img, skp)

    tkp = detector.detect(template)
    tkp, td = descriptor.compute(template, tkp)

    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(sd, flann_params)
    idx, dist = flann.knnSearch(td, 1, params={})
    #idx, dist = RandomForestClassifier(td,n_estimators=10,max_depth=10)
    del flann

    dist = dist[:, 0] / 2500.0
    dist = dist.reshape(-1, ).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    skp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            skp_final.append(skp[i])

    flann = cv2.flann_Index(td, flann_params)
    idx, dist = flann.knnSearch(sd, 1, params={})
    del flann

    dist = dist[:, 0] / 2500.0
    dist = dist.reshape(-1, ).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    tkp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            tkp_final.append(tkp[i])

    return skp_final, tkp_final
def findKeyPoints(img, template, distance=200):
    detector = cv2.FeatureDetector_create("SIFT")
    descriptor = cv2.DescriptorExtractor_create("SIFT")

    skp = detector.detect(img)
    skp, sd = descriptor.compute(img, skp)

    tkp = detector.detect(template)
    tkp, td = descriptor.compute(template, tkp)

    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(sd, flann_params)
    idx, dist = flann.knnSearch(td, 1, params={})
    #idx, dist = RandomForestClassifier(td,n_estimators=10,max_depth=10)
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    skp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            skp_final.append(skp[i])

    flann = cv2.flann_Index(td, flann_params)
    idx, dist = flann.knnSearch(sd, 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    tkp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            tkp_final.append(tkp[i])

    return skp_final, tkp_final
Ejemplo n.º 19
0
def findKeyPoints(im, ref, distance=200):
    detector = cv2.SIFT()
    descriptor = cv2.DescriptorExtractor_create("SIFT")

    im_kp = detector.detect(im)
    im_kp, im_d = descriptor.compute(im, im_kp)

    ref_kp = detector.detect(ref)
    ref_kp, ref_d = descriptor.compute(ref, ref_kp)

    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(im_d, flann_params)
    idx, dist = flann.knnSearch(ref_d, 1, params={})
    del flann

    dist = dist[:, 0] / 2500.0
    dist = dist.reshape(-1, ).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    im_kp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            im_kp_final.append(im_kp[i])

    flann = cv2.flann_Index(ref_d, flann_params)
    idx, dist = flann.knnSearch(im_d, 1, params={})
    del flann

    dist = dist[:, 0] / 2500.0
    dist = dist.reshape(-1, ).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    ref_kp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            ref_kp_final.append(ref_kp[i])

    return im_kp_final, ref_kp_final
Ejemplo n.º 20
0
    def __init__(self, desc, method):
        method_to_option = {"SURF":1, "SIFT":1,"FAST":1,"ORB":0}
        self._option  = method_to_option[method]
        if self._option == self.FLANN_INDEX_LININD:
            nd = desc.view(np.float32).copy()
            desc = nd

        options = [{'algorithm': self.FLANN_INDEX_LININD},
                   {'algorithm': self.FLANN_INDEX_KDTREE, 'trees': 4}]
        self._flann = cv2.flann_Index(desc, options[method_to_option[method]])
Ejemplo n.º 21
0
    def load(self, directory):
        with open(directory + "/matcher.bin", "rb") as infile:
            input_obj = pickle.load(infile)
            self.flann_index = cv2.flann_Index()
            self.train_images_names = input_obj["names"]
            self.train_images_descriptors = input_obj["descriptors"]
            self.index_list = input_obj["index_list"]

        self.flann_index.build(self.train_images_descriptors,
                               params=self.flann_index_params)
Ejemplo n.º 22
0
def match_flann(desc1, desc2, r_threshold = 0.06):
    FLANN_INDEX_KDTREE = 1
    flann = cv2.flann_Index(desc2, {'algorithm': FLANN_INDEX_KDTREE, 'trees': 4})

    (idx2, dist) = flann.knnSearch(desc1, 2, params={}) # bug: need empty {}

    mask = dist[:,0] / dist[:,1] < r_threshold

    idx1  = np.arange(len(desc1))
    pairs = np.int32(zip(idx1, idx2[:,0]))
    return pairs[mask]
Ejemplo n.º 23
0
 def createTargetFeatures(self,image,rect):
    #extract features using suf and train kmeans matcher, run this on every frame
    #kps = self.surf.detect(image)
    #self.FLANN = cv2.flann_Index(kps, self.FLANN_params)
    kp,des = self.findFeatures(image,USE_SIFT_FT)
    points, descs = [], []
    descs = np.uint8(descs)
    self.FlannMatcher.add([descs])
    self.FlannMatcher2 = cv2.flann_Index(des,self.FLANN_params)
    track = PlanarTarget(image = image, rect=rect, keypoints = kp, descrs=descs, data=None)
    self.toTrack.append(track)
Ejemplo n.º 24
0
def find_key_points(img, template):
    detector = cv2.FeatureDetector_create('SIFT')
    descriptor = cv2.DescriptorExtractor_create('SIFT')

    img_key_points = detector.detect(img)
    img_key_points, img_descriptors = descriptor.compute(img, img_key_points)
    template_key_points = detector.detect(template)
    template_key_points, template_descriptors = descriptor.compute(template, template_key_points)

    flann_params = {'trees': 4, 'algorithm': 1}
    flann = cv2.flann_Index(img_descriptors, flann_params)
    idx, dist = flann.knnSearch(template_descriptors, 1, params={})
    del flann
    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    skp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < SIFT_DISTANCE:
            skp_final.append(img_key_points[i])

    flann = cv2.flann_Index(template_descriptors, flann_params)
    idx, dist = flann.knnSearch(img_descriptors, 1, params={})
    del flann
    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    tkp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < SIFT_DISTANCE:
            tkp_final.append(template_key_points[i])

    return skp_final, tkp_final
Ejemplo n.º 25
0
def findKeyPointsEps(kp1, d1, kp2, d2, epsilon=1):
    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(d1, flann_params)
    idx, dist = flann.knnSearch(d2, 2, params={})
    kp1_result = []
    kp2_result = []
    for i in xrange(len(kp1)):
        if dist[i][0] / dist[i][1] < epsilon:
            kp1_result.append(kp1[idx[i][0]])
            kp2_result.append(kp2[i])

    return kp1_result, kp2_result
Ejemplo n.º 26
0
def main(dim, db_size, q_size, knn, FLANN_INDEX_ALGO):
    print("Dimension : {0}".format(dim))
    ####### Utilisation flann
    # - les matrices descripteurs ou requetes doivent etre du type 'np.float32'
    # - le vecteur requete doit etre correctement formate : qdesc.shape = (1, desc_dim)
    # -> reshape
    # distance (a,b) calculee par knnSearch = sum (ai-bi)^2
    ########

    #### Generer aleatoirement une matrice des descripteurs de la base
    # - randint(100) : distribution uniforme, nombres entiers entre 0 et 100
    # - size=(10,5) : 10 descripteurs de dimension 5
    mat_desc = np.array(np.random.randint(100, size=(db_size, dim)),
                        dtype=np.float32)
    #print("Database descriptors : \n{0}".format(mat_desc))

    #### Generer un vecteur ou une matrice de vecteurs requete
    qdesc = np.array(np.random.randint(100, size=(q_size, dim)),
                     dtype=np.float32)
    #print("Query descriptor : \n{0}".format(qdesc))

    ######## Database Descriptors indexing
    # FLANN parameters
    # Algorithms
    # 0 : FLANN_INDEX_LINEAR,
    # 1 : FLANN_INDEX_KDTREE,

    start = timer()
    index_params = dict(algorithm=FLANN_INDEX_ALGO)  # for linear search
    #index_params = dict(algorithm = FLANN_INDEX_ALGO, trees = 5) # for kdtree search

    ### OpenCV 3
    fl = cv2.flann_Index(mat_desc, index_params)

    end = timer()
    print("Indexing time: {0}".format(end - start))

    ######## Query search
    start = timer()
    #search_params = dict(checks=50)
    idx, dist = fl.knnSearch(qdesc, knn, params={})
    end = timer()
    print("Search time: {0}".format(end - start))

    #print idx.shape
    #print("indices \n{0}".format(idx))
    #print("distances \n{0}".format(dist))

    print("Distance to neareast neighbor = {0}".format(dist[0][0]))
    print("Distance to farthest neighbor = {0}".format(dist[0][db_size - 1]))
    print("Ratio 1-nn and 2-nn = {0}".format(dist[0][0] / dist[0][1]))
    print("Ratio 1-nn and N-nn = {0}".format(dist[0][0] /
                                             dist[0][db_size - 1]))
Ejemplo n.º 27
0
def match_template(imagename, templatename, threshold, cutoff):

    img = cv2.imread(imagename)
    template = cv2.imread(templatename)

    [kpi, di] = detect_keypoints(imagename, threshold)
    [kpt, dt] = detect_keypoints(templatename, threshold)

    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(np.asarray(di, np.float32), flann_params)
    idx, dist = flann.knnSearch(np.asarray(dt, np.float32), 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]

    kpi_cut = []
    for i, dis in itertools.izip(idx, dist):
        print ("distance: " + str(dis))
        if dis < cutoff:
            kpi_cut.append(kpi[i])
        else:
            break

    kpt_cut = []
    for i, dis in itertools.izip(indices, dist):
        print ("distance: " + str(dis))
        if dis < cutoff:
            kpt_cut.append(kpt[i])
        else:
            break

    h1, w1 = img.shape[:2]
    h2, w2 = template.shape[:2]
    nWidth = w1 + w2
    nHeight = max(h1, h2)
    hdif = (h1 - h2) / 2
    newimg = np.zeros((nHeight, nWidth, 3), np.uint8)
    newimg[hdif:hdif+h2, :w2] = template
    newimg[:h1, w2:w1+w2] = img

    for i in range(min(len(kpi), len(kpt))):
        pt_a = (int(kpt[i,1]), int(kpt[i,0] + hdif))
        pt_b = (int(kpi[i,1] + w2), int(kpi[i,0]))
        cv2.line(newimg, pt_a, pt_b, (255, 0, 0))

    cv2.imwrite('matches.jpg', newimg)
Ejemplo n.º 28
0
 def __matchUsingFlann(self, desc1, desc2, r_threshold=0.6):
     '''
     Internal flann descriptors matcher in order to find the best match.
     
     @param desc1, desc2: SURF features descriptors of currently processed object orientation and the test image.
     @param r_threshold: Tunnable threshold for kNN normalized distance inside the descriptors space.
     '''
     flann = cv2.flann_Index(desc2, self.flann_params)
     idx2, dist = flann.knnSearch(desc1, 2, params={}) # bug: need to provide empty dict
     mask = dist[:, 0] / dist[:, 1] < r_threshold
     idx1 = np.arange(len(desc1))
     pairs = np.int32(zip(idx1, idx2[:, 0]))
     return pairs[mask]
Ejemplo n.º 29
0
def findKeyPoints(img, template, distance=200):
    surf = cv2.SURF(400)
    skp, sd = surf.detectAndCompute(img,None)
    tkp, td = surf.detectAndCompute(template,None)

    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(sd, flann_params)
    idx, dist = flann.knnSearch(td, 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    skp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            skp_final.append(skp[i])

    flann = cv2.flann_Index(td, flann_params)
    idx, dist = flann.knnSearch(sd, 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    tkp_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            tkp_final.append(tkp[i])

    return skp_final, tkp_final
Ejemplo n.º 30
0
def match_template(imagename, templatename, threshold, cutoff):
	
    img = cv2.imread(imagename)
    template = cv2.imread(templatename)

    [kpi, di] = detect_keypoints(imagename, threshold)
    [kpt, dt] = detect_keypoints(templatename, threshold)

    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(np.asarray(di, np.float32), flann_params)
    idx, dist = flann.knnSearch(np.asarray(dt, np.float32), 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]

    kpi_cut = []
    for i, dis in itertools.izip(idx, dist):
    	if dis < cutoff:
    		kpi_cut.append(kpi[i])
    	else:
    		break

    kpt_cut = []
    for i, dis in itertools.izip(indices, dist):
    	if dis < cutoff:
    		kpt_cut.append(kpt[i])
    	else:
    		break

    h1, w1 = img.shape[:2]
    h2, w2 = template.shape[:2]
    nWidth = w1 + w2
    nHeight = max(h1, h2)
    hdif = (h1 - h2) / 2
    newimg = np.zeros((nHeight, nWidth, 3), np.uint8)
    newimg[hdif:hdif+h2, :w2] = template
    newimg[:h1, w2:w1+w2] = img

    for i in range(min(len(kpi), len(kpt))):
    	pt_a = (int(kpt[i,1]), int(kpt[i,0] + hdif))
    	pt_b = (int(kpi[i,1] + w2), int(kpi[i,0]))
    	cv2.line(newimg, pt_a, pt_b, (255, 0, 0))

    cv2.imwrite('matches.jpg', newimg)
Ejemplo n.º 31
0
def match_flann(desc1, desc2, key1, key2, max_dist = 0.05):
  "Nearest neighbors matching of sets of features."
  flann = cv2.flann_Index(desc2, flann_params)
  idx2, dist = flann.knnSearch(desc1, 5, params = {}) # bug: need to provide empty dict
  matches = [-1] * len(desc1)
  for id1, match2 in zip(range(len(desc1)), idx2):
    best = 100
    for i, m in enumerate(match2):
      if dist[id1][i] > max_dist: continue
      diff = abs(np.array(key1[id1].pt) - numpy.array(key2[m].pt))
      l2 = numpy.linalg.norm(diff)
      if l2 < best:
        best = l2 
        matches[id1] = m
        break
  return [(i,m) for i, m in enumerate(matches) if m != -1] 
Ejemplo n.º 32
0
def find_matches(desc, template_descriptors, current_img_descriptors, match_thresh):
    # print '\n*'
    # print desc
    # print len(template_descriptors)
    # print len(current_img_descriptors)
    # print '*\n'

    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(current_img_descriptors, flann_params)
    idx, dist = flann.knnSearch(template_descriptors, 2, params={})
    del flann
    matches = np.c_[np.arange(len(idx)), idx[:,0]]
    pass_filter = dist[:,0]*match_thresh < dist[:,1]
    matches = matches[pass_filter]

    return len(matches)
def match_flann(desc1, desc2, r_threshold = 0.12):
  'Finds strong corresponding features in the two given vectors.'
  # Build a kd-tree from the second feature vector.
  FLANN_INDEX_KDTREE = 1
  flann = cv2.flann_Index(desc2, {'algorithm': FLANN_INDEX_KDTREE, 'trees': 4})

  # For each feature in desc1, find the two closest ones in desc2.
  (idx2, dist) = flann.knnSearch(desc1, 2, params={})

  # Create a mask that indicates if the first-found item is sufficiently
  # closer than the second-found, to check if the match is robust.
  mask = dist[:,0] / dist[:,1] < r_threshold
  
  # Only return robust feature pairs.
  idx1  = np.arange(len(desc1))
  pairs = np.int32(zip(idx1, idx2[:,0]))
  return pairs[mask]
Ejemplo n.º 34
0
def getKP(img1, img2, threshold):
    detector = cv2.FeatureDetector_create("SIFT")
    descriptor = cv2.DescriptorExtractor_create("SIFT")
    kp1 = detector.detect(img1)
    kp1, d1 = descriptor.compute(img1, kp1)
    kp2 = detector.detect(img2)
    kp2, d2 = descriptor.compute(img2, kp2)
    flann_params = dict(algorithm=1, trees=4)
    flann = cv2.flann_Index(d1, flann_params)
    idx, dist = flann.knnSearch(d2, 2, params={})
    kp1_result = []
    kp2_result = []
    for i in xrange(len(dist)):
        if dist[i][0] / dist[i][1] < threshold:
            kp1_result.append(kp1[idx[i][0]])
            kp2_result.append(kp2[i])
    return kp1_result, kp2_result
Ejemplo n.º 35
0
def get_nearest_neighbors(desc1, desc2):
    """
    Parameters
    ----------
    desc1, desc2 : lists of descriptors
        Feature descriptors.
    
    Returns
    --------
    points : list of list of pairs 
        For each descriptor in desc1, the n-best matches as an index and distance pair.
    """    

    flann = cv2.flann_Index(desc2, flann_params)
    idx2, dist = flann.knnSearch(desc1, 5, params = {}) 
    return [zip(matches, dists)
            for matches, dists in izip(idx2, dist)]
Ejemplo n.º 36
0
def build_flann_index(features, config):
    FLANN_INDEX_LINEAR          = 0
    FLANN_INDEX_KDTREE          = 1
    FLANN_INDEX_KMEANS          = 2
    FLANN_INDEX_COMPOSITE       = 3
    FLANN_INDEX_KDTREE_SINGLE   = 4
    FLANN_INDEX_HIERARCHICAL    = 5
    FLANN_INDEX_LSH             = 6

    if features.dtype.type is np.float32:
        FLANN_INDEX_METHOD = FLANN_INDEX_KMEANS
    else:
        FLANN_INDEX_METHOD = FLANN_INDEX_LSH

    flann_params = dict(algorithm=FLANN_INDEX_METHOD,
                        branching=config.get('flann_branching', 16),
                        iterations=config.get('flann_iterations', 20))
    index = cv2.flann_Index(features, flann_params)
    return index
Ejemplo n.º 37
0
def build_flann_index(features, config):
    FLANN_INDEX_LINEAR = 0
    FLANN_INDEX_KDTREE = 1
    FLANN_INDEX_KMEANS = 2
    FLANN_INDEX_COMPOSITE = 3
    FLANN_INDEX_KDTREE_SINGLE = 4
    FLANN_INDEX_HIERARCHICAL = 5
    FLANN_INDEX_LSH = 6

    if features.dtype.type is np.float32:
        FLANN_INDEX_METHOD = FLANN_INDEX_KMEANS
    else:
        FLANN_INDEX_METHOD = FLANN_INDEX_LSH

    flann_params = dict(algorithm=FLANN_INDEX_METHOD,
                        branching=config.get('flann_branching', 16),
                        iterations=config.get('flann_iterations', 20))
    index = cv2.flann_Index(features, flann_params)
    return index
Ejemplo n.º 38
0
def search_index(retcfg):

    q_features = load_features(retcfg['path']['qfeature'])
    q_namelist = np.loadtxt(retcfg['path']['qlist'], dtype=dict(names=('qname', 'nfeat'), formats=('U100', np.int32)))

    assert q_features.shape[0] == np.sum(q_namelist['nfeat']), "Inconsistent number of features sum and size of" \
                                                               "query features array"

    norm = retcfg.get('feature', 'norm', fallback=None)

    db_features = load_features(retcfg['path']['dbfeature'])
    if norm:
        db_features = normalize(db_features, norm)

    fidx = cv2.flann_Index()
    ifile = get_index_path(retcfg['path']['outdir'], retcfg['DEFAULT']['expname'])
    fidx.load(db_features, ifile)

    outdir = retcfg['path']['outdir'] + "queryfiles/"
    safe_create_dir(outdir)

    search_type = retcfg['search']['search_type']
    knn = retcfg.getint('search', 'knn')
    rfactor = retcfg.getfloat('search', 'radius_factor')

    sidx = 0
    for qname, n in q_namelist:
        qfeat = q_features[sidx:sidx+n]

        if norm:
            qfeat = normalize(qfeat, norm)

        matchfpath = "{0:s}{1:s}.matches".format(outdir, qname)
        distfpath = "{0:s}{1:s}.dist".format(outdir, qname)

        votes, dists, _ = flann_search(qfeat, fidx, stype=search_type, k=knn, f=rfactor, flib="cv")

        print(qname, "-> ", sidx, ":", sidx+n)
        print(votes.shape)
        print(dists.shape, end="\n---\n")
        np.save(matchfpath + ".npy", votes)
        np.save(distfpath + ".npy", dists)
        sidx += n
Ejemplo n.º 39
0
def match_flann(desc1, desc2, r_threshold = 0.06):
  'Finds strong corresponding features in the two given vectors.'
  ## Adapted from <http://stackoverflow.com/a/8311498/72470>.

  ## Build a kd-tree from the second feature vector.
  FLANN_INDEX_KDTREE = 1  # bug: flann enums are missing
  flann = cv2.flann_Index(desc2, {'algorithm': FLANN_INDEX_KDTREE, 'trees': 4})

  ## For each feature in desc1, find the two closest ones in desc2.
  (idx2, dist) = flann.knnSearch(desc1, 2, params={}) # bug: need empty {}

  ## Create a mask that indicates if the first-found item is sufficiently
  ## closer than the second-found, to check if the match is robust.
  mask = dist[:,0] / dist[:,1] < r_threshold
  
  ## Only return robust feature pairs.
  idx1  = np.arange(len(desc1))
  pairs = np.int32(zip(idx1, idx2[:,0]))
  return pairs[mask]
Ejemplo n.º 40
0
    def __init__(self, config):
        '''
        参数
        config: 配置
        '''

        self.__config = config
        self.__hashFeatures = np.fromfile(config['hashFeatureFilename'],
                                          dtype=np.float32)
        self.__hashFeatures.shape = self.__hashFeatures.size // ImageDb._HASH_FEATURE_DIM, ImageDb._HASH_FEATURE_DIM
        self.__imageFeatures = np.fromfile(config['imageFeatureFilename'],
                                           dtype=np.float32)
        self.__imageFeatures.shape = self.__imageFeatures.size // ImageDb._IMGE_FEATURE_DIM, ImageDb._IMGE_FEATURE_DIM
        # self.__imageLabels = np.fromfile(config['imageLabelsFilename'],dtype=np.string_)
        imageLabels = np.loadtxt(config['labelsFilename'],
                                 dtype=np.string_).astype(str).tolist()
        self.__imageLabels = imageLabels if isinstance(
            imageLabels, list) else [imageLabels]
        self.__indexer = cv2.flann_Index(self.__hashFeatures,
                                         ImageDb._FLANN_INDEX_PARAM)
Ejemplo n.º 41
0
def match_flann(desc1, desc2, r_threshold=0.2):
    'Finds strong corresponding features in the two given vectors.'

    ## Build a kd-tree from the second feature vector.
    FLANN_INDEX_KDTREE = 1  # bug: flann enums are missing
    flann = cv2.flann_Index(desc2, {
        'algorithm': FLANN_INDEX_KDTREE,
        'trees': 4
    })

    ## For each feature in desc1, find the two closest ones in desc2.
    (idx2, dist) = flann.knnSearch(desc1, 2, params={})  # bug: need empty {}

    ## Create a mask that indicates if the first-found item is sufficiently
    ## closer than the second-found, to check if the match is robust.
    mask = dist[:, 0] / dist[:, 1] < r_threshold

    ## Only return robust feature pairs.
    idx1 = np.arange(len(desc1))
    pairs = np.int32(zip(idx1, idx2[:, 0]))
    return pairs[mask]
Ejemplo n.º 42
0
def flann_matcher(desc1, desc2, threshold=0.6, trees=4):
    """Returns a 2 by N ndarray of matching indices.

    Column 0 are indices of first set and col 1 are corresponding indices in
    second set. Uses the KDTree algorithm in cv2.flann_index.

    :PARAMETERS:
        *desc1* --- List of descriptors for first set
        *desc2* --- List of descriptors for second set
        **threshold** --- Lower value returns fewer but better matches
        **trees** --- The number of parallel kd-trees to use. Try [1 to 16].

    :RETURNS:
        2 by N ndarray, where N is the number of matches.

    """
    flann = cv2.flann_Index(desc2, dict(algorithm=1, trees=trees))
    idx2, dist = flann.knnSearch(desc1, 2, params={})
    mask = dist[:,0] / dist[:,1] < threshold
    idx1 = arange(len(desc1))
    pairs = int32( zip(idx1, idx2[:,0]) )
    return pairs[mask].astype(int).T
Ejemplo n.º 43
0
def get_sift_descriptors_and_matches(img1, img2):
	'''
	img1 is test image, img2 is template
	'''
	kp1, des1 = SIFT.detectAndCompute(img1, None)
	kp2, des2 = SIFT.detectAndCompute(img2, None)

	# FLANN parameters
	detector = cv2.FeatureDetector_create("SIFT")
	descriptor = cv2.DescriptorExtractor_create("SIFT")

	kps1 = detector.detect(img1)
	kps1, descr1 = descriptor.compute(img1, kps1)

	kps2 = detector.detect(img2)
	kps2, descr2 = descriptor.compute(img2, kps2)

	flann_params = dict(algorithm=1, trees=10)
	flann = cv2.flann_Index(descr1, flann_params)
	idx, dist = flann.knnSearch(descr2, 1, params={})

	return idx, dist, descr1, descr2
Ejemplo n.º 44
0
def flann_matcher(desc1, desc2, threshold=0.6, trees=4):
    """Returns a 2 by N ndarray of matching indices.

    Column 0 are indices of first set and col 1 are corresponding indices in
    second set. Uses the KDTree algorithm in cv2.flann_index.

    :PARAMETERS:
        *desc1* --- List of descriptors for first set
        *desc2* --- List of descriptors for second set
        **threshold** --- Lower value returns fewer but better matches
        **trees** --- The number of parallel kd-trees to use. Try [1 to 16].

    :RETURNS:
        2 by N ndarray, where N is the number of matches.

    """
    flann = cv2.flann_Index(desc2, dict(algorithm=1, trees=trees))
    idx2, dist = flann.knnSearch(desc1, 2, params={})
    mask = dist[:, 0] / dist[:, 1] < threshold
    idx1 = arange(len(desc1))
    pairs = int32(zip(idx1, idx2[:, 0]))
    return pairs[mask].astype(int).T
def orb(image):
    votacion = np.zeros((image.shape[0], image.shape[1]))
    flann = cv2.flann_Index(descs, index_params)
    imgPrueba = image
    kp, des = detector.detectAndCompute(imgPrueba, None)
    idx, dist = flann.knnSearch(des, knn=1)
    keypnts = []
    puntos = []

    for i in range(len(idx)):
        puntos.append(idx[i])

    x, y = idx.shape
    for i in range(x):
        ind = idx[i]
        for j in range(y):
            vector = indxKpV[ind[j]]
            print(vector)
    output = imgPrueba.copy()
    output = cv2.drawKeypoints(imgPrueba, points, output, (0, 255, 0),
                               cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    return output
def match_flann(desc1, desc2, r_threshold = 0.5):
  # 'Finds strong corresponding features in the two given vectors.'
  
  if len(desc1) == 0 or len(desc2) == 0:
    print "No features passed into match_flann"
    return []

  ## Build a kd-tree from the second feature vector.
  FLANN_INDEX_KDTREE = 1  # bug: flann enums are missing
  flann = cv2.flann_Index(desc2, {'algorithm': FLANN_INDEX_KDTREE, 'trees': 4})

  ## For each feature in desc1, find the two closest ones in desc2.
  (idx2, dist) = flann.knnSearch(desc1, 2, params={}) # bug: need empty {}

  ## Create a mask that indicates if the first-found item is sufficiently
  ## closer than the second-found, to check if the match is robust.
  mask = dist[:,0] / dist[:,1] < r_threshold
  
  ## Only return robust feature pairs.
  idx1  = np.arange(len(desc1))
  pairs = np.int32(zip(idx1, idx2[:,0]))
  return [(i,j) for (i,j) in pairs[mask]]
Ejemplo n.º 47
0
def flann_key_points(kp1, desc1, desc2, distance):
    flann_params = dict(algorithm=1, trees=4)
    flann = cv.flann_Index(desc1, flann_params)
    idx, dist = flann.knnSearch(desc2, 1, params={})
    del flann

    dist = dist[:,0]/2500.0
    dist = dist.reshape(-1,).tolist()
    idx = idx.reshape(-1).tolist()
    indices = range(len(dist))
    indices.sort(key=lambda i: dist[i])
    dist = [dist[i] for i in indices]
    idx = [idx[i] for i in indices]
    key_points_final = []
    description_final = []
    for i, dis in itertools.izip(idx, dist):
        if dis < distance:
            key_points_final.append(kp1[i])
            description_final.append(desc1[i])
        else:
            break;

    return key_points_final, description_final
Ejemplo n.º 48
0
    def _find_neighbours(self, descriptors0, descriptors1, flann0):
        # return a list of pairs (idx0, idx1) such that descriptors0[idx0] is
        # very likely to describe the same feature as descriptors1[idx1]
        # using the same params as the find_obj.cpp demo from opencv

        flann1 = None

        if flann0 is None:
            flann1 = cv2.flann_Index(descriptors1,
                                    {'algorithm': FLANN_INDEX_KDTREE,
                                    'trees': 4})
            indices, dists = flann1.knnSearch(descriptors0, 2, params={})
            needles_number = len(descriptors0)
            # we did a search of descriptors0 in descriptors1, and we got the
            # indices in descriptors1 where we can find the elements of
            # descriptors0, indices are for descriptors1
            # descriptors1[indices[i][0]] <-> descriptors0[i]
        else:
            print "searching using flann", flann0
            needles_number = len(descriptors1)
            indices, dists = flann0.knnSearch(descriptors1, 2, params={})
            # reverse case of above, indices are for descriptors0
            # descriptors0[indices[i][0]] <-> descriptors1[i]

        result = []
        result_dists = []

        for i, flann_idx, (small_dist, big_dist) in izip(xrange(needles_number),
                                                         indices,
                                                         dists):
            if small_dist < big_dist * 0.6:
                if flann0 is not None:
                    result.append((flann_idx[0], i))
                else:
                    result.append((i, flann_idx[0]))
                result_dists.append(small_dist)
        return result, result_dists, flann1
Ejemplo n.º 49
0
def flann_matcher(desc1, desc2, r_threshold=0.20):
    'Finds strong corresponding features in the two given vectors.'
    ## Adapted from http://stackoverflow.com/a/8311498/72470

    ## Build a kd-tree from the second feature vector.
    FLANN_INDEX_KDTREE = 1  # bug: flann enums are missing
    flann = cv2.flann_Index(desc2, {
        'algorithm': FLANN_INDEX_KDTREE,
        'trees': 4
    })

    ## For each feature in desc1, find the two closest ones in desc2.
    (idx2, dist) = flann.knnSearch(desc1, 2, params={})  # bug: need empty {}

    ## Create a mask that indicates if the first-found item is sufficiently
    ## closer than the second-found, to check if the match is robust.
    mask = dist[:, 0] / dist[:, 1] < r_threshold

    ## Only return robust feature pairs.
    idx1 = np.arange(len(desc1))
    pairs = np.int32(zip(idx1, idx2[:, 0]))
    return pairs[mask]  # apply r_threshold mask

    # pairs = [list(pair) for pair in zipped_pairs]
    # pairs.sort(key=lambda x: abs(dist[x[0]][0] - dist[x[1]][1]))  # sort by closest distances

    def bf_matcher(desc1, desc2, k=2):
        bf = cv2.BFMatcher(cv2.NORM_L2)

    matches = bf.knnMatch(desc1, desc2, k)
    # Apply ratio test
    good = []
    for m, n in matches:
        if m.distance < 0.75 * n.distance:
            good.append((m.queryIdx, m.trainIdx))
    return good
Ejemplo n.º 50
0
print(input_mat_1)
print(search_mat)
FLANN_INDEX_KDTREE = 1

#从本地加载模型
params = dict(algorithm=FLANN_INDEX_KDTREE, trees=1)


def read_yml():
    fs = cv2.FileStorage("tree.yml", cv2.FILE_STORAGE_READ)
    fn = fs.getNode("tree")
    print(fn.mat())


#建树
kdtree = cv2.flann_Index()
kdtree.build(input_mat, params)
kdtree.build(input_mat_1, params)
print("build tree success")
#检索
indices, dists = kdtree.knnSearch(search_mat, 2, params=-1)
print("search kdtree")
#输出检索结果
print(indices)
print(np.sqrt(dists))
#使用numpy下的欧式距离验证结果是否正确
print((np.linalg.norm(input_mat - search_mat, axis=1)))
#模型
f = cv2.FileStorage("tree.yml", cv2.FILE_STORAGE_WRITE)
f.write("tree", input_mat)
f.release()
def main(options, args):
    global test_bodypart
    global bodypart_knn_pos, bodypart_knn_neg, bodypart_trained_data_pos, bodypart_vote

    bodypart_trained_data_pos = SaveClass()
    bodypart_trained_data_pos = pickle.load(open(options.train_data_p, 'rb'))
    bodypart_trained_data_neg = SaveClass()
    bodypart_trained_data_neg = pickle.load(open(options.train_data_n, 'rb'))

    test_bodypart = bodypart_trained_data_neg.bodypart
    print "test_bodypart:", test_bodypart

    bodypart_knn_pos = cv2.flann_Index(bodypart_trained_data_pos.descriptors,
                                       dict(algorithm=1, trees=4))
    bodypart_knn_neg = cv2.flann_Index(bodypart_trained_data_neg.descriptors,
                                       dict(algorithm=1, trees=4))

    bodypart_vote = np.zeros(
        (2 * options.vote_patch_size + 1, 2 * options.vote_patch_size + 1, 1),
        np.float)

    for x in range(-options.vote_patch_size, options.vote_patch_size + 1):
        for y in range(-options.vote_patch_size, options.vote_patch_size + 1):
            bodypart_vote[y + options.vote_patch_size,
                          x + options.vote_patch_size] = 1.0 + np.exp(
                              -0.5 * (x * x + y * y) /
                              (np.square(options.vote_sigma))) / (
                                  options.vote_sigma * np.sqrt(2 * np.pi))

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(("", options.socket_port))
    sock.listen(1)
    conn, addr = sock.accept()
    conn.setblocking(1)
    print 'Connected by', addr
    while 1:

        data = conn.recv(unpacker_header.size)
        header = unpacker_header.unpack(data)
        packet = {}
        packet["version"] = header[0]
        assert packet["version"] == "01"
        packet["size"] = header[1]
        if (options.verbosity >= 1):
            print "packet version:", packet["version"]
            print "packet size:", packet["size"]

        data = conn.recv(unpacker_list_header.size)
        list_header = unpacker_list_header.unpack(data)
        packet["list"] = {}
        packet["list"]["version"] = list_header[0]
        assert packet["list"]["version"] == "00"
        packet["list"]["frame_index"] = list_header[1]
        packet["list"]["length"] = list_header[2]
        thresh = list_header[3]
        if (options.verbosity >= 1):
            print "list version:", packet["list"]["version"]
            print "list frame_index:", packet["list"]["frame_index"]
            print "list length:", packet["list"]["length"]

        ack_message = '{ "received" : "' + str(packet["list"]["frame_index"])
        frame_vote_max = -1
        bodypart_coords_est = {}
        for i in range(0, packet["list"]["length"]):
            data = conn.recv(unpacker_image_header.size)
            header_data = unpacker_image_header.unpack(data)
            image_header = {}
            image_header["version"] = header_data[0]
            assert image_header["version"] == "01"
            image_header["rows"] = header_data[1]
            image_header["cols"] = header_data[2]
            image_header["crop_x"] = header_data[3]
            image_header["crop_y"] = header_data[4]

            if (options.verbosity >= 1):
                print "image header version:", image_header["version"]
                print "image header num rows:", image_header["rows"]
                print "image header num cols:", image_header["cols"]
                print "image header origin-x-coord:", image_header["crop_x"]
                print "image header origin-y-coord:", image_header["crop_y"]

            image_buffer_size = image_header["rows"] * image_header["cols"]
            data = bytearray(image_buffer_size)
            view = memoryview(data)
            toread = image_buffer_size
            while toread:
                nbytes = conn.recv_into(view, toread)
                view = view[nbytes:]
                toread -= nbytes
            if (options.verbosity >= 1):
                print len(data)
            image_buffer = np.frombuffer(data, dtype='uint8')

            frame = image_buffer.reshape(
                (image_header["rows"], image_header["cols"]))

            bodypart_vote_map = np.zeros(
                (np.shape(frame)[0], np.shape(frame)[1], 1), np.float)
            if (options.display_level >= 2):
                display_voters = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)

            ack_message += " " + str(image_header["rows"]) + "x" + str(
                image_header["cols"])

            kp_frame, desc_frame = surf.detectAndCompute(frame, None)
            if desc_frame == None:
                print "skipped frame: ", packet["list"]["frame_index"]
                continue
            for h, desc in enumerate(desc_frame):
                desc = np.array(desc, np.float32).reshape((1, 128))
                r_pos, d_pos = bodypart_knn_pos.knnSearch(
                    desc, 1, params=dict(checks=8))
                r_neg, d_neg = bodypart_knn_neg.knnSearch(
                    desc, 1, params=dict(checks=8))
                relative_distance = d_pos - d_neg

                if (relative_distance <= thresh):
                    a = np.pi * kp_frame[h].angle / 180.0
                    R = np.array([[np.cos(a), -np.sin(a)],
                                  [np.sin(a), np.cos(a)]])
                    p = kp_frame[h].pt + np.dot(
                        R, bodypart_trained_data_pos.votes[r_pos])
                    x, y = p
                    if (not (x <= options.vote_patch_size or
                             x >= np.shape(frame)[1] - options.vote_patch_size
                             or y <= options.vote_patch_size or y >=
                             np.shape(frame)[0] - options.vote_patch_size)):
                        bodypart_vote_map[y - options.vote_patch_size:y +
                                          options.vote_patch_size + 1,
                                          x - options.vote_patch_size:x +
                                          options.vote_patch_size +
                                          1] += bodypart_vote
                        if (options.display_level >= 2):
                            cv2.circle(display_voters, (int(x), int(y)),
                                       4, (0, 0, 255),
                                       thickness=-1)

            if (options.display_level >= 2):
                display_voters = cv2.resize(display_voters, (0, 0),
                                            fx=0.5,
                                            fy=0.5)
                cv2.imshow("voters", display_voters)

            vote_max = np.amax(bodypart_vote_map)
            if (vote_max > options.vote_threshold
                    and vote_max > frame_vote_max):
                frame_vote_max = vote_max
                vote_max_loc = np.array(
                    np.where(bodypart_vote_map == vote_max))
                vote_max_loc = vote_max_loc[:, 0]
                bodypart_coords_est["conf"] = vote_max
                bodypart_coords_est["x"] = int(vote_max_loc[1]) + int(
                    image_header["crop_x"])
                bodypart_coords_est["y"] = int(vote_max_loc[0]) + int(
                    image_header["crop_y"])

            if (options.display_level >= 1):
                display_vote_map = np.array(
                    cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR).copy(), np.float)
                display_vote_map /= 255.0
                bodypart_vote_map /= np.amax(bodypart_vote_map)
                display_vote_map[:, :, 2] = bodypart_vote_map[:, :, 0]
                if ("x" in bodypart_coords_est):
                    cv2.circle(
                        display_vote_map,
                        (bodypart_coords_est["x"], bodypart_coords_est["y"]),
                        4, (0, 255, 255),
                        thickness=-1)
                display_vote_map = cv2.resize(display_vote_map, (0, 0),
                                              fx=0.5,
                                              fy=0.5)
                cv2.imshow("voters", display_vote_map)

        if ("x" in bodypart_coords_est):
            ack_message += '" , "detections" : [ { "frame_index" : ' + str(
                packet["list"]["frame_index"]
            ) + ' , "test_bodypart" : "' + test_bodypart + '" , "coord_x" : ' + str(
                bodypart_coords_est["x"]) + ' , "coord_y" : ' + str(
                    bodypart_coords_est["y"]
                ) + ' , "conf" : ' + "{:.2f}".format(
                    bodypart_coords_est["conf"]) + ' } ] }'
        else:
            ack_message += '" , "detections" : [ { "frame_index" : ' + str(
                packet["list"]["frame_index"]) + ' } ] }'

        if (options.display_level >= 1):
            cv2.waitKey(100)
            cv2.destroyAllWindows()

        if (options.verbosity >= 1):
            print "ack message:", ack_message
        header = ('01', len(ack_message))
        packed_ack_header = packer_ack_header.pack(*header)
        conn.sendall(packed_ack_header)
        conn.sendall(ack_message)

    conn.close()
Ejemplo n.º 52
0
def build_flann_index(f, index_file, config):
    flann_params = dict(algorithm=2,
                        branching=config['flann_branching'],
                        terations=config['flann_iterations'])
    index = cv2.flann_Index(f, flann_params)
    index.save(index_file)
Ejemplo n.º 53
0
 def load_feature_index(self, image, features):
     index = cv2.flann_Index()
     index.load(features, self.__feature_index_file(image))
     return index
Ejemplo n.º 54
0
 def load_feature_index(self, image, features):
     index = cv2.flann.Index() if context.OPENCV3 else cv2.flann_Index()
     index.load(features, self.__feature_index_file(image))
     return index
	#ANN!!!
	# next we need to flatten our 3D feature vector into 2D for ANN to work
	f_A_reshape = []
	f_B_reshape = []
	for i in xrange(5):
		f_hA,f_wA = feature_vectorA[i].shape[0:2]
		f_A_reshape.append(kernel5*np.array(np.reshape(feature_vectorA[i],[f_hA*f_wA,25]),dtype=np.float32))

		f_hB,f_wB = feature_vectorB[i].shape[0:2]
		f_B_reshape.append(kernel5*np.array(np.reshape(feature_vectorB[i],[f_hB*f_wB,25]),dtype=np.float32))

	# train dataset
	n = 0
	trainset = np.array(f_A_reshape[n],dtype=np.float32)
	params = dict(algorithm=1,trees=4)
	flnn = cv2.flann_Index(trainset,params)

	# now to perform the actual ANN
	hh,ww = pyramidB[n].shape
	ilist,jlist,B_prime_Y = ANNalgo(flnn,n,hh,ww,feature_vectorA_prime,f_B_reshape)

	stop_time = time.time()
	print "algo complete"
	print "Serial: %f" % (stop_time - start_time)

	start_time = time.time()

	# COHERENCE CODE 
	for i in xrange(2, heightB - 1, 1):
		for j in xrange(2, widthB - 1, 1):          
			#set coh distance = infinity
imageBasePaths = np.load(output_dir + args.db_name + "_imagesPaths.npy")
imageBaseIndex = np.load(output_dir + args.db_name + "_imagesIndex.npy")
end = timer()
print "load descriptors: " + str(end - start)

## Load database index (computed offline)
start = timer()
FLANN_INDEX_ALGO = 1
index_params = dict(algorithm=254,
                    filename=output_dir + args.db_name + "_flann_index" +
                    str(FLANN_INDEX_ALGO) + ".dat")

d = np.asarray(dataBaseDescriptors, np.float32)
print 'd type', type(d)
print 'd size', d.shape
fl = cv2.flann_Index(np.asarray(dataBaseDescriptors, np.float32), index_params)
end = timer()
print "load index: " + str(end - start)

## Search on the database index
start = timer()
knn = 50
#idx = np.zeros((1,5))
search_params = dict(checks=50)  # or pass empty dictionary
idx, dist = fl.knnSearch(np.asarray(qdesc, np.float32), knn, params={})
end = timer()
print "knn search: " + str(end - start)

print idx.shape
print type(imageBaseIndex)
print imageBaseIndex.shape
Ejemplo n.º 57
0
height, width = template.shape[:2]
template = cv2.resize(template,(width/3, height/3), interpolation = cv2.INTER_CUBIC)

#find keypoints
detector = cv2.FeatureDetector_create("SIFT")
descriptor = cv2.DescriptorExtractor_create("SIFT")

skp = detector.detect(img)#list of all the keypoints found on the image
skp, sd = descriptor.compute(img, skp)#sd is the descriptor for the image

tkp = detector.detect(template)
tkp, td = descriptor.compute(template, tkp)

flann_params = dict(algorithm=1, trees=4)
flann = cv2.flann_Index(sd, flann_params)
idx, dist = flann.knnSearch(td, 1, params={})
del flann

dist = dist[:,0]/2500.0
dist = dist.reshape(-1,).tolist()
idx = idx.reshape(-1).tolist()
indices = range(len(dist))
indices.sort(key=lambda i: dist[i])
dist = [dist[i] for i in indices]
idx = [idx[i] for i in indices]

skp_final = []
tkp_final = []
for i, dis in itertools.izip(idx, dist):
    if dis < dist: