multicalib.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_MULTICAMERACALIBRATION_HPP__
  42. #define __OPENCV_MULTICAMERACALIBRATION_HPP__
  43. #include "opencv2/ccalib/randpattern.hpp"
  44. #include "opencv2/ccalib/omnidir.hpp"
  45. #include <string>
  46. #include <iostream>
  47. namespace cv { namespace multicalib {
  48. //! @addtogroup ccalib
  49. //! @{
  50. #define HEAD -1
  51. #define INVALID -2
  52. /** @brief Class for multiple camera calibration that supports pinhole camera and omnidirection camera.
  53. For omnidirectional camera model, please refer to omnidir.hpp in ccalib module.
  54. It first calibrate each camera individually, then a bundle adjustment like optimization is applied to
  55. refine extrinsic parameters. So far, it only support "random" pattern for calibration,
  56. see randomPattern.hpp in ccalib module for details.
  57. Images that are used should be named by "cameraIdx-timestamp.*", several images with the same timestamp
  58. means that they are the same pattern that are photographed. cameraIdx should start from 0.
  59. For more details, please refer to paper
  60. B. Li, L. Heng, K. Kevin and M. Pollefeys, "A Multiple-Camera System
  61. Calibration Toolbox Using A Feature Descriptor-Based Calibration
  62. Pattern", in IROS 2013.
  63. */
  64. class CV_EXPORTS MultiCameraCalibration
  65. {
  66. public:
  67. enum {
  68. PINHOLE,
  69. OMNIDIRECTIONAL
  70. //FISHEYE
  71. };
  72. // an edge connects a camera and pattern
  73. struct edge
  74. {
  75. int cameraVertex; // vertex index for camera in this edge
  76. int photoVertex; // vertex index for pattern in this edge
  77. int photoIndex; // photo index among photos for this camera
  78. Mat transform; // transform from pattern to camera
  79. edge(int cv, int pv, int pi, Mat trans)
  80. {
  81. cameraVertex = cv;
  82. photoVertex = pv;
  83. photoIndex = pi;
  84. transform = trans;
  85. }
  86. };
  87. struct vertex
  88. {
  89. Mat pose; // relative pose to the first camera. For camera vertex, it is the
  90. // transform from the first camera to this camera, for pattern vertex,
  91. // it is the transform from pattern to the first camera
  92. int timestamp; // timestamp of photo, only available for photo vertex
  93. vertex(Mat po, int ts)
  94. {
  95. pose = po;
  96. timestamp = ts;
  97. }
  98. vertex()
  99. {
  100. pose = Mat::eye(4, 4, CV_32F);
  101. timestamp = -1;
  102. }
  103. };
  104. /* @brief Constructor
  105. @param cameraType camera type, PINHOLE or OMNIDIRECTIONAL
  106. @param nCameras number of cameras
  107. @fileName filename of string list that are used for calibration, the file is generated
  108. by imagelist_creator from OpenCv samples. The first one in the list is the pattern filename.
  109. @patternWidth the physical width of pattern, in user defined unit.
  110. @patternHeight the physical height of pattern, in user defined unit.
  111. @showExtration whether show extracted features and feature filtering.
  112. @nMiniMatches minimal number of matched features for a frame.
  113. @flags Calibration flags
  114. @criteria optimization stopping criteria.
  115. @detector feature detector that detect feature points in pattern and images.
  116. @descriptor feature descriptor.
  117. @matcher feature matcher.
  118. */
  119. MultiCameraCalibration(int cameraType, int nCameras, const std::string& fileName, float patternWidth,
  120. float patternHeight, int verbose = 0, int showExtration = 0, int nMiniMatches = 20, int flags = 0,
  121. TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 200, 1e-7),
  122. Ptr<FeatureDetector> detector = AKAZE::create(AKAZE::DESCRIPTOR_MLDB, 0, 3, 0.006f),
  123. Ptr<DescriptorExtractor> descriptor = AKAZE::create(AKAZE::DESCRIPTOR_MLDB,0, 3, 0.006f),
  124. Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-L1"));
  125. /* @brief load images
  126. */
  127. void loadImages();
  128. /* @brief initialize multiple camera calibration. It calibrates each camera individually.
  129. */
  130. void initialize();
  131. /* @brief optimization extrinsic parameters
  132. */
  133. double optimizeExtrinsics();
  134. /* @brief run multi-camera camera calibration, it runs loadImage(), initialize() and optimizeExtrinsics()
  135. */
  136. double run();
  137. /* @brief write camera parameters to file.
  138. */
  139. void writeParameters(const std::string& filename);
  140. private:
  141. std::vector<std::string> readStringList();
  142. int getPhotoVertex(int timestamp);
  143. void graphTraverse(const Mat& G, int begin, std::vector<int>& order, std::vector<int>& pre);
  144. void findRowNonZero(const Mat& row, Mat& idx);
  145. void computeJacobianExtrinsic(const Mat& extrinsicParams, Mat& JTJ_inv, Mat& JTE);
  146. void computePhotoCameraJacobian(const Mat& rvecPhoto, const Mat& tvecPhoto, const Mat& rvecCamera,
  147. const Mat& tvecCamera, Mat& rvecTran, Mat& tvecTran, const Mat& objectPoints, const Mat& imagePoints, const Mat& K,
  148. const Mat& distort, const Mat& xi, Mat& jacobianPhoto, Mat& jacobianCamera, Mat& E);
  149. void compose_motion(InputArray _om1, InputArray _T1, InputArray _om2, InputArray _T2, Mat& om3, Mat& T3, Mat& dom3dom1,
  150. Mat& dom3dT1, Mat& dom3dom2, Mat& dom3dT2, Mat& dT3dom1, Mat& dT3dT1, Mat& dT3dom2, Mat& dT3dT2);
  151. void JRodriguesMatlab(const Mat& src, Mat& dst);
  152. void dAB(InputArray A, InputArray B, OutputArray dABdA, OutputArray dABdB);
  153. double computeProjectError(Mat& parameters);
  154. void vector2parameters(const Mat& parameters, std::vector<Vec3f>& rvecVertex, std::vector<Vec3f>& tvecVertexs);
  155. void parameters2vector(const std::vector<Vec3f>& rvecVertex, const std::vector<Vec3f>& tvecVertex, Mat& parameters);
  156. int _camType; //PINHOLE, FISHEYE or OMNIDIRECTIONAL
  157. int _nCamera;
  158. int _nMiniMatches;
  159. int _flags;
  160. int _verbose;
  161. double _error;
  162. float _patternWidth, _patternHeight;
  163. TermCriteria _criteria;
  164. std::string _filename;
  165. int _showExtraction;
  166. Ptr<FeatureDetector> _detector;
  167. Ptr<DescriptorExtractor> _descriptor;
  168. Ptr<DescriptorMatcher> _matcher;
  169. std::vector<edge> _edgeList;
  170. std::vector<vertex> _vertexList;
  171. std::vector<std::vector<cv::Mat> > _objectPointsForEachCamera;
  172. std::vector<std::vector<cv::Mat> > _imagePointsForEachCamera;
  173. std::vector<cv::Mat> _cameraMatrix;
  174. std::vector<cv::Mat> _distortCoeffs;
  175. std::vector<cv::Mat> _xi;
  176. std::vector<std::vector<Mat> > _omEachCamera, _tEachCamera;
  177. };
  178. //! @}
  179. }} // namespace multicalib, cv
  180. #endif