randpattern.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2015, Baisheng Lai (laibaisheng@gmail.com), Zhejiang University,
  14. // all rights reserved.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef __OPENCV_RANDOMPATTERN_HPP__
  42. #define __OPENCV_RANDOMPATTERN_HPP__
  43. #include "opencv2/features2d.hpp"
  44. #include "opencv2/highgui.hpp"
  45. namespace cv { namespace randpattern {
  46. //! @addtogroup ccalib
  47. //! @{
  48. /** @brief Class for finding features points and corresponding 3D in world coordinate of
  49. a "random" pattern, which can be to be used in calibration. It is useful when pattern is
  50. partly occluded or only a part of pattern can be observed in multiple cameras calibration.
  51. The pattern can be generated by RandomPatternGenerator class described in this file.
  52. Please refer to paper
  53. B. Li, L. Heng, K. Kevin and M. Pollefeys, "A Multiple-Camera System
  54. Calibration Toolbox Using A Feature Descriptor-Based Calibration
  55. Pattern", in IROS 2013.
  56. */
  57. class CV_EXPORTS RandomPatternCornerFinder
  58. {
  59. public:
  60. /* @brief Construct RandomPatternCornerFinder object
  61. @param patternWidth the real width of "random" pattern in a user defined unit.
  62. @param patternHeight the real height of "random" pattern in a user defined unit.
  63. @param nMiniMatch number of minimal matches, otherwise that image is abandoned
  64. @depth depth of output objectPoints and imagePoints, set it to be CV_32F or CV_64F.
  65. @showExtraction whether show feature extraction, 0 for no and 1 for yes.
  66. @detector feature detector to detect feature points in pattern and images.
  67. @descriptor feature descriptor.
  68. @matcher feature matcher.
  69. */
  70. RandomPatternCornerFinder(float patternWidth, float patternHeight,
  71. int nminiMatch = 20, int depth = CV_32F, int verbose = 0, int showExtraction = 0,
  72. Ptr<FeatureDetector> detector = AKAZE::create(AKAZE::DESCRIPTOR_MLDB, 0, 3, 0.005f),
  73. Ptr<DescriptorExtractor> descriptor = AKAZE::create(AKAZE::DESCRIPTOR_MLDB,0, 3, 0.005f),
  74. Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-L1"));
  75. /* @brief Load pattern image and compute features for pattern
  76. @param patternImage image for "random" pattern generated by RandomPatternGenerator, run it first.
  77. */
  78. void loadPattern(const cv::Mat &patternImage);
  79. /* @brief Load pattern and features
  80. @param patternImage image for "random" pattern generated by RandomPatternGenerator, run it first.
  81. @param patternKeyPoints keyPoints created from a FeatureDetector.
  82. @param patternDescriptors descriptors created from a DescriptorExtractor.
  83. */
  84. void loadPattern(const cv::Mat &patternImage, const std::vector<cv::KeyPoint> &patternKeyPoints, const cv::Mat &patternDescriptors);
  85. /* @brief Compute matched object points and image points which are used for calibration
  86. The objectPoints (3D) and imagePoints (2D) are stored inside the class. Run getObjectPoints()
  87. and getImagePoints() to get them.
  88. @param inputImages vector of 8-bit grayscale images containing "random" pattern
  89. that are used for calibration.
  90. */
  91. void computeObjectImagePoints(std::vector<cv::Mat> inputImages);
  92. //void computeObjectImagePoints2(std::vector<cv::Mat> inputImages);
  93. /* @brief Compute object and image points for a single image. It returns a vector<Mat> that
  94. the first element stores the imagePoints and the second one stores the objectPoints.
  95. @param inputImage single input image for calibration
  96. */
  97. std::vector<cv::Mat> computeObjectImagePointsForSingle(cv::Mat inputImage);
  98. /* @brief Get object(3D) points
  99. */
  100. const std::vector<cv::Mat> &getObjectPoints();
  101. /* @brief and image(2D) points
  102. */
  103. const std::vector<cv::Mat> &getImagePoints();
  104. private:
  105. std::vector<cv::Mat> _objectPonits, _imagePoints;
  106. float _patternWidth, _patternHeight;
  107. cv::Size _patternImageSize;
  108. int _nminiMatch;
  109. int _depth;
  110. int _verbose;
  111. Ptr<FeatureDetector> _detector;
  112. Ptr<DescriptorExtractor> _descriptor;
  113. Ptr<DescriptorMatcher> _matcher;
  114. Mat _descriptorPattern;
  115. std::vector<cv::KeyPoint> _keypointsPattern;
  116. Mat _patternImage;
  117. int _showExtraction;
  118. void keyPoints2MatchedLocation(const std::vector<cv::KeyPoint>& imageKeypoints,
  119. const std::vector<cv::KeyPoint>& patternKeypoints, const std::vector<cv::DMatch> matchces,
  120. cv::Mat& matchedImagelocation, cv::Mat& matchedPatternLocation);
  121. void getFilteredLocation(cv::Mat& imageKeypoints, cv::Mat& patternKeypoints, const cv::Mat mask);
  122. void getObjectImagePoints(const cv::Mat& imageKeypoints, const cv::Mat& patternKeypoints);
  123. void crossCheckMatching( cv::Ptr<DescriptorMatcher>& descriptorMatcher,
  124. const Mat& descriptors1, const Mat& descriptors2,
  125. std::vector<DMatch>& filteredMatches12, int knn=1 );
  126. void drawCorrespondence(const Mat& image1, const std::vector<cv::KeyPoint> keypoint1,
  127. const Mat& image2, const std::vector<cv::KeyPoint> keypoint2, const std::vector<cv::DMatch> matchces,
  128. const Mat& mask1, const Mat& mask2, const int step);
  129. };
  130. /* @brief Class to generate "random" pattern image that are used for RandomPatternCornerFinder
  131. Please refer to paper
  132. B. Li, L. Heng, K. Kevin and M. Pollefeys, "A Multiple-Camera System
  133. Calibration Toolbox Using A Feature Descriptor-Based Calibration
  134. Pattern", in IROS 2013.
  135. */
  136. class CV_EXPORTS RandomPatternGenerator
  137. {
  138. public:
  139. /* @brief Construct RandomPatternGenerator
  140. @param imageWidth image width of the generated pattern image
  141. @param imageHeight image height of the generated pattern image
  142. */
  143. RandomPatternGenerator(int imageWidth, int imageHeight);
  144. /* @brief Generate pattern
  145. */
  146. void generatePattern();
  147. /* @brief Get pattern
  148. */
  149. cv::Mat getPattern();
  150. private:
  151. cv::Mat _pattern;
  152. int _imageWidth, _imageHeight;
  153. };
  154. //! @}
  155. }} //namespace randpattern, cv
  156. #endif