我正在尝试创建一个用于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'
如果不使用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)
我有
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
到目前为止我的代码是:
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
似乎问题在于对象&#34;描述符&#34;这是一个列表清单。我试图将其转换为np.array,但这种方法也不起作用。 我做错了什么或错过了什么?
答案 0 :(得分:2)
好的,显然问题是通过改变代码而不是我所做的列表理解来解决的:
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)
与cv2 kmeans函数一起使用。
答案 1 :(得分:0)
对于python 3,应为:
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)) # (notice the // here)
desc = np.float32(desc)