当前位置:首页 » 《随便一记》 » 正文

error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function ‘knnMatch‘> Overload resolution fail

6 人参与  2023年05月03日 18:25  分类 : 《随便一记》  评论

点击全文阅读


 各位大佬,我正在用Sobel、SIFT feature extraction、Color histogram去做图像分类检索。想输入一张图片去检索相类似的图像,出现了以下错误,请问该怎么解决呢?万分感谢

# Define a function to find similar images
def find_similar_images(image_path, k=5):
    # Load input image and compute descriptors
    img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    img = cv2.resize(img, (img_size, img_size))
    keypoints, descriptors = sift.detectAndCompute(img, None)

    # If no keypoints are found, return an empty list
    if descriptors is None:
        return []

    # Convert descriptors to np.float32 data type
    descriptors = descriptors.astype(np.float32)
    
    # Compute distances between input image descriptors and dataset descriptors
    matcher = cv2.FlannBasedMatcher()
    matches = matcher.knnMatch(descriptors, data, k=2)
    distances = [m[0].distance for m in matches]
    
    # Find indices of the most similar images
    indices = np.argsort(distances)[:k]
    similar_images = []
    for index in indices:
        category = labels[index]
        folder_path = os.path.join(path, category)
        img_names = os.listdir(folder_path)
        img_path = os.path.join(folder_path, img_names[index])
        similar_images.append(cv2.imread(img_path))
    
    return similar_images

# Input an image and get similar images
input_image_path = "bridge.jpg"
similar_images = find_similar_images(input_image_path)

# Display similar images
for image in similar_images:
    cv2.imshow("Similar Image", image)
    cv2.waitKey(1000)
    
cv2.destroyAllWindows()

error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'knnMatch'> Overload resolution failed:>  - trainDescriptors data type = 17 is not supported>  - Expected Ptr<cv::UMat> for argument 'trainDescriptors'>  - argument for DescriptorMatcher.knnMatch() given by name ('k') and position (2)>  - argument for DescriptorMatcher.knnMatch() given by name ('k') and position (2)

点击全文阅读


本文链接:http://zhangshiyu.com/post/60542.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1