Pickling cv2.KeyPoint causes PicklingError

后端 未结 3 1701
南笙
南笙 2020-12-02 23:35

I want to search surfs in all images in a given directory and save their keypoints and descriptors for future use. I decided to use pickle as shown below:

#!         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 00:12

    The problem is that you cannot dump cv2.KeyPoint to a pickle file. I had the same issue, and managed to work around it by essentially serializing and deserializing the keypoints myself before dumping them with Pickle.

    So represent every keypoint and its descriptor with a tuple:

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

    Append all these points to some list that you then dump with Pickle.

    Then when you want to retrieve the data again, load all the data with 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]
    

    Create a cv2.KeyPoint from this data using the above code, and you can then use these points to construct a list of features.

    I suspect there is a neater way to do this, but the above works fine (and fast) for me. You might have to play around with your data format a bit, as my features are stored in format-specific lists. I tried to present the above using my idea at its generic base. I hope that this may help you.

提交回复
热议问题