ccalib.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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) 2014, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  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_CCALIB_HPP__
  42. #define __OPENCV_CCALIB_HPP__
  43. #include <opencv2/core.hpp>
  44. #include <opencv2/features2d.hpp>
  45. #include <opencv2/imgproc.hpp>
  46. #include <opencv2/calib3d.hpp>
  47. #include <vector>
  48. /** @defgroup ccalib Custom Calibration Pattern for 3D reconstruction
  49. */
  50. namespace cv{ namespace ccalib{
  51. //! @addtogroup ccalib
  52. //! @{
  53. class CV_EXPORTS CustomPattern : public Algorithm
  54. {
  55. public:
  56. CustomPattern();
  57. virtual ~CustomPattern();
  58. bool create(InputArray pattern, const Size2f boardSize, OutputArray output = noArray());
  59. bool findPattern(InputArray image, OutputArray matched_features, OutputArray pattern_points, const double ratio = 0.7,
  60. const double proj_error = 8.0, const bool refine_position = false, OutputArray out = noArray(),
  61. OutputArray H = noArray(), OutputArray pattern_corners = noArray());
  62. bool isInitialized();
  63. void getPatternPoints(std::vector<KeyPoint>& original_points);
  64. /**<
  65. Returns a vector<Point> of the original points.
  66. */
  67. double getPixelSize();
  68. /**<
  69. Get the pixel size of the pattern
  70. */
  71. bool setFeatureDetector(Ptr<FeatureDetector> featureDetector);
  72. bool setDescriptorExtractor(Ptr<DescriptorExtractor> extractor);
  73. bool setDescriptorMatcher(Ptr<DescriptorMatcher> matcher);
  74. Ptr<FeatureDetector> getFeatureDetector();
  75. Ptr<DescriptorExtractor> getDescriptorExtractor();
  76. Ptr<DescriptorMatcher> getDescriptorMatcher();
  77. double calibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints,
  78. Size imageSize, InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
  79. OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, int flags = 0,
  80. TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON));
  81. /**<
  82. Calls the calirateCamera function with the same inputs.
  83. */
  84. bool findRt(InputArray objectPoints, InputArray imagePoints, InputArray cameraMatrix, InputArray distCoeffs,
  85. InputOutputArray rvec, InputOutputArray tvec, bool useExtrinsicGuess = false, int flags = SOLVEPNP_ITERATIVE);
  86. bool findRt(InputArray image, InputArray cameraMatrix, InputArray distCoeffs,
  87. InputOutputArray rvec, InputOutputArray tvec, bool useExtrinsicGuess = false, int flags = SOLVEPNP_ITERATIVE);
  88. /**<
  89. Uses solvePnP to find the rotation and translation of the pattern
  90. with respect to the camera frame.
  91. */
  92. bool findRtRANSAC(InputArray objectPoints, InputArray imagePoints, InputArray cameraMatrix, InputArray distCoeffs,
  93. InputOutputArray rvec, InputOutputArray tvec, bool useExtrinsicGuess = false, int iterationsCount = 100,
  94. float reprojectionError = 8.0, int minInliersCount = 100, OutputArray inliers = noArray(), int flags = SOLVEPNP_ITERATIVE);
  95. bool findRtRANSAC(InputArray image, InputArray cameraMatrix, InputArray distCoeffs,
  96. InputOutputArray rvec, InputOutputArray tvec, bool useExtrinsicGuess = false, int iterationsCount = 100,
  97. float reprojectionError = 8.0, int minInliersCount = 100, OutputArray inliers = noArray(), int flags = SOLVEPNP_ITERATIVE);
  98. /**<
  99. Uses solvePnPRansac()
  100. */
  101. void drawOrientation(InputOutputArray image, InputArray tvec, InputArray rvec, InputArray cameraMatrix,
  102. InputArray distCoeffs, double axis_length = 3, int axis_width = 2);
  103. /**<
  104. pattern_corners -> projected over the image position of the edges of the pattern.
  105. */
  106. private:
  107. Mat img_roi;
  108. std::vector<Point2f> obj_corners;
  109. double pxSize;
  110. bool initialized;
  111. Ptr<FeatureDetector> detector;
  112. Ptr<DescriptorExtractor> descriptorExtractor;
  113. Ptr<DescriptorMatcher> descriptorMatcher;
  114. std::vector<KeyPoint> keypoints;
  115. std::vector<Point3f> points3d;
  116. Mat descriptor;
  117. bool init(Mat& image, const float pixel_size, OutputArray output = noArray());
  118. bool findPatternPass(const Mat& image, std::vector<Point2f>& matched_features, std::vector<Point3f>& pattern_points,
  119. Mat& H, std::vector<Point2f>& scene_corners, const double pratio, const double proj_error,
  120. const bool refine_position = false, const Mat& mask = Mat(), OutputArray output = noArray());
  121. void scaleFoundPoints(const double squareSize, const std::vector<KeyPoint>& corners, std::vector<Point3f>& pts3d);
  122. void check_matches(std::vector<Point2f>& matched, const std::vector<Point2f>& pattern, std::vector<DMatch>& good, std::vector<Point3f>& pattern_3d, const Mat& H);
  123. void keypoints2points(const std::vector<KeyPoint>& in, std::vector<Point2f>& out);
  124. void updateKeypointsPos(std::vector<KeyPoint>& in, const std::vector<Point2f>& new_pos);
  125. void refinePointsPos(const Mat& img, std::vector<Point2f>& p);
  126. void refineKeypointsPos(const Mat& img, std::vector<KeyPoint>& kp);
  127. };
  128. //! @}
  129. }} // namespace ccalib, cv
  130. #endif