1

I would like to cache KeyPoint in a JSON file and then retrieve them later for use in a FlannBasedMatcher. Is there a way to convert the KeyPoint to something like an array of strings or floats that could be stored and then retreived from a JSON file? I think this should be ok for the descriptors since they just look like an array of ints.

COMPUTING KEYPOINTs

kp2, des2 = brisk.detectAndCompute(img2, None)

MATCHER

matches = flann.knnMatch(des1,des2,k=2)
Anters Bear
  • 1,816
  • 1
  • 15
  • 41

1 Answers1

2

You can save your KeyPoint to a JSON file directly in string type:

import json
def save_2_jason(arr):
        data = {}  
        cnt = 0
        for i in arr:
            data['KeyPoint_%d'%cnt] = []  
            data['KeyPoint_%d'%cnt].append({'x': i.pt[0]})
            data['KeyPoint_%d'%cnt].append({'y': i.pt[1]})
            data['KeyPoint_%d'%cnt].append({'size': i.size})
            cnt+=1
        with open('data.txt', 'w') as outfile:  
            json.dump(data, outfile)

Save to data.txt with json format:

(kpt, desc) = brisk.detectAndCompute(img, None)
save_2_jason(kpt)

Converting back to KeyPoint from a JSON file need change it to cv2.KeyPoint class:

import json
def read_from_jason():
        result = []    
        with open('data.txt') as json_file:  
            data = json.load(json_file)
            cnt = 0
            while(data.__contains__('KeyPoint_%d'%cnt)):
                pt = cv2.KeyPoint(x=data['KeyPoint_%d'%cnt][0]['x'],y=data['KeyPoint_%d'%cnt][1]['y'], _size=data['KeyPoint_%d'%cnt][2]['size'])
                result.append(pt)
                cnt+=1
        return result

Read from data.txt you save:

kpt_read_from_jason = read_from_jason()
Zhubei Federer
  • 1,274
  • 2
  • 10
  • 27
  • Thanks for the answer, however I get `TypeError: 'cv2.KeyPoint' object is not iterable` on `for j in i:` – Anters Bear May 23 '19 at 07:53
  • I have edit my post and change to save/load KeyPoint ~ – Zhubei Federer May 23 '19 at 08:34
  • thanks, I figured something similar out from your previous code. Having a new issue now :( https://stackoverflow.com/questions/56271253/issue-converting-keypoints-to-and-from-json-and-then-using-flannbasedmatcher – Anters Bear May 23 '19 at 08:39