omnidir.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include <opencv2/core.hpp>
  42. #include <vector>
  43. #ifndef __OPENCV_OMNIDIR_HPP__
  44. #define __OPENCV_OMNIDIR_HPP__
  45. namespace cv
  46. {
  47. namespace omnidir
  48. {
  49. //! @addtogroup ccalib
  50. //! @{
  51. enum {
  52. CALIB_USE_GUESS = 1,
  53. CALIB_FIX_SKEW = 2,
  54. CALIB_FIX_K1 = 4,
  55. CALIB_FIX_K2 = 8,
  56. CALIB_FIX_P1 = 16,
  57. CALIB_FIX_P2 = 32,
  58. CALIB_FIX_XI = 64,
  59. CALIB_FIX_GAMMA = 128,
  60. CALIB_FIX_CENTER = 256
  61. };
  62. enum{
  63. RECTIFY_PERSPECTIVE = 1,
  64. RECTIFY_CYLINDRICAL = 2,
  65. RECTIFY_LONGLATI = 3,
  66. RECTIFY_STEREOGRAPHIC = 4
  67. };
  68. enum{
  69. XYZRGB = 1,
  70. XYZ = 2
  71. };
  72. /**
  73. * This module was accepted as a GSoC 2015 project for OpenCV, authored by
  74. * Baisheng Lai, mentored by Bo Li.
  75. */
  76. /** @brief Projects points for omnidirectional camera using CMei's model
  77. @param objectPoints Object points in world coordinate, vector of vector of Vec3f or Mat of
  78. 1xN/Nx1 3-channel of type CV_32F and N is the number of points. 64F is also acceptable.
  79. @param imagePoints Output array of image points, vector of vector of Vec2f or
  80. 1xN/Nx1 2-channel of type CV_32F. 64F is also acceptable.
  81. @param rvec vector of rotation between world coordinate and camera coordinate, i.e., om
  82. @param tvec vector of translation between pattern coordinate and camera coordinate
  83. @param K Camera matrix \f$K = \vecthreethree{f_x}{s}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$.
  84. @param D Input vector of distortion coefficients \f$(k_1, k_2, p_1, p_2)\f$.
  85. @param xi The parameter xi for CMei's model
  86. @param jacobian Optional output 2Nx16 of type CV_64F jacobian matrix, contains the derivatives of
  87. image pixel points wrt parameters including \f$om, T, f_x, f_y, s, c_x, c_y, xi, k_1, k_2, p_1, p_2\f$.
  88. This matrix will be used in calibration by optimization.
  89. The function projects object 3D points of world coordinate to image pixels, parameter by intrinsic
  90. and extrinsic parameters. Also, it optionally compute a by-product: the jacobian matrix containing
  91. contains the derivatives of image pixel points wrt intrinsic and extrinsic parameters.
  92. */
  93. CV_EXPORTS_W void projectPoints(InputArray objectPoints, OutputArray imagePoints, InputArray rvec, InputArray tvec,
  94. InputArray K, double xi, InputArray D, OutputArray jacobian = noArray());
  95. /** @brief Undistort 2D image points for omnidirectional camera using CMei's model
  96. @param distorted Array of distorted image points, vector of Vec2f
  97. or 1xN/Nx1 2-channel Mat of type CV_32F, 64F depth is also acceptable
  98. @param K Camera matrix \f$K = \vecthreethree{f_x}{s}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$.
  99. @param D Distortion coefficients \f$(k_1, k_2, p_1, p_2)\f$.
  100. @param xi The parameter xi for CMei's model
  101. @param R Rotation trainsform between the original and object space : 3x3 1-channel, or vector: 3x1/1x3
  102. 1-channel or 1x1 3-channel
  103. @param undistorted array of normalized object points, vector of Vec2f/Vec2d or 1xN/Nx1 2-channel Mat with the same
  104. depth of distorted points.
  105. */
  106. CV_EXPORTS_W void undistortPoints(InputArray distorted, OutputArray undistorted, InputArray K, InputArray D, InputArray xi, InputArray R);
  107. /** @brief Computes undistortion and rectification maps for omnidirectional camera image transform by a rotation R.
  108. It output two maps that are used for cv::remap(). If D is empty then zero distortion is used,
  109. if R or P is empty then identity matrices are used.
  110. @param K Camera matrix \f$K = \vecthreethree{f_x}{s}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$, with depth CV_32F or CV_64F
  111. @param D Input vector of distortion coefficients \f$(k_1, k_2, p_1, p_2)\f$, with depth CV_32F or CV_64F
  112. @param xi The parameter xi for CMei's model
  113. @param R Rotation transform between the original and object space : 3x3 1-channel, or vector: 3x1/1x3, with depth CV_32F or CV_64F
  114. @param P New camera matrix (3x3) or new projection matrix (3x4)
  115. @param size Undistorted image size.
  116. @param mltype Type of the first output map that can be CV_32FC1 or CV_16SC2 . See convertMaps()
  117. for details.
  118. @param map1 The first output map.
  119. @param map2 The second output map.
  120. @param flags Flags indicates the rectification type, RECTIFY_PERSPECTIVE, RECTIFY_CYLINDRICAL, RECTIFY_LONGLATI and RECTIFY_STEREOGRAPHIC
  121. are supported.
  122. */
  123. CV_EXPORTS_W void initUndistortRectifyMap(InputArray K, InputArray D, InputArray xi, InputArray R, InputArray P, const cv::Size& size,
  124. int mltype, OutputArray map1, OutputArray map2, int flags);
  125. /** @brief Undistort omnidirectional images to perspective images
  126. @param distorted The input omnidirectional image.
  127. @param undistorted The output undistorted image.
  128. @param K Camera matrix \f$K = \vecthreethree{f_x}{s}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$.
  129. @param D Input vector of distortion coefficients \f$(k_1, k_2, p_1, p_2)\f$.
  130. @param xi The parameter xi for CMei's model.
  131. @param flags Flags indicates the rectification type, RECTIFY_PERSPECTIVE, RECTIFY_CYLINDRICAL, RECTIFY_LONGLATI and RECTIFY_STEREOGRAPHIC
  132. @param Knew Camera matrix of the distorted image. If it is not assigned, it is just K.
  133. @param new_size The new image size. By default, it is the size of distorted.
  134. @param R Rotation matrix between the input and output images. By default, it is identity matrix.
  135. */
  136. CV_EXPORTS_W void undistortImage(InputArray distorted, OutputArray undistorted, InputArray K, InputArray D, InputArray xi, int flags,
  137. InputArray Knew = cv::noArray(), const Size& new_size = Size(), InputArray R = Mat::eye(3, 3, CV_64F));
  138. /** @brief Perform omnidirectional camera calibration, the default depth of outputs is CV_64F.
  139. @param objectPoints Vector of vector of Vec3f object points in world (pattern) coordinate.
  140. It also can be vector of Mat with size 1xN/Nx1 and type CV_32FC3. Data with depth of 64_F is also acceptable.
  141. @param imagePoints Vector of vector of Vec2f corresponding image points of objectPoints. It must be the same
  142. size and the same type with objectPoints.
  143. @param size Image size of calibration images.
  144. @param K Output calibrated camera matrix.
  145. @param xi Output parameter xi for CMei's model
  146. @param D Output distortion parameters \f$(k_1, k_2, p_1, p_2)\f$
  147. @param rvecs Output rotations for each calibration images
  148. @param tvecs Output translation for each calibration images
  149. @param flags The flags that control calibrate
  150. @param criteria Termination criteria for optimization
  151. @param idx Indices of images that pass initialization, which are really used in calibration. So the size of rvecs is the
  152. same as idx.total().
  153. */
  154. CV_EXPORTS_W double calibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, Size size,
  155. InputOutputArray K, InputOutputArray xi, InputOutputArray D, OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
  156. int flags, TermCriteria criteria, OutputArray idx=noArray());
  157. /** @brief Stereo calibration for omnidirectional camera model. It computes the intrinsic parameters for two
  158. cameras and the extrinsic parameters between two cameras. The default depth of outputs is CV_64F.
  159. @param objectPoints Object points in world (pattern) coordinate. Its type is vector<vector<Vec3f> >.
  160. It also can be vector of Mat with size 1xN/Nx1 and type CV_32FC3. Data with depth of 64_F is also acceptable.
  161. @param imagePoints1 The corresponding image points of the first camera, with type vector<vector<Vec2f> >.
  162. It must be the same size and the same type as objectPoints.
  163. @param imagePoints2 The corresponding image points of the second camera, with type vector<vector<Vec2f> >.
  164. It must be the same size and the same type as objectPoints.
  165. @param imageSize1 Image size of calibration images of the first camera.
  166. @param imageSize2 Image size of calibration images of the second camera.
  167. @param K1 Output camera matrix for the first camera.
  168. @param xi1 Output parameter xi of Mei's model for the first camera
  169. @param D1 Output distortion parameters \f$(k_1, k_2, p_1, p_2)\f$ for the first camera
  170. @param K2 Output camera matrix for the first camera.
  171. @param xi2 Output parameter xi of CMei's model for the second camera
  172. @param D2 Output distortion parameters \f$(k_1, k_2, p_1, p_2)\f$ for the second camera
  173. @param rvec Output rotation between the first and second camera
  174. @param tvec Output translation between the first and second camera
  175. @param rvecsL Output rotation for each image of the first camera
  176. @param tvecsL Output translation for each image of the first camera
  177. @param flags The flags that control stereoCalibrate
  178. @param criteria Termination criteria for optimization
  179. @param idx Indices of image pairs that pass initialization, which are really used in calibration. So the size of rvecs is the
  180. same as idx.total().
  181. @
  182. */
  183. CV_EXPORTS_W double stereoCalibrate(InputOutputArrayOfArrays objectPoints, InputOutputArrayOfArrays imagePoints1, InputOutputArrayOfArrays imagePoints2,
  184. const Size& imageSize1, const Size& imageSize2, InputOutputArray K1, InputOutputArray xi1, InputOutputArray D1, InputOutputArray K2, InputOutputArray xi2,
  185. InputOutputArray D2, OutputArray rvec, OutputArray tvec, OutputArrayOfArrays rvecsL, OutputArrayOfArrays tvecsL, int flags, TermCriteria criteria, OutputArray idx=noArray());
  186. /** @brief Stereo rectification for omnidirectional camera model. It computes the rectification rotations for two cameras
  187. @param R Rotation between the first and second camera
  188. @param T Translation between the first and second camera
  189. @param R1 Output 3x3 rotation matrix for the first camera
  190. @param R2 Output 3x3 rotation matrix for the second camera
  191. */
  192. CV_EXPORTS_W void stereoRectify(InputArray R, InputArray T, OutputArray R1, OutputArray R2);
  193. /** @brief Stereo 3D reconstruction from a pair of images
  194. @param image1 The first input image
  195. @param image2 The second input image
  196. @param K1 Input camera matrix of the first camera
  197. @param D1 Input distortion parameters \f$(k_1, k_2, p_1, p_2)\f$ for the first camera
  198. @param xi1 Input parameter xi for the first camera for CMei's model
  199. @param K2 Input camera matrix of the second camera
  200. @param D2 Input distortion parameters \f$(k_1, k_2, p_1, p_2)\f$ for the second camera
  201. @param xi2 Input parameter xi for the second camera for CMei's model
  202. @param R Rotation between the first and second camera
  203. @param T Translation between the first and second camera
  204. @param flag Flag of rectification type, RECTIFY_PERSPECTIVE or RECTIFY_LONGLATI
  205. @param numDisparities The parameter 'numDisparities' in StereoSGBM, see StereoSGBM for details.
  206. @param SADWindowSize The parameter 'SADWindowSize' in StereoSGBM, see StereoSGBM for details.
  207. @param disparity Disparity map generated by stereo matching
  208. @param image1Rec Rectified image of the first image
  209. @param image2Rec rectified image of the second image
  210. @param newSize Image size of rectified image, see omnidir::undistortImage
  211. @param Knew New camera matrix of rectified image, see omnidir::undistortImage
  212. @param pointCloud Point cloud of 3D reconstruction, with type CV_64FC3
  213. @param pointType Point cloud type, it can be XYZRGB or XYZ
  214. */
  215. CV_EXPORTS_W void stereoReconstruct(InputArray image1, InputArray image2, InputArray K1, InputArray D1, InputArray xi1,
  216. InputArray K2, InputArray D2, InputArray xi2, InputArray R, InputArray T, int flag, int numDisparities, int SADWindowSize,
  217. OutputArray disparity, OutputArray image1Rec, OutputArray image2Rec, const Size& newSize = Size(), InputArray Knew = cv::noArray(),
  218. OutputArray pointCloud = cv::noArray(), int pointType = XYZRGB);
  219. namespace internal
  220. {
  221. void initializeCalibration(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, Size size, OutputArrayOfArrays omAll,
  222. OutputArrayOfArrays tAll, OutputArray K, double& xi, OutputArray idx = noArray());
  223. void initializeStereoCalibration(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2,
  224. const Size& size1, const Size& size2, OutputArray om, OutputArray T, OutputArrayOfArrays omL, OutputArrayOfArrays tL, OutputArray K1, OutputArray D1, OutputArray K2, OutputArray D2,
  225. double &xi1, double &xi2, int flags, OutputArray idx);
  226. void computeJacobian(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, InputArray parameters, Mat& JTJ_inv, Mat& JTE, int flags,
  227. double epsilon);
  228. void computeJacobianStereo(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2,
  229. InputArray parameters, Mat& JTJ_inv, Mat& JTE, int flags, double epsilon);
  230. void encodeParameters(InputArray K, InputArrayOfArrays omAll, InputArrayOfArrays tAll, InputArray distoaration, double xi, OutputArray parameters);
  231. void encodeParametersStereo(InputArray K1, InputArray K2, InputArray om, InputArray T, InputArrayOfArrays omL, InputArrayOfArrays tL,
  232. InputArray D1, InputArray D2, double xi1, double xi2, OutputArray parameters);
  233. void decodeParameters(InputArray paramsters, OutputArray K, OutputArrayOfArrays omAll, OutputArrayOfArrays tAll, OutputArray distoration, double& xi);
  234. void decodeParametersStereo(InputArray parameters, OutputArray K1, OutputArray K2, OutputArray om, OutputArray T, OutputArrayOfArrays omL,
  235. OutputArrayOfArrays tL, OutputArray D1, OutputArray D2, double& xi1, double& xi2);
  236. void estimateUncertainties(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, InputArray parameters, Mat& errors, Vec2d& std_error, double& rms, int flags);
  237. void estimateUncertaintiesStereo(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2, InputArray parameters, Mat& errors,
  238. Vec2d& std_error, double& rms, int flags);
  239. double computeMeanReproErr(InputArrayOfArrays imagePoints, InputArrayOfArrays proImagePoints);
  240. double computeMeanReproErr(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, InputArray K, InputArray D, double xi, InputArrayOfArrays omAll,
  241. InputArrayOfArrays tAll);
  242. double computeMeanReproErrStereo(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2, InputArray K1, InputArray K2,
  243. InputArray D1, InputArray D2, double xi1, double xi2, InputArray om, InputArray T, InputArrayOfArrays omL, InputArrayOfArrays TL);
  244. void subMatrix(const Mat& src, Mat& dst, const std::vector<int>& cols, const std::vector<int>& rows);
  245. void flags2idx(int flags, std::vector<int>& idx, int n);
  246. void flags2idxStereo(int flags, std::vector<int>& idx, int n);
  247. void fillFixed(Mat&G, int flags, int n);
  248. void fillFixedStereo(Mat& G, int flags, int n);
  249. double findMedian(const Mat& row);
  250. Vec3d findMedian3(InputArray mat);
  251. void getInterset(InputArray idx1, InputArray idx2, OutputArray inter1, OutputArray inter2, OutputArray inter_ori);
  252. void compose_motion(InputArray _om1, InputArray _T1, InputArray _om2, InputArray _T2, Mat& om3, Mat& T3, Mat& dom3dom1,
  253. Mat& dom3dT1, Mat& dom3dom2, Mat& dom3dT2, Mat& dT3dom1, Mat& dT3dT1, Mat& dT3dom2, Mat& dT3dT2);
  254. //void JRodriguesMatlab(const Mat& src, Mat& dst);
  255. //void dAB(InputArray A, InputArray B, OutputArray dABdA, OutputArray dABdB);
  256. } // internal
  257. //! @}
  258. } // omnidir
  259. } //cv
  260. #endif