对一组图像和筛选描述符执行 kmeans

the*_*ggg 1 opencv scipy sift k-means python-2.7

我正在尝试创建一个用于 CBIR 任务的密码本。一切正常,直到我尝试执行 kmeans,然后我有

Traceback (most recent call last):
File "path", line 36, in <module>
scipy.cluster.vq.kmeans(descriptors, k_or_guess=500, iter=20, thresh=1e-05)
File "path", line 513, in kmeans
No = obs.shape[0]
AttributeError: 'list' object has no attribute 'shape'
Run Code Online (Sandbox Code Playgroud)

如果我使用 kmeans 代替 scipy 函数

cv2.kmeans(descriptors, K=500, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 1, 10), attempts=1, flags=cv2.KMEANS_RANDOM_CENTERS)
Run Code Online (Sandbox Code Playgroud)

我有

Traceback (most recent call last):
File "path", line 35, in <module>
cv2.kmeans(descriptors, K=500, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 1,  10), attempts=1, flags=cv2.KMEANS_RANDOM_CENTERS)
TypeError: data is not a numpy array, neither a scalar
Run Code Online (Sandbox Code Playgroud)

到目前为止我的代码是:

from scipy.cluster.vq import *
import numpy as np
import glob
import cv2


#CB

#creating a list of images 
images = []
for infile in glob.glob('path'):
    pic = cv2.imread(infile)
    images.append(pic)

np.random.shuffle(images)
my_set = images

#split set
train = my_set[:120]
test = my_set[120:]

#get train descriptors
descriptors = [cv2.SIFT().detectAndCompute(pic, None) for pic in train]


#kmeans
scipy.cluster.vq.kmeans(desc, k_or_guess=1000, iter=20, thresh=1e-05)

#then indexing 
#then implement retrieval
Run Code Online (Sandbox Code Playgroud)

问题似乎出在对象“描述符”上,它是一个列表列表。我尝试将其转换为 np.array,但这种方法效果不佳。我做错了什么或错过了什么?

the*_*ggg 6

好吧,显然这个问题是通过改变一点代码来解决的,而不是我所做的列表理解:

descriptors = np.array([])
for pic in train:
    kp, des = cv2.SIFT().detectAndCompute(pic, None)
    descriptors = np.append(descriptors, des)

desc = np.reshape(descriptors, (len(descriptors)/128, 128))
desc = np.float32(desc)
Run Code Online (Sandbox Code Playgroud)

它与 cv2 kmeans 函数一起使用。