Search code examples
matlabimage-processingcomputer-visionvlfeat

Bag-of-Words (BOW) in VLFeat


I'm building a project Images Classification with Bag-of-Visual-Words (BoVW) using VLFeat library. The BoVW pipeline includes:

  1. SIFT
  2. k-means
  3. Building histogram
  4. SVM classification

I can use vl_sift and vl_kmeans for (1) and (2), but I don't know how to build histogram features and use them in SVM.


Solution

  • given that you already have the "dictionary" from vl_kmeans:

    [centers] = vl_kmeans(data, numClusters); 
    

    In order to build histogram of image I, you need to get the 128-D descriptors of that image using vl_sift:

    [~,D] = vl_sift(I)  
    

    Each column of D is the descriptor of one interest point (or frame) in image I. Now you need to build the histogram of I based on D and the dictionary centers. The simplest way is using a for loop:

    H = zeros(1,numClusters);
    for i=1:size(D,2)
      [~, k] = min(vl_alldist(D(:,i), centers)) ;
      H(k) = H(k) + 1;
    end
    

    Now it is up to you to normalise the histogram H or not, before passing it to SVM. Note that there is possibly a faster way to build the histogram that does not need a loop; but I think my code (in Matlab) is clear enough to explain the algorithm.