gpt4 book ai didi

python - 将提取的 SURF 描述符和关键点存储在 *.npy 文件中

转载 作者:太空宇宙 更新时间:2023-11-03 21:22:18 25 4
gpt4 key购买 nike

我是 Python 和 opencv 的新手。我设法获取描述符并将关键点绘制到图像上,但我需要知道如何存储它以供将来比较。

最佳答案

您可以关注this link .我个人使用以下代码来加载和保存 SURF 描述符

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)))

[编辑]:添加更多代码和用法示例:

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)

关于python - 将提取的 SURF 描述符和关键点存储在 *.npy 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25680529/

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