gpt4 book ai didi

python - Pickling cv2.KeyPoint 导致 PicklingError

转载 作者:IT老高 更新时间:2023-10-28 21:01:35 28 4
gpt4 key购买 nike

我想在给定目录中的所有图像中搜索冲浪,并保存它们的关键点和描述符以供将来使用。我决定使用pickle,如下所示:

#!/usr/bin/env python
import os
import pickle
import cv2

class Frame:
def __init__(self, filename):
surf = cv2.SURF(500, 4, 2, True)
self.filename = filename
self.keypoints, self.descriptors = surf.detect(cv2.imread(filename, cv2.CV_LOAD_IMAGE_GRAYSCALE), None, False)

if __name__ == '__main__':

Fdb = open('db.dat', 'wb')
base_path = "img/"
frame_base = []

for filename in os.listdir(base_path):
frame_base.append(Frame(base_path+filename))
print filename

pickle.dump(frame_base,Fdb,-1)

Fdb.close()

当我尝试执行时,出现以下错误:

File "src/pickle_test.py", line 23, in <module>
pickle.dump(frame_base,Fdb,-1)
...
pickle.PicklingError: Can't pickle <type 'cv2.KeyPoint'>: it's not the same object as cv2.KeyPoint

有谁知道,这是什么意思以及如何解决?我正在使用 Python 2.6 和 Opencv 2.3.1

非常感谢

最佳答案

问题是您不能将 cv2.KeyPoint 转储到 pickle 文件。我遇到了同样的问题,并设法通过自己序列化和反序列化关键点来解决它,然后再用 Pickle 倾倒它们。

所以用元组表示每个关键点及其描述符:

temp = (point.pt, point.size, point.angle, point.response, point.octave, 
point.class_id, desc)

将所有这些点附加到一些列表中,然后用 Pickle 转储。

然后当你想再次检索数据时,用 Pickle 加载所有数据:

temp_feature = cv2.KeyPoint(x=point[0][0],y=point[0][1],_size=point[1], _angle=point[2], 
_response=point[3], _octave=point[4], _class_id=point[5])
temp_descriptor = point[6]

使用上面的代码从这个数据中创建一个 cv2.KeyPoint,然后你可以使用这些点来构造一个特征列表。

我怀疑有一种更简洁的方法可以做到这一点,但上述方法对我来说效果很好(而且速度很快)。您可能需要稍微调整一下您的数据格式,因为我的功能存储在特定格式的列表中。我试图在其通用基础上使用我的想法来呈现上述内容。希望对您有所帮助。

关于python - Pickling cv2.KeyPoint 导致 PicklingError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10045363/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com