Overview

8 Content based image search

Modern search experiences increasingly go beyond text, allowing users to find relevant images by their visual content rather than by manually written captions or tags. The chapter motivates content-based image retrieval by highlighting the ambiguity, cost, and limited recall of text-only metadata, and introduces “query by example,” where a user submits an image to retrieve visually similar results. It frames the core challenge as learning robust, compact representations of images that capture semantics (objects, shapes, textures) while being resilient to variations like lighting, viewpoint, and scale.

The chapter first surveys classical, hand-crafted representations. Global features, such as color histograms across RGB (or other color spaces), are simple and fast but ignore spatial layout and can miss object-specific detail. Local features, like SIFT and SURF, detect stable keypoints (edges, corners, textured patches) and describe them in a way that is robust to rotation and scale. Because each image yields many local descriptors, they are aggregated into a fixed-length vector using the Bag of Visual Words model: cluster all local descriptors (e.g., with k-means) to form a vocabulary of “visual words,” then represent each image by its distribution over these words. This pipeline enables efficient similarity search but still requires careful engineering and can struggle to capture higher-level semantics.

Deep learning—specifically convolutional neural networks—supersedes manual feature design by learning hierarchical features directly from pixels: early layers capture edges and simple patterns; deeper layers learn shapes and object parts. Trained in a supervised fashion on labeled images, CNNs yield both class predictions and powerful intermediate embeddings (from a dense layer) that serve as semantic feature vectors for retrieval. The chapter shows how to build such a model, extract embeddings for query and catalog images, and index them for efficient nearest-neighbor search (e.g., using Lucene’s n-dimensional point structures based on k-d trees). The end-to-end flow is: feed the query image to the CNN, extract its embedding, then retrieve and rank the most similar embeddings to deliver visually relevant results.

Figure 8.1. Learning image abstractions incrementally
replearn images
Figure 8.2. Manually captioned sport car images
cars tagged
Figure 8.3. Some images described as "tall man"
tallman
Figure 8.4. Some images described as "basketball player wearing a 35 numbered jersey"
jersey35
Figure 8.5. Some visually similar images
simsearch1
Figure 8.6. A red sport car photo retrieved by the image search engine based on a picture of a red sport car model
rsc
Figure 8.7. A picture of a butterfly
butterfly
Figure 8.8. Color histogram for the butterfly picture
butterfly hist
Figure 8.9. Another picture of a butterfly
butterfly2
Figure 8.10. Comparing histograms of two butterfly images
hist comp
Figure 8.11. Analyzing image cells
butterfly grid
Figure 8.12. Stars and clusters
stars
Figure 8.13. Image of dogs classified by AlexNet
dogs
Figure 8.14. Building blocks (and flow) of convolutional neural networks
cnn flow
Figure 8.15. Convolutional layer
convlayer
Figure 8.16. Some examples from the Cifar dataset
cifar
Figure 8.17. A simple CNN with 2 convolution + pooling layers
cnn simple
Figure 8.18. Testing a CNN on the Cifar dataset
cifar test
Figure 8.19. Resulting model from DL4J UI
cnn dl4j

 Summary

  • Searching through binary content like images requires learning a representation which can capture visual semantics that can be compared across different images
  • Traditional techniques for feature extraction have limits and require significant engineering efforts
  • Convolutional neural networks are at the core of the recent rise of deep learning as they can learn image representation abstractions (edges, shapes, objects) incrementally during network training
  • CNNs can be used to extract feature vectors from images that can be leveraged to perform similar images search

FAQ

What is content-based image retrieval (CBIR) and why use it instead of text tags?CBIR searches images by what they visually contain rather than by attached text. It avoids the high cost and inconsistency of manual captions/tags, reduces ambiguity in wording, and enables “query by example,” where a user supplies an image to find visually similar ones.
Why can’t we match images by raw pixels?Pixel values lack context, are sensitive to lighting, viewpoint, and scale, and are extremely high-dimensional. Two photos of the same object often differ significantly at the pixel level, making direct comparisons unreliable and computationally expensive.
What’s the difference between global and local visual features?Global features summarize the whole image (for example, color histograms), offering speed and interpretability but missing fine details and spatial cues. Local features (for example, SIFT or SURF) describe distinctive points/regions and are more robust to rotation and scale, but yield many descriptors per image that must be aggregated.
How does the Bag of Visual Words (BoVW) model represent images?BoVW extracts local descriptors from images, clusters all descriptors (for example, with k-means) to form a fixed “visual vocabulary,” and represents each image as a histogram over these visual words. Similar images have similar histograms, enabling efficient comparison.
How do convolutional neural networks (CNNs) learn image representations?CNNs learn features directly from pixels via stacked convolution and pooling layers. Early layers learn edges and simple patterns; deeper layers learn shapes and objects. The dense layer near the end acts as a compact feature vector (embedding) that captures image semantics for retrieval.
What do convolutional and pooling layers do?Convolutional layers apply learned filters over local receptive fields with shared parameters and configurable stride, producing activation maps. Pooling layers downsample these maps (for example, max or average pooling), reducing dimensionality while preserving salient information.
How do I extract feature vectors from a trained CNN for search?Feed an image through the CNN and take the activations from the penultimate (dense) layer as its embedding. Store that fixed-length vector to represent the image during indexing and similarity search.
How can I index and search image embeddings efficiently?Index each image’s embedding as an n-dimensional point (for example, Lucene FloatPoint). Use a nearest-neighbor search structure (for example, a k-d tree in Lucene) to retrieve the top-k closest embeddings to a query image’s embedding. This scales better than brute-force comparisons.
When are handcrafted features still useful?Global features like color histograms are fast and interpretable, and can work well for tasks like near-duplicate detection. Local features (SIFT/SURF) are robust to rotation/scale and can be effective when deep models are unavailable, though deep embeddings usually outperform them semantically.
What’s a practical end-to-end pipeline for content-based image search?1) Train or fine-tune a CNN on labeled images similar to your domain. 2) Extract embeddings from the CNN’s penultimate layer for all images. 3) Index embeddings as points in a search engine (for example, Lucene). 4) At query time, compute the query image’s embedding and run a nearest-neighbor search to return similar images. Consider normalization, distance choice, dimensionality, and evaluation for best results.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Deep Learning for Search ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Deep Learning for Search ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Deep Learning for Search ebook for free