我想在JSON文件中缓存KeyPoint
,然后将其检索以在FlannBasedMatcher
中使用。有没有办法将KeyPoint
转换为可以存储然后从JSON文件重新归还的字符串或浮子之类的东西?我认为对于描述符来说,这应该可以,因为它们看起来像一系列Ints。
计算关键点
kp2, des2 = brisk.detectAndCompute(img2, None)
Matcher
matches = flann.knnMatch(des1,des2,k=2)
您可以将关键点直接保存在字符串类型中:
中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)
保存在data.txt上,带有json格式:
(kpt, desc) = brisk.detectAndCompute(img, None)
save_2_jason(kpt)
从JSON文件转换回KePoint需要将其更改为CV2.Keypoint类:
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
从data.txt中读取您:
kpt_read_from_jason = read_from_jason()