Assertion failed: _queryDescriptors.type() == trainDescType in func knnMatcImpl

问题错误:cv2.error:OpenCV(3.4.2) /io/opencv/modules/features2d/src/matchers.cpp:744:error:(-215:Assertion failed)  _queryDescriptors.type() == trainDescType  in function  knnMatchImpl

class KeypointData(object):
    def __init__(self, keypoints, descriptors):
        self.keypoints = keypoints
        self.descriptors = descriptors
        
    def save(self, file_name):
        data = []
        for keypoint, descriptor in zip(self.keypoints, self.descriptors):
            data.append([keypoint.pt
                , keypoint.size
                , keypoint.angle
                , keypoint.response
                , keypoint.octave
                , keypoint.class_id
                , descriptor])
    
        pickle.dump(data, open(file_name, "wb" ))
        
    @staticmethod
    def load(file_name, flg):
        data = pickle.load(open(file_name, "rb" ),encoding='bytes')
        
        keypoints = []
        descriptors = []
        
        for entry in data:
            point = entry[0]
            size = entry[1]
            angle = entry[2]
            response = entry[3]
            octave = entry[4]
            class_id = entry[5]
            
            keypoints.append(cv2.KeyPoint(x=point[0],y=point[1]
                , _size=size
                , _angle=angle
                , _response=response
                , _octave=octave
                , _class_id=class_id))
            
            descriptors.append(entry[6])
        
        if flg:    
            return KeypointData(keypoints, np.array(descriptors, np.uint8))
        else:
            return KeypointData(keypoints, np.array(descriptors, np.float32)) 

 

 

问题描述:把照片利用上述的方法把SURF和ORB特征先保存在本地,然后load一下进行本地调用,发现会报出上述错误。

解决方案:仔细研究发现,ORB提特征的descriptors是uint8类型的,而SURF提的特征是float32的,所以后面返回的时候加了一段函数,一个返回unit8,一个返回float32,这样就解决问题了!

你可能感兴趣的:(Assertion failed: _queryDescriptors.type() == trainDescType in func knnMatcImpl)