Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store the extracted SURF descriptors and keypoints in *.npy file

Tags:

python

opencv

I'm new to Python and opencv. I manage to get the descriptors as well as draw the keypoints onto the image but I need to know how to store it for future comparison.

like image 823
CY91 Avatar asked Oct 20 '25 03:10

CY91


1 Answers

You may follow this link. I personally use the following code to load and save SURF descriptors

def read_features_from_file(filename):
    """ Read feature properties and return in matrix form. """
    if os.path.getsize(filename) <= 0:
        return np.array([]), np.array([])
    f = np.load(filename)
    if f.size == 0:
        return np.array([]), np.array([])
    f = np.atleast_2d(f)
    return f[:,:7], f[:,7:] # feature locations, descriptors

def write_features_to_file(filename, locs, desc):
    np.save(filename, np.hstack((locs,desc)))

[EDIT]: add more codes and usage example:

def pack_keypoint(keypoints, descriptors):
    kpts = np.array([[kp.pt[0], kp.pt[1], kp.size,
                  kp.angle, kp.response, kp.octave,
                  kp.class_id]
                 for kp in keypoints])
    desc = np.array(descriptors)
    return kpts, desc

def unpack_keypoint(array):
    try:
        kpts = array[:,:7]
        desc = array[:,7:]
        keypoints = [cv2.KeyPoint(x, y, _size, _angle, _response, int(_octave), int(_class_id))
                 for x, y, _size, _angle, _response, _octave, _class_id in list(kpts)]
        return keypoints, np.array(desc)
    except(IndexError):
        return np.array([]), np.array([])

def process_image(imagename, resultname):
    img = cv2.imread(imagename, 0)
    k = surf.detect(img, None)
    if len(k) > 0:
        k, des = surf.compute(img, k)
    else:
        des = []
    k, des = pack_keypoint(k, des) #
    write_features_to_file(resultname, k, des)
like image 114
skyuuka Avatar answered Oct 21 '25 16:10

skyuuka