这是我的代码:
sift=cv2.xfeatures2d.SIFT_create()
descriptors_unclustered=[]
dictionarysize=800
BOW=cv2.BOWKmeansTrainer(dictionarysize)
for p in training-paths :
kp,dsc=sift.detectAndCompute(image,None)
BOW.add(dsc)
dictionary=BOW.cluster()
bowdiction=cv2.BOWImgDescriptorExtractor(sift, cv2.BFMatcher(cv2.NORM_L2))
bowdiction.setvocabulary(dictionary)
我想保存此弓箭数据以便以后使用。我不想每次都等待这些计算,那么如何保存这些数据呢?
答案 0 :(得分:1)
将pickle用于此
将BOW保存到pickle:
import pickle
sift=cv2.xfeatures2d.SIFT_create()
descriptors_unclustered=[]
dictionarysize=800
BOW=cv2.BOWKmeansTrainer(dictionarysize)
for p in training-paths :
kp,dsc=sift.detectAndCompute(image,None)
BOW.add(dsc)
with open('bow_pickle.pickle', 'wb') as f:
pickle.dump(f)
从pickle返回数据:
import pickle
with open('bow_pickle.pickle', 'rb') as f:
BOW = pickle.load(f)
dictionary=BOW.cluster()
bowdiction=cv2.BOWImgDescriptorExtractor(sift,cv2.BFMatcher(cv2.NORM_L2))
bowdiction.setvocabulary(dictionary)
答案 1 :(得分:-1)
我知道这是旧的。但是当我来到这里并没有看到任何答案时,我尝试了一些方法并且这奏效了:
dictionary=BOW.cluster()
是最耗时的。所以你只需要保存字典。这只是一个 ndarray:
np.savetxt('bow_dict.txt', dictionary)
loaded_dictionary = np.loadtxt('bow_dict.txt')
然后继续
bowdiction=cv2.BOWImgDescriptorExtractor(sift,cv2.BFMatcher(cv2.NORM_L2))
bowdiction.setvocabulary(loaded_dictionary)