operations.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Copyright (C) 2015, Itseez Inc., all rights reserved.
  17. // Third party copyrights are property of their respective owners.
  18. //
  19. // Redistribution and use in source and binary forms, with or without modification,
  20. // are permitted provided that the following conditions are met:
  21. //
  22. // * Redistribution's of source code must retain the above copyright notice,
  23. // this list of conditions and the following disclaimer.
  24. //
  25. // * Redistribution's in binary form must reproduce the above copyright notice,
  26. // this list of conditions and the following disclaimer in the documentation
  27. // and/or other materials provided with the distribution.
  28. //
  29. // * The name of the copyright holders may not be used to endorse or promote products
  30. // derived from this software without specific prior written permission.
  31. //
  32. // This software is provided by the copyright holders and contributors "as is" and
  33. // any express or implied warranties, including, but not limited to, the implied
  34. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  35. // In no event shall the Intel Corporation or contributors be liable for any direct,
  36. // indirect, incidental, special, exemplary, or consequential damages
  37. // (including, but not limited to, procurement of substitute goods or services;
  38. // loss of use, data, or profits; or business interruption) however caused
  39. // and on any theory of liability, whether in contract, strict liability,
  40. // or tort (including negligence or otherwise) arising in any way out of
  41. // the use of this software, even if advised of the possibility of such damage.
  42. //
  43. //M*/
  44. #ifndef OPENCV_CORE_OPERATIONS_HPP
  45. #define OPENCV_CORE_OPERATIONS_HPP
  46. #ifndef __cplusplus
  47. # error operations.hpp header must be compiled as C++
  48. #endif
  49. #include <cstdio>
  50. #if defined(__GNUC__) || defined(__clang__) // at least GCC 3.1+, clang 3.5+
  51. # if defined(__MINGW_PRINTF_FORMAT) // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.
  52. # define CV_FORMAT_PRINTF(string_idx, first_to_check) __attribute__ ((format (__MINGW_PRINTF_FORMAT, string_idx, first_to_check)))
  53. # else
  54. # define CV_FORMAT_PRINTF(string_idx, first_to_check) __attribute__ ((format (printf, string_idx, first_to_check)))
  55. # endif
  56. #else
  57. # define CV_FORMAT_PRINTF(A, B)
  58. #endif
  59. namespace cv
  60. {
  61. //! @cond IGNORED
  62. ////////////////////////////// Matx methods depending on core API /////////////////////////////
  63. namespace internal
  64. {
  65. template<typename _Tp, int m, int n> struct Matx_FastInvOp
  66. {
  67. bool operator()(const Matx<_Tp, m, n>& a, Matx<_Tp, n, m>& b, int method) const
  68. {
  69. return invert(a, b, method) != 0;
  70. }
  71. };
  72. template<typename _Tp, int m> struct Matx_FastInvOp<_Tp, m, m>
  73. {
  74. bool operator()(const Matx<_Tp, m, m>& a, Matx<_Tp, m, m>& b, int method) const
  75. {
  76. if (method == DECOMP_LU || method == DECOMP_CHOLESKY)
  77. {
  78. Matx<_Tp, m, m> temp = a;
  79. // assume that b is all 0's on input => make it a unity matrix
  80. for (int i = 0; i < m; i++)
  81. b(i, i) = (_Tp)1;
  82. if (method == DECOMP_CHOLESKY)
  83. return Cholesky(temp.val, m*sizeof(_Tp), m, b.val, m*sizeof(_Tp), m);
  84. return LU(temp.val, m*sizeof(_Tp), m, b.val, m*sizeof(_Tp), m) != 0;
  85. }
  86. else
  87. {
  88. return invert(a, b, method) != 0;
  89. }
  90. }
  91. };
  92. template<typename _Tp> struct Matx_FastInvOp<_Tp, 2, 2>
  93. {
  94. bool operator()(const Matx<_Tp, 2, 2>& a, Matx<_Tp, 2, 2>& b, int /*method*/) const
  95. {
  96. _Tp d = (_Tp)determinant(a);
  97. if (d == 0)
  98. return false;
  99. d = 1/d;
  100. b(1,1) = a(0,0)*d;
  101. b(0,0) = a(1,1)*d;
  102. b(0,1) = -a(0,1)*d;
  103. b(1,0) = -a(1,0)*d;
  104. return true;
  105. }
  106. };
  107. template<typename _Tp> struct Matx_FastInvOp<_Tp, 3, 3>
  108. {
  109. bool operator()(const Matx<_Tp, 3, 3>& a, Matx<_Tp, 3, 3>& b, int /*method*/) const
  110. {
  111. _Tp d = (_Tp)determinant(a);
  112. if (d == 0)
  113. return false;
  114. d = 1/d;
  115. b(0,0) = (a(1,1) * a(2,2) - a(1,2) * a(2,1)) * d;
  116. b(0,1) = (a(0,2) * a(2,1) - a(0,1) * a(2,2)) * d;
  117. b(0,2) = (a(0,1) * a(1,2) - a(0,2) * a(1,1)) * d;
  118. b(1,0) = (a(1,2) * a(2,0) - a(1,0) * a(2,2)) * d;
  119. b(1,1) = (a(0,0) * a(2,2) - a(0,2) * a(2,0)) * d;
  120. b(1,2) = (a(0,2) * a(1,0) - a(0,0) * a(1,2)) * d;
  121. b(2,0) = (a(1,0) * a(2,1) - a(1,1) * a(2,0)) * d;
  122. b(2,1) = (a(0,1) * a(2,0) - a(0,0) * a(2,1)) * d;
  123. b(2,2) = (a(0,0) * a(1,1) - a(0,1) * a(1,0)) * d;
  124. return true;
  125. }
  126. };
  127. template<typename _Tp, int m, int l, int n> struct Matx_FastSolveOp
  128. {
  129. bool operator()(const Matx<_Tp, m, l>& a, const Matx<_Tp, m, n>& b,
  130. Matx<_Tp, l, n>& x, int method) const
  131. {
  132. return cv::solve(a, b, x, method);
  133. }
  134. };
  135. template<typename _Tp, int m, int n> struct Matx_FastSolveOp<_Tp, m, m, n>
  136. {
  137. bool operator()(const Matx<_Tp, m, m>& a, const Matx<_Tp, m, n>& b,
  138. Matx<_Tp, m, n>& x, int method) const
  139. {
  140. if (method == DECOMP_LU || method == DECOMP_CHOLESKY)
  141. {
  142. Matx<_Tp, m, m> temp = a;
  143. x = b;
  144. if( method == DECOMP_CHOLESKY )
  145. return Cholesky(temp.val, m*sizeof(_Tp), m, x.val, n*sizeof(_Tp), n);
  146. return LU(temp.val, m*sizeof(_Tp), m, x.val, n*sizeof(_Tp), n) != 0;
  147. }
  148. else
  149. {
  150. return cv::solve(a, b, x, method);
  151. }
  152. }
  153. };
  154. template<typename _Tp> struct Matx_FastSolveOp<_Tp, 2, 2, 1>
  155. {
  156. bool operator()(const Matx<_Tp, 2, 2>& a, const Matx<_Tp, 2, 1>& b,
  157. Matx<_Tp, 2, 1>& x, int) const
  158. {
  159. _Tp d = (_Tp)determinant(a);
  160. if (d == 0)
  161. return false;
  162. d = 1/d;
  163. x(0) = (b(0)*a(1,1) - b(1)*a(0,1))*d;
  164. x(1) = (b(1)*a(0,0) - b(0)*a(1,0))*d;
  165. return true;
  166. }
  167. };
  168. template<typename _Tp> struct Matx_FastSolveOp<_Tp, 3, 3, 1>
  169. {
  170. bool operator()(const Matx<_Tp, 3, 3>& a, const Matx<_Tp, 3, 1>& b,
  171. Matx<_Tp, 3, 1>& x, int) const
  172. {
  173. _Tp d = (_Tp)determinant(a);
  174. if (d == 0)
  175. return false;
  176. d = 1/d;
  177. x(0) = d*(b(0)*(a(1,1)*a(2,2) - a(1,2)*a(2,1)) -
  178. a(0,1)*(b(1)*a(2,2) - a(1,2)*b(2)) +
  179. a(0,2)*(b(1)*a(2,1) - a(1,1)*b(2)));
  180. x(1) = d*(a(0,0)*(b(1)*a(2,2) - a(1,2)*b(2)) -
  181. b(0)*(a(1,0)*a(2,2) - a(1,2)*a(2,0)) +
  182. a(0,2)*(a(1,0)*b(2) - b(1)*a(2,0)));
  183. x(2) = d*(a(0,0)*(a(1,1)*b(2) - b(1)*a(2,1)) -
  184. a(0,1)*(a(1,0)*b(2) - b(1)*a(2,0)) +
  185. b(0)*(a(1,0)*a(2,1) - a(1,1)*a(2,0)));
  186. return true;
  187. }
  188. };
  189. } // internal
  190. template<typename _Tp, int m, int n> inline
  191. Matx<_Tp,m,n> Matx<_Tp,m,n>::randu(_Tp a, _Tp b)
  192. {
  193. Matx<_Tp,m,n> M;
  194. cv::randu(M, Scalar(a), Scalar(b));
  195. return M;
  196. }
  197. template<typename _Tp, int m, int n> inline
  198. Matx<_Tp,m,n> Matx<_Tp,m,n>::randn(_Tp a, _Tp b)
  199. {
  200. Matx<_Tp,m,n> M;
  201. cv::randn(M, Scalar(a), Scalar(b));
  202. return M;
  203. }
  204. template<typename _Tp, int cn> inline
  205. Vec<_Tp, cn> Vec<_Tp, cn>::randu(_Tp a, _Tp b)
  206. {
  207. Vec<_Tp,cn> V;
  208. cv::randu(V, Scalar(a), Scalar(b));
  209. return V;
  210. }
  211. template<typename _Tp, int cn> inline
  212. Vec<_Tp, cn> Vec<_Tp, cn>::randn(_Tp a, _Tp b)
  213. {
  214. Vec<_Tp,cn> V;
  215. cv::randn(V, Scalar(a), Scalar(b));
  216. return V;
  217. }
  218. template<typename _Tp, int m, int n> inline
  219. Matx<_Tp, n, m> Matx<_Tp, m, n>::inv(int method, bool *p_is_ok /*= NULL*/) const
  220. {
  221. Matx<_Tp, n, m> b;
  222. bool ok = cv::internal::Matx_FastInvOp<_Tp, m, n>()(*this, b, method);
  223. if (p_is_ok) *p_is_ok = ok;
  224. return ok ? b : Matx<_Tp, n, m>::zeros();
  225. }
  226. template<typename _Tp, int m, int n> template<int l> inline
  227. Matx<_Tp, n, l> Matx<_Tp, m, n>::solve(const Matx<_Tp, m, l>& rhs, int method) const
  228. {
  229. Matx<_Tp, n, l> x;
  230. bool ok = cv::internal::Matx_FastSolveOp<_Tp, m, n, l>()(*this, rhs, x, method);
  231. return ok ? x : Matx<_Tp, n, l>::zeros();
  232. }
  233. ////////////////////////// Augmenting algebraic & logical operations //////////////////////////
  234. #define CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
  235. static inline A& operator op (A& a, const B& b) { cvop; return a; }
  236. #define CV_MAT_AUG_OPERATOR(op, cvop, A, B) \
  237. CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
  238. CV_MAT_AUG_OPERATOR1(op, cvop, const A, B)
  239. #define CV_MAT_AUG_OPERATOR_T(op, cvop, A, B) \
  240. template<typename _Tp> CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
  241. template<typename _Tp> CV_MAT_AUG_OPERATOR1(op, cvop, const A, B)
  242. #define CV_MAT_AUG_OPERATOR_TN(op, cvop, A) \
  243. template<typename _Tp, int m, int n> static inline A& operator op (A& a, const Matx<_Tp,m,n>& b) { cvop; return a; } \
  244. template<typename _Tp, int m, int n> static inline const A& operator op (const A& a, const Matx<_Tp,m,n>& b) { cvop; return a; }
  245. CV_MAT_AUG_OPERATOR (+=, cv::add(a, b, (const Mat&)a), Mat, Mat)
  246. CV_MAT_AUG_OPERATOR (+=, cv::add(a, b, (const Mat&)a), Mat, Scalar)
  247. CV_MAT_AUG_OPERATOR_T(+=, cv::add(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
  248. CV_MAT_AUG_OPERATOR_T(+=, cv::add(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
  249. CV_MAT_AUG_OPERATOR_T(+=, cv::add(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
  250. CV_MAT_AUG_OPERATOR_TN(+=, cv::add(a, Mat(b), (const Mat&)a), Mat)
  251. CV_MAT_AUG_OPERATOR_TN(+=, cv::add(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
  252. CV_MAT_AUG_OPERATOR (-=, cv::subtract(a, b, (const Mat&)a), Mat, Mat)
  253. CV_MAT_AUG_OPERATOR (-=, cv::subtract(a, b, (const Mat&)a), Mat, Scalar)
  254. CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
  255. CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
  256. CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
  257. CV_MAT_AUG_OPERATOR_TN(-=, cv::subtract(a, Mat(b), (const Mat&)a), Mat)
  258. CV_MAT_AUG_OPERATOR_TN(-=, cv::subtract(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
  259. CV_MAT_AUG_OPERATOR (*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat, Mat)
  260. CV_MAT_AUG_OPERATOR_T(*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat)
  261. CV_MAT_AUG_OPERATOR_T(*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat_<_Tp>)
  262. CV_MAT_AUG_OPERATOR (*=, a.convertTo(a, -1, b), Mat, double)
  263. CV_MAT_AUG_OPERATOR_T(*=, a.convertTo(a, -1, b), Mat_<_Tp>, double)
  264. CV_MAT_AUG_OPERATOR_TN(*=, cv::gemm(a, Mat(b), 1, Mat(), 0, a, 0), Mat)
  265. CV_MAT_AUG_OPERATOR_TN(*=, cv::gemm(a, Mat(b), 1, Mat(), 0, a, 0), Mat_<_Tp>)
  266. CV_MAT_AUG_OPERATOR (/=, cv::divide(a, b, (const Mat&)a), Mat, Mat)
  267. CV_MAT_AUG_OPERATOR_T(/=, cv::divide(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
  268. CV_MAT_AUG_OPERATOR_T(/=, cv::divide(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
  269. CV_MAT_AUG_OPERATOR (/=, a.convertTo((Mat&)a, -1, 1./b), Mat, double)
  270. CV_MAT_AUG_OPERATOR_T(/=, a.convertTo((Mat&)a, -1, 1./b), Mat_<_Tp>, double)
  271. CV_MAT_AUG_OPERATOR_TN(/=, cv::divide(a, Mat(b), (const Mat&)a), Mat)
  272. CV_MAT_AUG_OPERATOR_TN(/=, cv::divide(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
  273. CV_MAT_AUG_OPERATOR (&=, cv::bitwise_and(a, b, (const Mat&)a), Mat, Mat)
  274. CV_MAT_AUG_OPERATOR (&=, cv::bitwise_and(a, b, (const Mat&)a), Mat, Scalar)
  275. CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
  276. CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
  277. CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
  278. CV_MAT_AUG_OPERATOR_TN(&=, cv::bitwise_and(a, Mat(b), (const Mat&)a), Mat)
  279. CV_MAT_AUG_OPERATOR_TN(&=, cv::bitwise_and(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
  280. CV_MAT_AUG_OPERATOR (|=, cv::bitwise_or(a, b, (const Mat&)a), Mat, Mat)
  281. CV_MAT_AUG_OPERATOR (|=, cv::bitwise_or(a, b, (const Mat&)a), Mat, Scalar)
  282. CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
  283. CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
  284. CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
  285. CV_MAT_AUG_OPERATOR_TN(|=, cv::bitwise_or(a, Mat(b), (const Mat&)a), Mat)
  286. CV_MAT_AUG_OPERATOR_TN(|=, cv::bitwise_or(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
  287. CV_MAT_AUG_OPERATOR (^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat, Mat)
  288. CV_MAT_AUG_OPERATOR (^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat, Scalar)
  289. CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
  290. CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
  291. CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
  292. CV_MAT_AUG_OPERATOR_TN(^=, cv::bitwise_xor(a, Mat(b), (const Mat&)a), Mat)
  293. CV_MAT_AUG_OPERATOR_TN(^=, cv::bitwise_xor(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
  294. #undef CV_MAT_AUG_OPERATOR_TN
  295. #undef CV_MAT_AUG_OPERATOR_T
  296. #undef CV_MAT_AUG_OPERATOR
  297. #undef CV_MAT_AUG_OPERATOR1
  298. ///////////////////////////////////////////// SVD /////////////////////////////////////////////
  299. inline SVD::SVD() {}
  300. inline SVD::SVD( InputArray m, int flags ) { operator ()(m, flags); }
  301. inline void SVD::solveZ( InputArray m, OutputArray _dst )
  302. {
  303. Mat mtx = m.getMat();
  304. SVD svd(mtx, (mtx.rows >= mtx.cols ? 0 : SVD::FULL_UV));
  305. _dst.create(svd.vt.cols, 1, svd.vt.type());
  306. Mat dst = _dst.getMat();
  307. svd.vt.row(svd.vt.rows-1).reshape(1,svd.vt.cols).copyTo(dst);
  308. }
  309. template<typename _Tp, int m, int n, int nm> inline void
  310. SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w, Matx<_Tp, m, nm>& u, Matx<_Tp, n, nm>& vt )
  311. {
  312. CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
  313. Mat _a(a, false), _u(u, false), _w(w, false), _vt(vt, false);
  314. SVD::compute(_a, _w, _u, _vt);
  315. CV_Assert(_w.data == (uchar*)&w.val[0] && _u.data == (uchar*)&u.val[0] && _vt.data == (uchar*)&vt.val[0]);
  316. }
  317. template<typename _Tp, int m, int n, int nm> inline void
  318. SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w )
  319. {
  320. CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
  321. Mat _a(a, false), _w(w, false);
  322. SVD::compute(_a, _w);
  323. CV_Assert(_w.data == (uchar*)&w.val[0]);
  324. }
  325. template<typename _Tp, int m, int n, int nm, int nb> inline void
  326. SVD::backSubst( const Matx<_Tp, nm, 1>& w, const Matx<_Tp, m, nm>& u,
  327. const Matx<_Tp, n, nm>& vt, const Matx<_Tp, m, nb>& rhs,
  328. Matx<_Tp, n, nb>& dst )
  329. {
  330. CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
  331. Mat _u(u, false), _w(w, false), _vt(vt, false), _rhs(rhs, false), _dst(dst, false);
  332. SVD::backSubst(_w, _u, _vt, _rhs, _dst);
  333. CV_Assert(_dst.data == (uchar*)&dst.val[0]);
  334. }
  335. /////////////////////////////////// Multiply-with-Carry RNG ///////////////////////////////////
  336. inline RNG::RNG() { state = 0xffffffff; }
  337. inline RNG::RNG(uint64 _state) { state = _state ? _state : 0xffffffff; }
  338. inline RNG::operator uchar() { return (uchar)next(); }
  339. inline RNG::operator schar() { return (schar)next(); }
  340. inline RNG::operator ushort() { return (ushort)next(); }
  341. inline RNG::operator short() { return (short)next(); }
  342. inline RNG::operator int() { return (int)next(); }
  343. inline RNG::operator unsigned() { return next(); }
  344. inline RNG::operator float() { return next()*2.3283064365386962890625e-10f; }
  345. inline RNG::operator double() { unsigned t = next(); return (((uint64)t << 32) | next()) * 5.4210108624275221700372640043497e-20; }
  346. inline unsigned RNG::operator ()(unsigned N) { return (unsigned)uniform(0,N); }
  347. inline unsigned RNG::operator ()() { return next(); }
  348. inline int RNG::uniform(int a, int b) { return a == b ? a : (int)(next() % (b - a) + a); }
  349. inline float RNG::uniform(float a, float b) { return ((float)*this)*(b - a) + a; }
  350. inline double RNG::uniform(double a, double b) { return ((double)*this)*(b - a) + a; }
  351. inline bool RNG::operator ==(const RNG& other) const { return state == other.state; }
  352. inline unsigned RNG::next()
  353. {
  354. state = (uint64)(unsigned)state* /*CV_RNG_COEFF*/ 4164903690U + (unsigned)(state >> 32);
  355. return (unsigned)state;
  356. }
  357. //! returns the next uniformly-distributed random number of the specified type
  358. template<typename _Tp> static inline _Tp randu()
  359. {
  360. return (_Tp)theRNG();
  361. }
  362. ///////////////////////////////// Formatted output of cv::Mat /////////////////////////////////
  363. static inline
  364. Ptr<Formatted> format(InputArray mtx, Formatter::FormatType fmt)
  365. {
  366. return Formatter::get(fmt)->format(mtx.getMat());
  367. }
  368. static inline
  369. int print(Ptr<Formatted> fmtd, FILE* stream = stdout)
  370. {
  371. int written = 0;
  372. fmtd->reset();
  373. for(const char* str = fmtd->next(); str; str = fmtd->next())
  374. written += fputs(str, stream);
  375. return written;
  376. }
  377. static inline
  378. int print(const Mat& mtx, FILE* stream = stdout)
  379. {
  380. return print(Formatter::get()->format(mtx), stream);
  381. }
  382. static inline
  383. int print(const UMat& mtx, FILE* stream = stdout)
  384. {
  385. return print(Formatter::get()->format(mtx.getMat(ACCESS_READ)), stream);
  386. }
  387. template<typename _Tp> static inline
  388. int print(const std::vector<Point_<_Tp> >& vec, FILE* stream = stdout)
  389. {
  390. return print(Formatter::get()->format(Mat(vec)), stream);
  391. }
  392. template<typename _Tp> static inline
  393. int print(const std::vector<Point3_<_Tp> >& vec, FILE* stream = stdout)
  394. {
  395. return print(Formatter::get()->format(Mat(vec)), stream);
  396. }
  397. template<typename _Tp, int m, int n> static inline
  398. int print(const Matx<_Tp, m, n>& matx, FILE* stream = stdout)
  399. {
  400. return print(Formatter::get()->format(cv::Mat(matx)), stream);
  401. }
  402. //! @endcond
  403. ///////////////////////////////// Formatted string generation /////////////////////////////////
  404. /** @brief Returns a text string formatted using the printf-like expression.
  405. The function acts like sprintf but forms and returns an STL string. It can be used to form an error
  406. message in the Exception constructor.
  407. @param fmt printf-compatible formatting specifiers.
  408. **Note**:
  409. |Type|Specifier|
  410. |-|-|
  411. |`const char*`|`%s`|
  412. |`char`|`%c`|
  413. |`float` / `double`|`%f`,`%g`|
  414. |`int`, `long`, `long long`|`%d`, `%ld`, ``%lld`|
  415. |`unsigned`, `unsigned long`, `unsigned long long`|`%u`, `%lu`, `%llu`|
  416. |`uint64` -> `uintmax_t`, `int64` -> `intmax_t`|`%ju`, `%jd`|
  417. |`size_t`|`%zu`|
  418. @ingroup core_utils
  419. */
  420. CV_EXPORTS String format(const char* fmt, ...) CV_FORMAT_PRINTF(1, 2);
  421. /****************************************************************************************\
  422. * Auxiliary algorithms *
  423. \****************************************************************************************/
  424. /** @brief Splits an element set into equivalency classes.
  425. The generic function partition implements an \f$O(N^2)\f$ algorithm for splitting a set of \f$N\f$ elements
  426. into one or more equivalency classes, as described in
  427. <http://en.wikipedia.org/wiki/Disjoint-set_data_structure> . The function returns the number of
  428. equivalency classes.
  429. @param _vec Set of elements stored as a vector.
  430. @param labels Output vector of labels. It contains as many elements as vec. Each label labels[i] is
  431. a 0-based cluster index of `vec[i]`.
  432. @param predicate Equivalence predicate (pointer to a boolean function of two arguments or an
  433. instance of the class that has the method bool operator()(const _Tp& a, const _Tp& b) ). The
  434. predicate returns true when the elements are certainly in the same class, and returns false if they
  435. may or may not be in the same class.
  436. @ingroup core_cluster
  437. */
  438. template<typename _Tp, class _EqPredicate> int
  439. partition( const std::vector<_Tp>& _vec, std::vector<int>& labels,
  440. _EqPredicate predicate=_EqPredicate())
  441. {
  442. int i, j, N = (int)_vec.size();
  443. const _Tp* vec = &_vec[0];
  444. const int PARENT=0;
  445. const int RANK=1;
  446. std::vector<int> _nodes(N*2);
  447. int (*nodes)[2] = (int(*)[2])&_nodes[0];
  448. // The first O(N) pass: create N single-vertex trees
  449. for(i = 0; i < N; i++)
  450. {
  451. nodes[i][PARENT]=-1;
  452. nodes[i][RANK] = 0;
  453. }
  454. // The main O(N^2) pass: merge connected components
  455. for( i = 0; i < N; i++ )
  456. {
  457. int root = i;
  458. // find root
  459. while( nodes[root][PARENT] >= 0 )
  460. root = nodes[root][PARENT];
  461. for( j = 0; j < N; j++ )
  462. {
  463. if( i == j || !predicate(vec[i], vec[j]))
  464. continue;
  465. int root2 = j;
  466. while( nodes[root2][PARENT] >= 0 )
  467. root2 = nodes[root2][PARENT];
  468. if( root2 != root )
  469. {
  470. // unite both trees
  471. int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
  472. if( rank > rank2 )
  473. nodes[root2][PARENT] = root;
  474. else
  475. {
  476. nodes[root][PARENT] = root2;
  477. nodes[root2][RANK] += rank == rank2;
  478. root = root2;
  479. }
  480. CV_Assert( nodes[root][PARENT] < 0 );
  481. int k = j, parent;
  482. // compress the path from node2 to root
  483. while( (parent = nodes[k][PARENT]) >= 0 )
  484. {
  485. nodes[k][PARENT] = root;
  486. k = parent;
  487. }
  488. // compress the path from node to root
  489. k = i;
  490. while( (parent = nodes[k][PARENT]) >= 0 )
  491. {
  492. nodes[k][PARENT] = root;
  493. k = parent;
  494. }
  495. }
  496. }
  497. }
  498. // Final O(N) pass: enumerate classes
  499. labels.resize(N);
  500. int nclasses = 0;
  501. for( i = 0; i < N; i++ )
  502. {
  503. int root = i;
  504. while( nodes[root][PARENT] >= 0 )
  505. root = nodes[root][PARENT];
  506. // re-use the rank as the class label
  507. if( nodes[root][RANK] >= 0 )
  508. nodes[root][RANK] = ~nclasses++;
  509. labels[i] = ~nodes[root][RANK];
  510. }
  511. return nclasses;
  512. }
  513. } // cv
  514. #endif