vocabtree  0.0.1
vision.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <opencv2/opencv.hpp>
4 #include <opencv2/stitching.hpp>
5 #include <memory>
6 
7 /// Provides useful wrappers around many OpenCV functions as well
8 /// as some simple vision - based routines.
9 namespace vision {
10 
11  typedef std::set<std::pair<int,int> > MatchesSet;
12 
13  /// Describes SIFT extraction parameters when calling OpenCV's
14  /// SIFT implementation. See the OpenCV documentation for
15  /// more details.
16  struct SIFTParams {
17  int max_features = 0; /// Max number of features to retrieve (0 keeps all features).
18  int num_octave_layers = 3; /// Number of octave layers in the pyramid to compute.
19  double contrast_threshold = 0.04; /// Contrast threshold, lower -> more features, but less stable.
20  double edge_threshold = 11; /// Edge threshold, higher -> more features, but less stable.
21  double sigma = 1.6; /// Smoothing kernel size, higher -> more smoothing, fewer features.
22  };
23 
24  /// Given a grayscale image, img, and SIFT extraction parameters computes sparse sift features. If
25  /// params is a nullptr, we use the default settings for SIFTParams. Returns true if successful,
26  /// false otherwise.
27  bool compute_sparse_sift_feature(const cv::Mat &img, const std::shared_ptr<const SIFTParams> &params ,
28  cv::Mat &keypoints, cv::Mat &descriptors);
29 
30  /// Given a set of image descriptors, a descriptor matcher computes the the cluster match based on the matcher
31  /// of each descriptor and returns the histogram of clusters. If cluster_indices is not null, it will also
32  /// return the cluster assignment of each descriptor vector. Returns true if successful, false otherwise.
33  bool compute_bow_feature(const cv::Mat& descriptors, const cv::Ptr<cv::DescriptorMatcher> &matcher,
34  cv::Mat& bow_descriptors, std::shared_ptr< std::vector<std::vector<uint32_t> > > cluster_indices);
35 
36  /// Given a vocabulary constructs a FLANN based matcher needed to compute Bag of Words (BoW) features. Expects
37  /// the vocabulary to be in the same format as computed in the search module.
38  cv::Ptr<cv::DescriptorMatcher> construct_descriptor_matcher(const cv::Mat &vocabulary);
39 
40  /// Merges the descriptors into a single matrix. This is useful for clustering, which requires a single
41  /// matrix.
42  cv::Mat merge_descriptors(std::vector<cv::Mat> &descriptors, bool release_original = true);
43 
44  /// Computes a homography between the input pairs of points and descriptors and returns the result
45  /// in MatchesInfo (including homography, confidence score, etc.) If output inlier vectors are
46  /// provided, will insert a list of feature indices belonging to the homography inliers.
47  void geo_verify_h(const cv::Mat &descriptors0, const cv::Mat &points0,
48  const cv::Mat &descriptors1, const cv::Mat &points1, cv::detail::MatchesInfo &matches_info,
49  std::vector<uint32_t> *inliers0 = 0, std::vector<uint32_t> *inliers1 = 0);
50 
51  /// Computes a fundamental matrix between the input pairs of points and descriptors and returns the result
52  /// in MatchesInfo (including fundamental, confidence score, etc.) If output inlier vectors are
53  /// provided, will insert a list of feature indices belonging to the fundamental inliers.
54  void geo_verify_f(const cv::Mat &descriptors0, const cv::Mat &points0,
55  const cv::Mat &descriptors1, const cv::Mat &points1, cv::detail::MatchesInfo &matches_info,
56  std::vector<uint32_t> *inliers0 = 0, std::vector<uint32_t> *inliers1 = 0);
57 
58  /// Returns true if matches_info represents a good match, false if otherwise. The heuristic is based
59  /// on the total number of inliers and the ratio of inliers to outliers.
60  bool is_good_match(const cv::detail::MatchesInfo &matches_info);
61 };