operations.hpp 21 KB

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