【问题标题】:Performing kmeans on a train set of images and sift descriptors在一组图像和筛选描述符上执行 kmeans
【发布时间】:2015-02-07 15:06:44
【问题描述】:

我正在尝试创建一个用于 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

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

【问题讨论】:

  • 你为什么使用 SciPy 的 kmeans 而不是 sklearn 的?只是好奇。
  • @MisterJT 为这个迟到的回复道歉。这是一项任务,如果我没记错的话,我们被介绍到那个库并且应该用它来解决我们的任务。

标签: python-2.7 opencv scipy k-means sift


【解决方案1】:

好的,显然问题是通过稍微更改代码而不是我所做的列表理解来解决的:

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 函数。

【讨论】:

    【解决方案2】:

    对于 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-07
      • 2018-03-25
      • 2020-08-29
      • 2014-03-10
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      相关资源
      最近更新 更多