【发布时间】:2014-03-30 12:42:11
【问题描述】:
如何对提取的 SIFT 描述符进行聚类。做聚类的目的是为了分类目的。
【问题讨论】:
标签: opencv image-processing classification sift
如何对提取的 SIFT 描述符进行聚类。做聚类的目的是为了分类目的。
【问题讨论】:
标签: opencv image-processing classification sift
方法:
首先为每个图像/对象计算 SIFT descriptor,然后将 push_back 该描述符计算为单个图像(我们称该图像为 Mat featuresUnclustered)。
之后,您的任务是将所有描述符聚集到一定数量的组/集群中(由您决定)。这将是您的词汇/字典的大小。
int dictionarySize=200;
最后是对它们进行聚类的步骤
//define Term Criteria
TermCriteria tc(CV_TERMCRIT_ITER,100,0.001);
//retries number
int retries=1;
//necessary flags
int flags=KMEANS_PP_CENTERS;
//Create the BoW (or BoF) trainer
BOWKMeansTrainer bowTrainer(dictionarySize,tc,retries,flags);
//cluster the feature vectors
Mat dictionary=bowTrainer.cluster(featuresUnclustered);
【讨论】:
为了聚类,将 N*128 维度(N 是每个图像的描述符数)转换为 M*128 维度的数组(所有图像的 M 个描述符数)。并对这些数据执行集群。
例如:
def dict2numpy(dict):
nkeys = len(dict)
array = zeros((nkeys * PRE_ALLOCATION_BUFFER, 128))
pivot = 0
for key in dict.keys():
value = dict[key]
nelements = value.shape[0]
while pivot + nelements > array.shape[0]:
padding = zeros_like(array)
array = vstack((array, padding))
array[pivot:pivot + nelements] = value
pivot += nelements
array = resize(array, (pivot, 128))
return array
all_features_array = dict2numpy(all_features)
nfeatures = all_features_array.shape[0]
nclusters = 100
codebook, distortion = vq.kmeans(all_features_array,
nclusters)
【讨论】:
通常kmeans用于得到k个中心,你可以将每张图像变成K的一个向量(每个维度代表该簇中有多少个patch)。
【讨论】: