operations.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. //! @cond IGNORED
  60. namespace cv
  61. {
  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 m, int n> inline
  205. Matx<_Tp, n, m> Matx<_Tp, m, n>::inv(int method, bool *p_is_ok /*= NULL*/) const
  206. {
  207. Matx<_Tp, n, m> b;
  208. bool ok = cv::internal::Matx_FastInvOp<_Tp, m, n>()(*this, b, method);
  209. if (p_is_ok) *p_is_ok = ok;
  210. return ok ? b : Matx<_Tp, n, m>::zeros();
  211. }
  212. template<typename _Tp, int m, int n> template<int l> inline
  213. Matx<_Tp, n, l> Matx<_Tp, m, n>::solve(const Matx<_Tp, m, l>& rhs, int method) const
  214. {
  215. Matx<_Tp, n, l> x;
  216. bool ok = cv::internal::Matx_FastSolveOp<_Tp, m, n, l>()(*this, rhs, x, method);
  217. return ok ? x : Matx<_Tp, n, l>::zeros();
  218. }
  219. ////////////////////////// Augmenting algebraic & logical operations //////////////////////////
  220. #define CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
  221. static inline A& operator op (A& a, const B& b) { cvop; return a; }
  222. #define CV_MAT_AUG_OPERATOR(op, cvop, A, B) \
  223. CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
  224. CV_MAT_AUG_OPERATOR1(op, cvop, const A, B)
  225. #define CV_MAT_AUG_OPERATOR_T(op, cvop, A, B) \
  226. template<typename _Tp> CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
  227. template<typename _Tp> CV_MAT_AUG_OPERATOR1(op, cvop, const A, B)
  228. #define CV_MAT_AUG_OPERATOR_TN(op, cvop, A) \
  229. template<typename _Tp, int m, int n> static inline A& operator op (A& a, const Matx<_Tp,m,n>& b) { cvop; return a; } \
  230. 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; }
  231. CV_MAT_AUG_OPERATOR (+=, cv::add(a,b,a), Mat, Mat)
  232. CV_MAT_AUG_OPERATOR (+=, cv::add(a,b,a), Mat, Scalar)
  233. CV_MAT_AUG_OPERATOR_T(+=, cv::add(a,b,a), Mat_<_Tp>, Mat)
  234. CV_MAT_AUG_OPERATOR_T(+=, cv::add(a,b,a), Mat_<_Tp>, Scalar)
  235. CV_MAT_AUG_OPERATOR_T(+=, cv::add(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
  236. CV_MAT_AUG_OPERATOR_TN(+=, cv::add(a,Mat(b),a), Mat)
  237. CV_MAT_AUG_OPERATOR_TN(+=, cv::add(a,Mat(b),a), Mat_<_Tp>)
  238. CV_MAT_AUG_OPERATOR (-=, cv::subtract(a,b,a), Mat, Mat)
  239. CV_MAT_AUG_OPERATOR (-=, cv::subtract(a,b,a), Mat, Scalar)
  240. CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a,b,a), Mat_<_Tp>, Mat)
  241. CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a,b,a), Mat_<_Tp>, Scalar)
  242. CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
  243. CV_MAT_AUG_OPERATOR_TN(-=, cv::subtract(a,Mat(b),a), Mat)
  244. CV_MAT_AUG_OPERATOR_TN(-=, cv::subtract(a,Mat(b),a), Mat_<_Tp>)
  245. CV_MAT_AUG_OPERATOR (*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat, Mat)
  246. CV_MAT_AUG_OPERATOR_T(*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat)
  247. CV_MAT_AUG_OPERATOR_T(*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat_<_Tp>)
  248. CV_MAT_AUG_OPERATOR (*=, a.convertTo(a, -1, b), Mat, double)
  249. CV_MAT_AUG_OPERATOR_T(*=, a.convertTo(a, -1, b), Mat_<_Tp>, double)
  250. CV_MAT_AUG_OPERATOR_TN(*=, cv::gemm(a, Mat(b), 1, Mat(), 0, a, 0), Mat)
  251. CV_MAT_AUG_OPERATOR_TN(*=, cv::gemm(a, Mat(b), 1, Mat(), 0, a, 0), Mat_<_Tp>)
  252. CV_MAT_AUG_OPERATOR (/=, cv::divide(a,b,a), Mat, Mat)
  253. CV_MAT_AUG_OPERATOR_T(/=, cv::divide(a,b,a), Mat_<_Tp>, Mat)
  254. CV_MAT_AUG_OPERATOR_T(/=, cv::divide(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
  255. CV_MAT_AUG_OPERATOR (/=, a.convertTo((Mat&)a, -1, 1./b), Mat, double)
  256. CV_MAT_AUG_OPERATOR_T(/=, a.convertTo((Mat&)a, -1, 1./b), Mat_<_Tp>, double)
  257. CV_MAT_AUG_OPERATOR_TN(/=, cv::divide(a, Mat(b), a), Mat)
  258. CV_MAT_AUG_OPERATOR_TN(/=, cv::divide(a, Mat(b), a), Mat_<_Tp>)
  259. CV_MAT_AUG_OPERATOR (&=, cv::bitwise_and(a,b,a), Mat, Mat)
  260. CV_MAT_AUG_OPERATOR (&=, cv::bitwise_and(a,b,a), Mat, Scalar)
  261. CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a,b,a), Mat_<_Tp>, Mat)
  262. CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a,b,a), Mat_<_Tp>, Scalar)
  263. CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
  264. CV_MAT_AUG_OPERATOR_TN(&=, cv::bitwise_and(a, Mat(b), a), Mat)
  265. CV_MAT_AUG_OPERATOR_TN(&=, cv::bitwise_and(a, Mat(b), a), Mat_<_Tp>)
  266. CV_MAT_AUG_OPERATOR (|=, cv::bitwise_or(a,b,a), Mat, Mat)
  267. CV_MAT_AUG_OPERATOR (|=, cv::bitwise_or(a,b,a), Mat, Scalar)
  268. CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a,b,a), Mat_<_Tp>, Mat)
  269. CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a,b,a), Mat_<_Tp>, Scalar)
  270. CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
  271. CV_MAT_AUG_OPERATOR_TN(|=, cv::bitwise_or(a, Mat(b), a), Mat)
  272. CV_MAT_AUG_OPERATOR_TN(|=, cv::bitwise_or(a, Mat(b), a), Mat_<_Tp>)
  273. CV_MAT_AUG_OPERATOR (^=, cv::bitwise_xor(a,b,a), Mat, Mat)
  274. CV_MAT_AUG_OPERATOR (^=, cv::bitwise_xor(a,b,a), Mat, Scalar)
  275. CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a,b,a), Mat_<_Tp>, Mat)
  276. CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a,b,a), Mat_<_Tp>, Scalar)
  277. CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
  278. CV_MAT_AUG_OPERATOR_TN(^=, cv::bitwise_xor(a, Mat(b), a), Mat)
  279. CV_MAT_AUG_OPERATOR_TN(^=, cv::bitwise_xor(a, Mat(b), a), Mat_<_Tp>)
  280. #undef CV_MAT_AUG_OPERATOR_TN
  281. #undef CV_MAT_AUG_OPERATOR_T
  282. #undef CV_MAT_AUG_OPERATOR
  283. #undef CV_MAT_AUG_OPERATOR1
  284. ///////////////////////////////////////////// SVD /////////////////////////////////////////////
  285. inline SVD::SVD() {}
  286. inline SVD::SVD( InputArray m, int flags ) { operator ()(m, flags); }
  287. inline void SVD::solveZ( InputArray m, OutputArray _dst )
  288. {
  289. Mat mtx = m.getMat();
  290. SVD svd(mtx, (mtx.rows >= mtx.cols ? 0 : SVD::FULL_UV));
  291. _dst.create(svd.vt.cols, 1, svd.vt.type());
  292. Mat dst = _dst.getMat();
  293. svd.vt.row(svd.vt.rows-1).reshape(1,svd.vt.cols).copyTo(dst);
  294. }
  295. template<typename _Tp, int m, int n, int nm> inline void
  296. SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w, Matx<_Tp, m, nm>& u, Matx<_Tp, n, nm>& vt )
  297. {
  298. CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
  299. Mat _a(a, false), _u(u, false), _w(w, false), _vt(vt, false);
  300. SVD::compute(_a, _w, _u, _vt);
  301. CV_Assert(_w.data == (uchar*)&w.val[0] && _u.data == (uchar*)&u.val[0] && _vt.data == (uchar*)&vt.val[0]);
  302. }
  303. template<typename _Tp, int m, int n, int nm> inline void
  304. SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w )
  305. {
  306. CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
  307. Mat _a(a, false), _w(w, false);
  308. SVD::compute(_a, _w);
  309. CV_Assert(_w.data == (uchar*)&w.val[0]);
  310. }
  311. template<typename _Tp, int m, int n, int nm, int nb> inline void
  312. SVD::backSubst( const Matx<_Tp, nm, 1>& w, const Matx<_Tp, m, nm>& u,
  313. const Matx<_Tp, n, nm>& vt, const Matx<_Tp, m, nb>& rhs,
  314. Matx<_Tp, n, nb>& dst )
  315. {
  316. CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
  317. Mat _u(u, false), _w(w, false), _vt(vt, false), _rhs(rhs, false), _dst(dst, false);
  318. SVD::backSubst(_w, _u, _vt, _rhs, _dst);
  319. CV_Assert(_dst.data == (uchar*)&dst.val[0]);
  320. }
  321. /////////////////////////////////// Multiply-with-Carry RNG ///////////////////////////////////
  322. inline RNG::RNG() { state = 0xffffffff; }
  323. inline RNG::RNG(uint64 _state) { state = _state ? _state : 0xffffffff; }
  324. inline RNG::operator uchar() { return (uchar)next(); }
  325. inline RNG::operator schar() { return (schar)next(); }
  326. inline RNG::operator ushort() { return (ushort)next(); }
  327. inline RNG::operator short() { return (short)next(); }
  328. inline RNG::operator int() { return (int)next(); }
  329. inline RNG::operator unsigned() { return next(); }
  330. inline RNG::operator float() { return next()*2.3283064365386962890625e-10f; }
  331. inline RNG::operator double() { unsigned t = next(); return (((uint64)t << 32) | next()) * 5.4210108624275221700372640043497e-20; }
  332. inline unsigned RNG::operator ()(unsigned N) { return (unsigned)uniform(0,N); }
  333. inline unsigned RNG::operator ()() { return next(); }
  334. inline int RNG::uniform(int a, int b) { return a == b ? a : (int)(next() % (b - a) + a); }
  335. inline float RNG::uniform(float a, float b) { return ((float)*this)*(b - a) + a; }
  336. inline double RNG::uniform(double a, double b) { return ((double)*this)*(b - a) + a; }
  337. inline bool RNG::operator ==(const RNG& other) const { return state == other.state; }
  338. inline unsigned RNG::next()
  339. {
  340. state = (uint64)(unsigned)state* /*CV_RNG_COEFF*/ 4164903690U + (unsigned)(state >> 32);
  341. return (unsigned)state;
  342. }
  343. //! returns the next uniformly-distributed random number of the specified type
  344. template<typename _Tp> static inline _Tp randu()
  345. {
  346. return (_Tp)theRNG();
  347. }
  348. ///////////////////////////////// Formatted string generation /////////////////////////////////
  349. /** @brief Returns a text string formatted using the printf-like expression.
  350. The function acts like sprintf but forms and returns an STL string. It can be used to form an error
  351. message in the Exception constructor.
  352. @param fmt printf-compatible formatting specifiers.
  353. **Note**:
  354. |Type|Specifier|
  355. |-|-|
  356. |`const char*`|`%s`|
  357. |`char`|`%c`|
  358. |`float` / `double`|`%f`,`%g`|
  359. |`int`, `long`, `long long`|`%d`, `%ld`, ``%lld`|
  360. |`unsigned`, `unsigned long`, `unsigned long long`|`%u`, `%lu`, `%llu`|
  361. |`uint64` -> `uintmax_t`, `int64` -> `intmax_t`|`%ju`, `%jd`|
  362. |`size_t`|`%zu`|
  363. */
  364. CV_EXPORTS String format( const char* fmt, ... ) CV_FORMAT_PRINTF(1, 2);
  365. ///////////////////////////////// Formatted output of cv::Mat /////////////////////////////////
  366. static inline
  367. Ptr<Formatted> format(InputArray mtx, Formatter::FormatType fmt)
  368. {
  369. return Formatter::get(fmt)->format(mtx.getMat());
  370. }
  371. static inline
  372. int print(Ptr<Formatted> fmtd, FILE* stream = stdout)
  373. {
  374. int written = 0;
  375. fmtd->reset();
  376. for(const char* str = fmtd->next(); str; str = fmtd->next())
  377. written += fputs(str, stream);
  378. return written;
  379. }
  380. static inline
  381. int print(const Mat& mtx, FILE* stream = stdout)
  382. {
  383. return print(Formatter::get()->format(mtx), stream);
  384. }
  385. static inline
  386. int print(const UMat& mtx, FILE* stream = stdout)
  387. {
  388. return print(Formatter::get()->format(mtx.getMat(ACCESS_READ)), stream);
  389. }
  390. template<typename _Tp> static inline
  391. int print(const std::vector<Point_<_Tp> >& vec, FILE* stream = stdout)
  392. {
  393. return print(Formatter::get()->format(Mat(vec)), stream);
  394. }
  395. template<typename _Tp> static inline
  396. int print(const std::vector<Point3_<_Tp> >& vec, FILE* stream = stdout)
  397. {
  398. return print(Formatter::get()->format(Mat(vec)), stream);
  399. }
  400. template<typename _Tp, int m, int n> static inline
  401. int print(const Matx<_Tp, m, n>& matx, FILE* stream = stdout)
  402. {
  403. return print(Formatter::get()->format(cv::Mat(matx)), stream);
  404. }
  405. //! @endcond
  406. /****************************************************************************************\
  407. * Auxiliary algorithms *
  408. \****************************************************************************************/
  409. /** @brief Splits an element set into equivalency classes.
  410. The generic function partition implements an \f$O(N^2)\f$ algorithm for splitting a set of \f$N\f$ elements
  411. into one or more equivalency classes, as described in
  412. <http://en.wikipedia.org/wiki/Disjoint-set_data_structure> . The function returns the number of
  413. equivalency classes.
  414. @param _vec Set of elements stored as a vector.
  415. @param labels Output vector of labels. It contains as many elements as vec. Each label labels[i] is
  416. a 0-based cluster index of `vec[i]`.
  417. @param predicate Equivalence predicate (pointer to a boolean function of two arguments or an
  418. instance of the class that has the method bool operator()(const _Tp& a, const _Tp& b) ). The
  419. predicate returns true when the elements are certainly in the same class, and returns false if they
  420. may or may not be in the same class.
  421. @ingroup core_cluster
  422. */
  423. template<typename _Tp, class _EqPredicate> int
  424. partition( const std::vector<_Tp>& _vec, std::vector<int>& labels,
  425. _EqPredicate predicate=_EqPredicate())
  426. {
  427. int i, j, N = (int)_vec.size();
  428. const _Tp* vec = &_vec[0];
  429. const int PARENT=0;
  430. const int RANK=1;
  431. std::vector<int> _nodes(N*2);
  432. int (*nodes)[2] = (int(*)[2])&_nodes[0];
  433. // The first O(N) pass: create N single-vertex trees
  434. for(i = 0; i < N; i++)
  435. {
  436. nodes[i][PARENT]=-1;
  437. nodes[i][RANK] = 0;
  438. }
  439. // The main O(N^2) pass: merge connected components
  440. for( i = 0; i < N; i++ )
  441. {
  442. int root = i;
  443. // find root
  444. while( nodes[root][PARENT] >= 0 )
  445. root = nodes[root][PARENT];
  446. for( j = 0; j < N; j++ )
  447. {
  448. if( i == j || !predicate(vec[i], vec[j]))
  449. continue;
  450. int root2 = j;
  451. while( nodes[root2][PARENT] >= 0 )
  452. root2 = nodes[root2][PARENT];
  453. if( root2 != root )
  454. {
  455. // unite both trees
  456. int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
  457. if( rank > rank2 )
  458. nodes[root2][PARENT] = root;
  459. else
  460. {
  461. nodes[root][PARENT] = root2;
  462. nodes[root2][RANK] += rank == rank2;
  463. root = root2;
  464. }
  465. CV_Assert( nodes[root][PARENT] < 0 );
  466. int k = j, parent;
  467. // compress the path from node2 to root
  468. while( (parent = nodes[k][PARENT]) >= 0 )
  469. {
  470. nodes[k][PARENT] = root;
  471. k = parent;
  472. }
  473. // compress the path from node to root
  474. k = i;
  475. while( (parent = nodes[k][PARENT]) >= 0 )
  476. {
  477. nodes[k][PARENT] = root;
  478. k = parent;
  479. }
  480. }
  481. }
  482. }
  483. // Final O(N) pass: enumerate classes
  484. labels.resize(N);
  485. int nclasses = 0;
  486. for( i = 0; i < N; i++ )
  487. {
  488. int root = i;
  489. while( nodes[root][PARENT] >= 0 )
  490. root = nodes[root][PARENT];
  491. // re-use the rank as the class label
  492. if( nodes[root][RANK] >= 0 )
  493. nodes[root][RANK] = ~nclasses++;
  494. labels[i] = ~nodes[root][RANK];
  495. }
  496. return nclasses;
  497. }
  498. } // cv
  499. #endif