Reputation: 1270
How to cluster the extracted SIFT descriptors. The aim of doing clustering is to use it for classification purpose.
Upvotes: 1
Views: 2656
Reputation: 1270
To cluster , convert N*128 dimension(N is the number of descriptor from each image) into a array of M*128 dimension (M number of descriptor from all images). and perform cluster on this data.
eg:
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)
Upvotes: 0
Reputation: 5649
Approach:
First of all compute the SIFT descriptor
for each image/object and then push_back
that descriptor into a single image (lets called that image Mat featuresUnclustered
).
After that your task is to cluster all the descriptor into some number of groups/clusters (which is decided by you). That will be the size of your vocabulary/dictionary.
int dictionarySize=200;
And then finally comes the step of clustering them
//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);
Upvotes: 2
Reputation: 2898
usually kmeans is applied to get k centers, you can change each image into a vector of the K (each dimension represent how many patch in that cluster).
Upvotes: -1