vocabtree  0.0.1
bag_of_words.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
5 /// Implements a Bag of Words based (BoW) image search. Note that search here is not implemented
6 /// and would throw an error should you try to call it. A naive implementation would have to compute
7 /// tf-idf with all possible image. Instead, you should train a BoW model and
8 /// use this model in conjuction with a InvertedIndex search model to perform a query.
9 class BagOfWords : public SearchBase {
10 public:
11 
12  /// Subclass of train params base which specifies inverted index training parameters.
13  struct TrainParams : public TrainParamsBase {
14  uint32_t numClusters = 512; // k number of clusters
15  uint32_t numFeatures = 0; // number of features to cluster
16  };
17 
18  /// Subclass of train params base which specifies inverted index training parameters.
19  struct SearchParams : public SearchParamsBase {
20 
21  };
22 
23  /// Subclass of match results base which also returns scores
24  struct MatchResults : public MatchResultsBase {
25  std::vector<float> tfidf_scores;
26  };
27 
28  BagOfWords();
29  BagOfWords(const std::string &file_path);
30 
31  /// Given a set of training parameters, list of images, trains. Returns true if successful, false
32  /// if not successful.
33  bool train(Dataset &dataset, const std::shared_ptr<const TrainParamsBase> &params,
34  const std::vector< std::shared_ptr<const Image > > &examples);
35 
36  /// Loads a trained search structure from the input filepath
37  bool load (const std::string &file_path);
38 
39  /// Saves a trained search structure to the input filepath
40  bool save (const std::string &file_path) const;
41 
42  /// Given a set of search parameters, a query image, searches for matching images and returns the match.
43  /// Search is not valid for bag of words - this would require computing tf-idf on all possible images in the dataset,
44  /// and this function will assert(0) should you try to run it. Instead, you should train a Bag of Words (BoW) model
45  /// and use it with one of the other search mechanisms, such as the inverted index.
46  std::shared_ptr<MatchResultsBase> search(Dataset &dataset, const std::shared_ptr<const SearchParamsBase> &params, const std::shared_ptr<const Image > &example);
47 
48  /// Returns the vocabulary matrix.
49  cv::Mat vocabulary() const;
50 
51  /// Returns the number of clusters in the vocabulary.
52  uint32_t num_clusters() const;
53 
54 protected:
55 
57 };