TypeError:query描述符不是一个numpy数组,也不是标量

时间:2018-06-07 05:34:03

标签: python opencv sift

我有以下代码:

import numpy as np
import cv2
import csv
from matplotlib import pyplot as plt

# load image
img1 = cv2.imread('/Users/alisa/Desktop/sift.jpg',0)  # queryImage

img2 = cv2.imread('/Users/alisa/Desktop/siftt.jpg',0) # trainImage

# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
kp2, des2 = sift.detectAndCompute(img2,None)

# read feature from file
with open('/Users/alisa/Desktop/q.csv') as csvDataFile:

    csvReader = csv.reader(csvDataFile)

    for gr_img1 in csvReader:

        des1=gr_img1

# Read keypoints from file
kp1 = []

lines = [line.strip() for line in open('/Users/alisa/Desktop/ali.txt')]

for line in lines:

    list = line.split(',')

    kps = cv2.KeyPoint(x=float(list[0]), y=float(list[1]), _size=float(list[2]), 

_angle=float(list[3]),

                               _response=float(list[4]), _octave=int(list[5]), 

_class_id=int(list[6]))

    kp1.append(kps)

# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

# Match descriptors.
matches = bf.match(des1,des2)

# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)

# Draw first 10 matches.
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10],None, flags=2)

plt.imshow(img3),plt.show()

但是,我得到以下内容:

  

TypeError:query Descriptors不是一个numpy数组,也不是标量。

有人可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

这是一个常见的错误。在您的代码中,kp1是一个列表而不是一个数组。

您可以查看:     类型(KP1)

但是drawMatches期待一个numpy数组。

你可以简单地说:     kp1 = np.asarray(kp1)