如何使用Python 3在OpenCV 3上正确加载cv2.KeyPoint和描述符? [英] How load cv2.KeyPoint and Descriptors correctly on OpenCV 3 with Python 3?

查看:238
本文介绍了如何使用Python 3在OpenCV 3上正确加载cv2.KeyPoint和描述符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天,我不得不恢复与OpenCV 3和Python 2.7一起使用的旧项目.

The other day I had to resume an old project that I used with OpenCV 3 and Python 2.7.

在此代码上,我要加载cv2.KeyPoint:

On this code, to load cv2.KeyPoint i do:

    import numpy as np
    import cPickle
    import cv2
    keypoints_list = cPickle.loads(open(path).read())
    kp = []
    for point in keypoints_list:
        temp = 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])
        kp.append(temp)

对于负载描述符,我这样做:

For load descriptors i do:

    descriptors_list = cPickle.loads(open(path).read())
    descriptors = []
    for i in xrange(len(descriptors_list )):
        temp = descriptors_list[i] * 1
        descriptors.append(temp)
    descriptors = np.asarray(descriptors)        

在Python2.7上可以正常工作,但是,我尝试将代码修改为适用于Python 3,如下所示:

It's working without problem on Python2.7, but, I tried to adapt the code to Python 3 as follows:

    import numpy as np
    import pickle #or import _pickle as pickle
    import cv2

    keypoints_list = pickle.loads(open(path).read())
    kp = []
    for point in keypoints_list:
        temp = 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])
        kp.append(temp)

返回:

index = pickle.loads(open(path).read()) TypeError:需要一个类似字节的对象,而不是'str'

index = pickle.loads(open(path).read()) TypeError: a bytes-like object is required, not 'str'

我认为,好吧,我将字符串添加为字节.我尝试了以下操作:

I thought, well, I will add the string as bytes. I tried the follow:

    keypoints_list = pickle.loads(open(path).read().encode())
    kp = []
    for point in keypoints_list :
        temp = 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])
        kp.append(temp)

还有描述符...:

        descriptors_list = pickle.loads(open(path).read().encode())
        descriptors = []
        for i in range(len(descriptors_list)):
            temp = descriptors_list[i] * 1
            descriptors.append(temp)
        descriptors = np.asarray(descriptors)

这样,关键点将正确加载,但是带有描述符的pickle.loads将返回以下内容:

With this, Keypoints load correctly, but, pickle.loads with descriptors return this:

index = pickle.loads(open(path).read().encode())UnicodeDecodeError: 'ascii'编解码器无法解码位置2的字节0xfa:序数不在 范围(128)

index = pickle.loads(open(path).read().encode()) UnicodeDecodeError: 'ascii' codec can't decode byte 0xfa in position 2: ordinal not in range(128)

我也尝试过:

    with open(path, 'r', encoding="utf-8") as f:
        index = pickle.loads(f.read().encode())
    descriptors = []
    for i in range(len(index)):
        temp = index[i] * 1
        descriptors.append(temp)
    descriptors = np.asarray(descriptors)

和其他派生词...但总是返回相同的错误.

And other derivates... but always return same error.

我看到描述符的类型为cnumpy.core.multiarray,但我不知道如何解决此错误.

I see that descriptors are of type cnumpy.core.multiarray, but i don't know how to solve this error.

推荐答案

好...我使用代码:

        descriptors_list = np.fromfile(path)
        descriptors = []
        for i in range(len(descriptors_list)):
            temp = descriptors_list[i] * 1
            descriptors.append(temp)
        descriptors = np.asarray(descriptors)

这篇关于如何使用Python 3在OpenCV 3上正确加载cv2.KeyPoint和描述符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆