各位大佬,我正在用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)