Opencv SIFT聚类将SIFT特征(128维向量)转换为词汇表
如何对提取的SIFT描述符进行聚类。进行聚类的目的是将其用于分类。通常用于获得k个中心,您可以将每个图像更改为k的向量(每个维度表示该聚类中有多少个面片)。方法:Opencv SIFT聚类将SIFT特征(128维向量)转换为词汇表,opencv,image-processing,classification,sift,Opencv,Image Processing,Classification,Sift,如何对提取的SIFT描述符进行聚类。进行聚类的目的是将其用于分类。通常用于获得k个中心,您可以将每个图像更改为k的向量(每个维度表示该聚类中有多少个面片)。方法: 首先,为每个图像/对象计算筛选描述符,然后将该描述符推回为单个图像(让我们称该图像为Mat Features SunClustered) 之后,您的任务是将所有描述符聚集到一定数量的组/簇中(由您决定)。这就是你的词汇量/字典的大小 int dictionarySize=200 最后是对它们进行聚类的步骤 //define Term
- 首先,为每个图像/对象计算
,然后筛选描述符
为单个图像(让我们称该图像为将该描述符推回
)Mat Features SunClustered
- 之后,您的任务是将所有描述符聚集到一定数量的组/簇中(由您决定)。这就是你的词汇量/字典的大小
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);
好的,如何表示集群的描述符,我知道描述符是128特性。因此,如何为一个对象/图像表示它们,您将得到一个描述符,它只不过是一个具有一行和128列的图像。因此,如果您使用50个图像,那么您将有50个这样的图像(即一行128列)。现在,您将所有这些行放在一个单独的图像中,该图像在上面的示例中显示为sunclustered。
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)