opencv on mbed

Dependencies:   mbed

Committer:
joeverbout
Date:
Thu Mar 31 21:16:38 2016 +0000
Revision:
0:ea44dc9ed014
OpenCV on mbed attempt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeverbout 0:ea44dc9ed014 1 /*M///////////////////////////////////////////////////////////////////////////////////////
joeverbout 0:ea44dc9ed014 2 //
joeverbout 0:ea44dc9ed014 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
joeverbout 0:ea44dc9ed014 4 //
joeverbout 0:ea44dc9ed014 5 // By downloading, copying, installing or using the software you agree to this license.
joeverbout 0:ea44dc9ed014 6 // If you do not agree to this license, do not download, install,
joeverbout 0:ea44dc9ed014 7 // copy or use the software.
joeverbout 0:ea44dc9ed014 8 //
joeverbout 0:ea44dc9ed014 9 //
joeverbout 0:ea44dc9ed014 10 // License Agreement
joeverbout 0:ea44dc9ed014 11 // For Open Source Computer Vision Library
joeverbout 0:ea44dc9ed014 12 //
joeverbout 0:ea44dc9ed014 13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
joeverbout 0:ea44dc9ed014 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
joeverbout 0:ea44dc9ed014 15 // Third party copyrights are property of their respective owners.
joeverbout 0:ea44dc9ed014 16 //
joeverbout 0:ea44dc9ed014 17 // Redistribution and use in source and binary forms, with or without modification,
joeverbout 0:ea44dc9ed014 18 // are permitted provided that the following conditions are met:
joeverbout 0:ea44dc9ed014 19 //
joeverbout 0:ea44dc9ed014 20 // * Redistribution's of source code must retain the above copyright notice,
joeverbout 0:ea44dc9ed014 21 // this list of conditions and the following disclaimer.
joeverbout 0:ea44dc9ed014 22 //
joeverbout 0:ea44dc9ed014 23 // * Redistribution's in binary form must reproduce the above copyright notice,
joeverbout 0:ea44dc9ed014 24 // this list of conditions and the following disclaimer in the documentation
joeverbout 0:ea44dc9ed014 25 // and/or other materials provided with the distribution.
joeverbout 0:ea44dc9ed014 26 //
joeverbout 0:ea44dc9ed014 27 // * The name of the copyright holders may not be used to endorse or promote products
joeverbout 0:ea44dc9ed014 28 // derived from this software without specific prior written permission.
joeverbout 0:ea44dc9ed014 29 //
joeverbout 0:ea44dc9ed014 30 // This software is provided by the copyright holders and contributors "as is" and
joeverbout 0:ea44dc9ed014 31 // any express or implied warranties, including, but not limited to, the implied
joeverbout 0:ea44dc9ed014 32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
joeverbout 0:ea44dc9ed014 33 // In no event shall the Intel Corporation or contributors be liable for any direct,
joeverbout 0:ea44dc9ed014 34 // indirect, incidental, special, exemplary, or consequential damages
joeverbout 0:ea44dc9ed014 35 // (including, but not limited to, procurement of substitute goods or services;
joeverbout 0:ea44dc9ed014 36 // loss of use, data, or profits; or business interruption) however caused
joeverbout 0:ea44dc9ed014 37 // and on any theory of liability, whether in contract, strict liability,
joeverbout 0:ea44dc9ed014 38 // or tort (including negligence or otherwise) arising in any way out of
joeverbout 0:ea44dc9ed014 39 // the use of this software, even if advised of the possibility of such damage.
joeverbout 0:ea44dc9ed014 40 //
joeverbout 0:ea44dc9ed014 41 //M*/
joeverbout 0:ea44dc9ed014 42
joeverbout 0:ea44dc9ed014 43 #ifndef __OPENCV_STITCHING_MATCHERS_HPP__
joeverbout 0:ea44dc9ed014 44 #define __OPENCV_STITCHING_MATCHERS_HPP__
joeverbout 0:ea44dc9ed014 45
joeverbout 0:ea44dc9ed014 46 #include "opencv2/core.hpp"
joeverbout 0:ea44dc9ed014 47 #include "opencv2/features2d.hpp"
joeverbout 0:ea44dc9ed014 48
joeverbout 0:ea44dc9ed014 49 #include "opencv2/opencv_modules.hpp"
joeverbout 0:ea44dc9ed014 50
joeverbout 0:ea44dc9ed014 51 #ifdef HAVE_OPENCV_XFEATURES2D
joeverbout 0:ea44dc9ed014 52 # include "opencv2/xfeatures2d/cuda.hpp"
joeverbout 0:ea44dc9ed014 53 #endif
joeverbout 0:ea44dc9ed014 54
joeverbout 0:ea44dc9ed014 55 namespace cv {
joeverbout 0:ea44dc9ed014 56 namespace detail {
joeverbout 0:ea44dc9ed014 57
joeverbout 0:ea44dc9ed014 58 //! @addtogroup stitching_match
joeverbout 0:ea44dc9ed014 59 //! @{
joeverbout 0:ea44dc9ed014 60
joeverbout 0:ea44dc9ed014 61 /** @brief Structure containing image keypoints and descriptors. */
joeverbout 0:ea44dc9ed014 62 struct CV_EXPORTS ImageFeatures
joeverbout 0:ea44dc9ed014 63 {
joeverbout 0:ea44dc9ed014 64 int img_idx;
joeverbout 0:ea44dc9ed014 65 Size img_size;
joeverbout 0:ea44dc9ed014 66 std::vector<KeyPoint> keypoints;
joeverbout 0:ea44dc9ed014 67 UMat descriptors;
joeverbout 0:ea44dc9ed014 68 };
joeverbout 0:ea44dc9ed014 69
joeverbout 0:ea44dc9ed014 70 /** @brief Feature finders base class */
joeverbout 0:ea44dc9ed014 71 class CV_EXPORTS FeaturesFinder
joeverbout 0:ea44dc9ed014 72 {
joeverbout 0:ea44dc9ed014 73 public:
joeverbout 0:ea44dc9ed014 74 virtual ~FeaturesFinder() {}
joeverbout 0:ea44dc9ed014 75 /** @overload */
joeverbout 0:ea44dc9ed014 76 void operator ()(InputArray image, ImageFeatures &features);
joeverbout 0:ea44dc9ed014 77 /** @brief Finds features in the given image.
joeverbout 0:ea44dc9ed014 78
joeverbout 0:ea44dc9ed014 79 @param image Source image
joeverbout 0:ea44dc9ed014 80 @param features Found features
joeverbout 0:ea44dc9ed014 81 @param rois Regions of interest
joeverbout 0:ea44dc9ed014 82
joeverbout 0:ea44dc9ed014 83 @sa detail::ImageFeatures, Rect_
joeverbout 0:ea44dc9ed014 84 */
joeverbout 0:ea44dc9ed014 85 void operator ()(InputArray image, ImageFeatures &features, const std::vector<cv::Rect> &rois);
joeverbout 0:ea44dc9ed014 86 /** @brief Frees unused memory allocated before if there is any. */
joeverbout 0:ea44dc9ed014 87 virtual void collectGarbage() {}
joeverbout 0:ea44dc9ed014 88
joeverbout 0:ea44dc9ed014 89 protected:
joeverbout 0:ea44dc9ed014 90 /** @brief This method must implement features finding logic in order to make the wrappers
joeverbout 0:ea44dc9ed014 91 detail::FeaturesFinder::operator()_ work.
joeverbout 0:ea44dc9ed014 92
joeverbout 0:ea44dc9ed014 93 @param image Source image
joeverbout 0:ea44dc9ed014 94 @param features Found features
joeverbout 0:ea44dc9ed014 95
joeverbout 0:ea44dc9ed014 96 @sa detail::ImageFeatures */
joeverbout 0:ea44dc9ed014 97 virtual void find(InputArray image, ImageFeatures &features) = 0;
joeverbout 0:ea44dc9ed014 98 };
joeverbout 0:ea44dc9ed014 99
joeverbout 0:ea44dc9ed014 100 /** @brief SURF features finder.
joeverbout 0:ea44dc9ed014 101
joeverbout 0:ea44dc9ed014 102 @sa detail::FeaturesFinder, SURF
joeverbout 0:ea44dc9ed014 103 */
joeverbout 0:ea44dc9ed014 104 class CV_EXPORTS SurfFeaturesFinder : public FeaturesFinder
joeverbout 0:ea44dc9ed014 105 {
joeverbout 0:ea44dc9ed014 106 public:
joeverbout 0:ea44dc9ed014 107 SurfFeaturesFinder(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
joeverbout 0:ea44dc9ed014 108 int num_octaves_descr = /*4*/3, int num_layers_descr = /*2*/4);
joeverbout 0:ea44dc9ed014 109
joeverbout 0:ea44dc9ed014 110 private:
joeverbout 0:ea44dc9ed014 111 void find(InputArray image, ImageFeatures &features);
joeverbout 0:ea44dc9ed014 112
joeverbout 0:ea44dc9ed014 113 Ptr<FeatureDetector> detector_;
joeverbout 0:ea44dc9ed014 114 Ptr<DescriptorExtractor> extractor_;
joeverbout 0:ea44dc9ed014 115 Ptr<Feature2D> surf;
joeverbout 0:ea44dc9ed014 116 };
joeverbout 0:ea44dc9ed014 117
joeverbout 0:ea44dc9ed014 118 /** @brief ORB features finder. :
joeverbout 0:ea44dc9ed014 119
joeverbout 0:ea44dc9ed014 120 @sa detail::FeaturesFinder, ORB
joeverbout 0:ea44dc9ed014 121 */
joeverbout 0:ea44dc9ed014 122 class CV_EXPORTS OrbFeaturesFinder : public FeaturesFinder
joeverbout 0:ea44dc9ed014 123 {
joeverbout 0:ea44dc9ed014 124 public:
joeverbout 0:ea44dc9ed014 125 OrbFeaturesFinder(Size _grid_size = Size(3,1), int nfeatures=1500, float scaleFactor=1.3f, int nlevels=5);
joeverbout 0:ea44dc9ed014 126
joeverbout 0:ea44dc9ed014 127 private:
joeverbout 0:ea44dc9ed014 128 void find(InputArray image, ImageFeatures &features);
joeverbout 0:ea44dc9ed014 129
joeverbout 0:ea44dc9ed014 130 Ptr<ORB> orb;
joeverbout 0:ea44dc9ed014 131 Size grid_size;
joeverbout 0:ea44dc9ed014 132 };
joeverbout 0:ea44dc9ed014 133
joeverbout 0:ea44dc9ed014 134
joeverbout 0:ea44dc9ed014 135 #ifdef HAVE_OPENCV_XFEATURES2D
joeverbout 0:ea44dc9ed014 136 class CV_EXPORTS SurfFeaturesFinderGpu : public FeaturesFinder
joeverbout 0:ea44dc9ed014 137 {
joeverbout 0:ea44dc9ed014 138 public:
joeverbout 0:ea44dc9ed014 139 SurfFeaturesFinderGpu(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
joeverbout 0:ea44dc9ed014 140 int num_octaves_descr = 4, int num_layers_descr = 2);
joeverbout 0:ea44dc9ed014 141
joeverbout 0:ea44dc9ed014 142 void collectGarbage();
joeverbout 0:ea44dc9ed014 143
joeverbout 0:ea44dc9ed014 144 private:
joeverbout 0:ea44dc9ed014 145 void find(InputArray image, ImageFeatures &features);
joeverbout 0:ea44dc9ed014 146
joeverbout 0:ea44dc9ed014 147 cuda::GpuMat image_;
joeverbout 0:ea44dc9ed014 148 cuda::GpuMat gray_image_;
joeverbout 0:ea44dc9ed014 149 cuda::SURF_CUDA surf_;
joeverbout 0:ea44dc9ed014 150 cuda::GpuMat keypoints_;
joeverbout 0:ea44dc9ed014 151 cuda::GpuMat descriptors_;
joeverbout 0:ea44dc9ed014 152 int num_octaves_, num_layers_;
joeverbout 0:ea44dc9ed014 153 int num_octaves_descr_, num_layers_descr_;
joeverbout 0:ea44dc9ed014 154 };
joeverbout 0:ea44dc9ed014 155 #endif
joeverbout 0:ea44dc9ed014 156
joeverbout 0:ea44dc9ed014 157 /** @brief Structure containing information about matches between two images.
joeverbout 0:ea44dc9ed014 158
joeverbout 0:ea44dc9ed014 159 It's assumed that there is a homography between those images.
joeverbout 0:ea44dc9ed014 160 */
joeverbout 0:ea44dc9ed014 161 struct CV_EXPORTS MatchesInfo
joeverbout 0:ea44dc9ed014 162 {
joeverbout 0:ea44dc9ed014 163 MatchesInfo();
joeverbout 0:ea44dc9ed014 164 MatchesInfo(const MatchesInfo &other);
joeverbout 0:ea44dc9ed014 165 const MatchesInfo& operator =(const MatchesInfo &other);
joeverbout 0:ea44dc9ed014 166
joeverbout 0:ea44dc9ed014 167 int src_img_idx, dst_img_idx; //!< Images indices (optional)
joeverbout 0:ea44dc9ed014 168 std::vector<DMatch> matches;
joeverbout 0:ea44dc9ed014 169 std::vector<uchar> inliers_mask; //!< Geometrically consistent matches mask
joeverbout 0:ea44dc9ed014 170 int num_inliers; //!< Number of geometrically consistent matches
joeverbout 0:ea44dc9ed014 171 Mat H; //!< Estimated homography
joeverbout 0:ea44dc9ed014 172 double confidence; //!< Confidence two images are from the same panorama
joeverbout 0:ea44dc9ed014 173 };
joeverbout 0:ea44dc9ed014 174
joeverbout 0:ea44dc9ed014 175 /** @brief Feature matchers base class. */
joeverbout 0:ea44dc9ed014 176 class CV_EXPORTS FeaturesMatcher
joeverbout 0:ea44dc9ed014 177 {
joeverbout 0:ea44dc9ed014 178 public:
joeverbout 0:ea44dc9ed014 179 virtual ~FeaturesMatcher() {}
joeverbout 0:ea44dc9ed014 180
joeverbout 0:ea44dc9ed014 181 /** @overload
joeverbout 0:ea44dc9ed014 182 @param features1 First image features
joeverbout 0:ea44dc9ed014 183 @param features2 Second image features
joeverbout 0:ea44dc9ed014 184 @param matches_info Found matches
joeverbout 0:ea44dc9ed014 185 */
joeverbout 0:ea44dc9ed014 186 void operator ()(const ImageFeatures &features1, const ImageFeatures &features2,
joeverbout 0:ea44dc9ed014 187 MatchesInfo& matches_info) { match(features1, features2, matches_info); }
joeverbout 0:ea44dc9ed014 188
joeverbout 0:ea44dc9ed014 189 /** @brief Performs images matching.
joeverbout 0:ea44dc9ed014 190
joeverbout 0:ea44dc9ed014 191 @param features Features of the source images
joeverbout 0:ea44dc9ed014 192 @param pairwise_matches Found pairwise matches
joeverbout 0:ea44dc9ed014 193 @param mask Mask indicating which image pairs must be matched
joeverbout 0:ea44dc9ed014 194
joeverbout 0:ea44dc9ed014 195 The function is parallelized with the TBB library.
joeverbout 0:ea44dc9ed014 196
joeverbout 0:ea44dc9ed014 197 @sa detail::MatchesInfo
joeverbout 0:ea44dc9ed014 198 */
joeverbout 0:ea44dc9ed014 199 void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
joeverbout 0:ea44dc9ed014 200 const cv::UMat &mask = cv::UMat());
joeverbout 0:ea44dc9ed014 201
joeverbout 0:ea44dc9ed014 202 /** @return True, if it's possible to use the same matcher instance in parallel, false otherwise
joeverbout 0:ea44dc9ed014 203 */
joeverbout 0:ea44dc9ed014 204 bool isThreadSafe() const { return is_thread_safe_; }
joeverbout 0:ea44dc9ed014 205
joeverbout 0:ea44dc9ed014 206 /** @brief Frees unused memory allocated before if there is any.
joeverbout 0:ea44dc9ed014 207 */
joeverbout 0:ea44dc9ed014 208 virtual void collectGarbage() {}
joeverbout 0:ea44dc9ed014 209
joeverbout 0:ea44dc9ed014 210 protected:
joeverbout 0:ea44dc9ed014 211 FeaturesMatcher(bool is_thread_safe = false) : is_thread_safe_(is_thread_safe) {}
joeverbout 0:ea44dc9ed014 212
joeverbout 0:ea44dc9ed014 213 /** @brief This method must implement matching logic in order to make the wrappers
joeverbout 0:ea44dc9ed014 214 detail::FeaturesMatcher::operator()_ work.
joeverbout 0:ea44dc9ed014 215
joeverbout 0:ea44dc9ed014 216 @param features1 first image features
joeverbout 0:ea44dc9ed014 217 @param features2 second image features
joeverbout 0:ea44dc9ed014 218 @param matches_info found matches
joeverbout 0:ea44dc9ed014 219 */
joeverbout 0:ea44dc9ed014 220 virtual void match(const ImageFeatures &features1, const ImageFeatures &features2,
joeverbout 0:ea44dc9ed014 221 MatchesInfo& matches_info) = 0;
joeverbout 0:ea44dc9ed014 222
joeverbout 0:ea44dc9ed014 223 bool is_thread_safe_;
joeverbout 0:ea44dc9ed014 224 };
joeverbout 0:ea44dc9ed014 225
joeverbout 0:ea44dc9ed014 226 /** @brief Features matcher which finds two best matches for each feature and leaves the best one only if the
joeverbout 0:ea44dc9ed014 227 ratio between descriptor distances is greater than the threshold match_conf
joeverbout 0:ea44dc9ed014 228
joeverbout 0:ea44dc9ed014 229 @sa detail::FeaturesMatcher
joeverbout 0:ea44dc9ed014 230 */
joeverbout 0:ea44dc9ed014 231 class CV_EXPORTS BestOf2NearestMatcher : public FeaturesMatcher
joeverbout 0:ea44dc9ed014 232 {
joeverbout 0:ea44dc9ed014 233 public:
joeverbout 0:ea44dc9ed014 234 /** @brief Constructs a "best of 2 nearest" matcher.
joeverbout 0:ea44dc9ed014 235
joeverbout 0:ea44dc9ed014 236 @param try_use_gpu Should try to use GPU or not
joeverbout 0:ea44dc9ed014 237 @param match_conf Match distances ration threshold
joeverbout 0:ea44dc9ed014 238 @param num_matches_thresh1 Minimum number of matches required for the 2D projective transform
joeverbout 0:ea44dc9ed014 239 estimation used in the inliers classification step
joeverbout 0:ea44dc9ed014 240 @param num_matches_thresh2 Minimum number of matches required for the 2D projective transform
joeverbout 0:ea44dc9ed014 241 re-estimation on inliers
joeverbout 0:ea44dc9ed014 242 */
joeverbout 0:ea44dc9ed014 243 BestOf2NearestMatcher(bool try_use_gpu = false, float match_conf = 0.3f, int num_matches_thresh1 = 6,
joeverbout 0:ea44dc9ed014 244 int num_matches_thresh2 = 6);
joeverbout 0:ea44dc9ed014 245
joeverbout 0:ea44dc9ed014 246 void collectGarbage();
joeverbout 0:ea44dc9ed014 247
joeverbout 0:ea44dc9ed014 248 protected:
joeverbout 0:ea44dc9ed014 249 void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info);
joeverbout 0:ea44dc9ed014 250
joeverbout 0:ea44dc9ed014 251 int num_matches_thresh1_;
joeverbout 0:ea44dc9ed014 252 int num_matches_thresh2_;
joeverbout 0:ea44dc9ed014 253 Ptr<FeaturesMatcher> impl_;
joeverbout 0:ea44dc9ed014 254 };
joeverbout 0:ea44dc9ed014 255
joeverbout 0:ea44dc9ed014 256 class CV_EXPORTS BestOf2NearestRangeMatcher : public BestOf2NearestMatcher
joeverbout 0:ea44dc9ed014 257 {
joeverbout 0:ea44dc9ed014 258 public:
joeverbout 0:ea44dc9ed014 259 BestOf2NearestRangeMatcher(int range_width = 5, bool try_use_gpu = false, float match_conf = 0.3f,
joeverbout 0:ea44dc9ed014 260 int num_matches_thresh1 = 6, int num_matches_thresh2 = 6);
joeverbout 0:ea44dc9ed014 261
joeverbout 0:ea44dc9ed014 262 void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
joeverbout 0:ea44dc9ed014 263 const cv::UMat &mask = cv::UMat());
joeverbout 0:ea44dc9ed014 264
joeverbout 0:ea44dc9ed014 265
joeverbout 0:ea44dc9ed014 266 protected:
joeverbout 0:ea44dc9ed014 267 int range_width_;
joeverbout 0:ea44dc9ed014 268 };
joeverbout 0:ea44dc9ed014 269
joeverbout 0:ea44dc9ed014 270 //! @} stitching_match
joeverbout 0:ea44dc9ed014 271
joeverbout 0:ea44dc9ed014 272 } // namespace detail
joeverbout 0:ea44dc9ed014 273 } // namespace cv
joeverbout 0:ea44dc9ed014 274
joeverbout 0:ea44dc9ed014 275 #endif // __OPENCV_STITCHING_MATCHERS_HPP__
joeverbout 0:ea44dc9ed014 276