Bag of Visual Words

Bag of Visual Words (BoVW) applies text-retrieval ideas to images (Sivic & Zisserman, 2003): quantize local feature descriptors into a discrete vocabulary of “visual words,” describe an image as a histogram of word occurrences, and compare images by comparing histograms. It is the classical engine behind loop-closure detection and relocalization in SLAM (DBoW2 in ORB-SLAM, for example).

Pipeline

  1. Vocabulary construction (offline). Collect a large set of local descriptors (e.g., ORB, SIFT) from a training corpus and cluster them with k-means into KK cluster centers — the visual words. For binary descriptors, cluster centers are computed with Hamming-space medians rather than Euclidean means.
  2. Image representation. For each new image, extract descriptors and assign each to its nearest visual word. The image becomes a KK-dimensional histogram v\mathbf{v}, where bin kk counts the keypoints assigned to word kk. All spatial layout is discarded — hence “bag.”
  3. TF-IDF weighting. Not all words are equally informative. Each bin is weighted by term frequency times inverse document frequency:

vk=nk,dndlogNnkv_k = \frac{n_{k,d}}{n_d} \cdot \log\frac{N}{n_k}

where nk,dn_{k,d} is the count of word kk in image dd, ndn_d the total words in the image, NN the number of database images, and nkn_k the number of database images containing word kk. Words that appear everywhere (floor texture, foliage) get down-weighted; rare, distinctive words dominate the score.

  1. Retrieval. Normalize the weighted histograms and score similarity — DBoW2 uses an L1-based score

s(v1,v2)=112v1v1v2v21s(\mathbf{v}_1, \mathbf{v}_2) = 1 - \frac{1}{2}\left\lVert \frac{\mathbf{v}_1}{\lVert \mathbf{v}_1 \rVert} - \frac{\mathbf{v}_2}{\lVert \mathbf{v}_2 \rVert} \right\rVert_1

— and return the top-scoring database images as loop-closure candidates.

The hierarchical vocabulary tree

A flat vocabulary requires O(K)O(K) distance computations to assign each descriptor, which is too slow for the K105K \sim 10^510610^6 words needed for discriminative retrieval. DBoW2/DBoW3 arrange words in a vocabulary tree built by hierarchical k-means: kk branches per node, LL levels, giving K=kLK = k^L leaf words. Assigning a descriptor walks the tree comparing against kk children per level — O(kL)O(kL), i.e., logarithmic in KK.

Two index structures make retrieval and verification fast:

Practical notes

Limitations and the verification step

BoVW throws away geometry, so two images with similar texture statistics can score high without depicting the same place (perceptual aliasing — corridors, brick walls). Practical loop-closure pipelines therefore treat BoVW output as candidates only and confirm them with geometric verification: match features, estimate an essential/fundamental matrix or PnP pose with RANSAC, and accept the loop only if enough inliers support a consistent geometry. Temporal consistency checks (requiring several consecutive matching frames) further suppress false positives. A vocabulary trained on one visual domain also transfers imperfectly to very different environments.

Why it matters for SLAM

Odometry drifts; loop closure is what makes SLAM more than dead reckoning, and BoVW is the classical way to find loops in constant time regardless of map size. It also powers relocalization after tracking loss and multi-session map alignment. Even as learned global descriptors (NetVLAD and successors) replace the retrieval stage in newer systems, the BoVW architecture — quantize, weight, invert, verify — remains the reference design, and DBoW2-style vocabularies still run in many deployed systems because they are fast, compact, and CPU-only.

Hands-on