estimation_internal.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2015-2016, Jiri Horner.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the Jiri Horner nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  *********************************************************************/
36 
37 #ifndef ESTIMATION_INTERNAL_H_
38 #define ESTIMATION_INTERNAL_H_
39 
41 
42 #include <cassert>
43 #include <opencv2/core/utility.hpp>
44 #include <opencv2/core/version.hpp>
45 #include <opencv2/features2d.hpp>
46 #include <opencv2/imgcodecs.hpp>
47 #include <opencv2/stitching/detail/matchers.hpp>
48 
49 #ifdef HAVE_OPENCV_XFEATURES2D
50 #include <opencv2/xfeatures2d/nonfree.hpp>
51 #endif
52 
53 namespace combine_grids
54 {
55 namespace internal
56 {
57 #if CV_VERSION_MAJOR >= 4
58 
59 static inline cv::Ptr<cv::Feature2D> chooseFeatureFinder(FeatureType type)
60 {
61  switch (type) {
62  case FeatureType::AKAZE:
63  return cv::AKAZE::create();
64  case FeatureType::ORB:
65  return cv::ORB::create();
66  case FeatureType::SURF:
67 #ifdef HAVE_OPENCV_XFEATURES2D
68  return xfeatures2d::SURF::create();
69 #else
70  return cv::AKAZE::create();
71 #endif
72  }
73 
74  assert(false);
75  return {};
76 }
77 
78 #else // (CV_VERSION_MAJOR < 4)
79 
80 static inline cv::Ptr<cv::detail::FeaturesFinder>
82 {
83  switch (type) {
84  case FeatureType::AKAZE:
85  return cv::makePtr<cv::detail::AKAZEFeaturesFinder>();
86  case FeatureType::ORB:
87  return cv::makePtr<cv::detail::OrbFeaturesFinder>();
88  case FeatureType::SURF:
89  return cv::makePtr<cv::detail::SurfFeaturesFinder>();
90  }
91 
92  assert(false);
93  return {};
94 }
95 
96 #endif // CV_VERSION_MAJOR >= 4
97 
98 static inline void writeDebugMatchingInfo(
99  const std::vector<cv::Mat>& images,
100  const std::vector<cv::detail::ImageFeatures>& image_features,
101  const std::vector<cv::detail::MatchesInfo>& pairwise_matches)
102 {
103  for (auto& match_info : pairwise_matches) {
104  if (match_info.H.empty() ||
105  match_info.src_img_idx >= match_info.dst_img_idx) {
106  continue;
107  }
108  std::cout << match_info.src_img_idx << " " << match_info.dst_img_idx
109  << std::endl
110  << "features: "
111  << image_features[size_t(match_info.src_img_idx)].keypoints.size()
112  << " "
113  << image_features[size_t(match_info.dst_img_idx)].keypoints.size()
114  << std::endl
115  << "matches: " << match_info.matches.size() << std::endl
116  << "inliers: " << match_info.num_inliers << std::endl
117  << "inliers/matches ratio: "
118  << match_info.num_inliers / double(match_info.matches.size())
119  << std::endl
120  << "confidence: " << match_info.confidence << std::endl
121  << match_info.H << std::endl;
122  cv::Mat img;
123  // draw all matches
124  cv::drawMatches(images[size_t(match_info.src_img_idx)],
125  image_features[size_t(match_info.src_img_idx)].keypoints,
126  images[size_t(match_info.dst_img_idx)],
127  image_features[size_t(match_info.dst_img_idx)].keypoints,
128  match_info.matches, img);
129  cv::imwrite(std::to_string(match_info.src_img_idx) + "_" +
130  std::to_string(match_info.dst_img_idx) + "_matches.png",
131  img);
132  // draw inliers only
133  cv::drawMatches(
134  images[size_t(match_info.src_img_idx)],
135  image_features[size_t(match_info.src_img_idx)].keypoints,
136  images[size_t(match_info.dst_img_idx)],
137  image_features[size_t(match_info.dst_img_idx)].keypoints,
138  match_info.matches, img, cv::Scalar::all(-1), cv::Scalar::all(-1),
139  *reinterpret_cast<const std::vector<char>*>(&match_info.inliers_mask));
140  cv::imwrite(std::to_string(match_info.src_img_idx) + "_" +
141  std::to_string(match_info.dst_img_idx) +
142  "_matches_inliers.png",
143  img);
144  }
145 }
146 
147 } // namespace internal
148 } // namespace combine_grids
149 
150 #endif // ESTIMATION_INTERNAL_H_
static void writeDebugMatchingInfo(const std::vector< cv::Mat > &images, const std::vector< cv::detail::ImageFeatures > &image_features, const std::vector< cv::detail::MatchesInfo > &pairwise_matches)
static cv::Ptr< cv::detail::FeaturesFinder > chooseFeatureFinder(FeatureType type)


map_merge
Author(s): Jiri Horner
autogenerated on Mon Feb 28 2022 22:46:02