EstervQrCode 2.0.0
Library for qr code manipulation
Loading...
Searching...
No Matches
operations.hpp
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
45#ifndef OPENCV_CORE_OPERATIONS_HPP
46#define OPENCV_CORE_OPERATIONS_HPP
47
48#ifndef __cplusplus
49# error operations.hpp header must be compiled as C++
50#endif
51
52#include <cstdio>
53
54#if defined(__GNUC__) || defined(__clang__) // at least GCC 3.1+, clang 3.5+
55# if defined(__MINGW_PRINTF_FORMAT) // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.
56# define CV_FORMAT_PRINTF(string_idx, first_to_check) __attribute__ ((format (__MINGW_PRINTF_FORMAT, string_idx, first_to_check)))
57# else
58# define CV_FORMAT_PRINTF(string_idx, first_to_check) __attribute__ ((format (printf, string_idx, first_to_check)))
59# endif
60#else
61# define CV_FORMAT_PRINTF(A, B)
62#endif
63
64namespace cv
65{
67
68
70
71namespace internal
72{
73
74template<typename _Tp, int m, int n> struct Matx_FastInvOp
75{
76 bool operator()(const Matx<_Tp, m, n>& a, Matx<_Tp, n, m>& b, int method) const
77 {
78 return invert(a, b, method) != 0;
79 }
80};
81
82template<typename _Tp, int m> struct Matx_FastInvOp<_Tp, m, m>
83{
84 bool operator()(const Matx<_Tp, m, m>& a, Matx<_Tp, m, m>& b, int method) const
85 {
86 if (method == DECOMP_LU || method == DECOMP_CHOLESKY)
87 {
88 Matx<_Tp, m, m> temp = a;
89
90 // assume that b is all 0's on input => make it a unity matrix
91 for (int i = 0; i < m; i++)
92 b(i, i) = (_Tp)1;
93
94 if (method == DECOMP_CHOLESKY)
95 return Cholesky(temp.val, m*sizeof(_Tp), m, b.val, m*sizeof(_Tp), m);
96
97 return LU(temp.val, m*sizeof(_Tp), m, b.val, m*sizeof(_Tp), m) != 0;
98 }
99 else
100 {
101 return invert(a, b, method) != 0;
102 }
103 }
104};
105
106template<typename _Tp> struct Matx_FastInvOp<_Tp, 2, 2>
107{
108 bool operator()(const Matx<_Tp, 2, 2>& a, Matx<_Tp, 2, 2>& b, int /*method*/) const
109 {
110 _Tp d = (_Tp)determinant(a);
111 if (d == 0)
112 return false;
113 d = 1/d;
114 b(1,1) = a(0,0)*d;
115 b(0,0) = a(1,1)*d;
116 b(0,1) = -a(0,1)*d;
117 b(1,0) = -a(1,0)*d;
118 return true;
119 }
120};
121
122template<typename _Tp> struct Matx_FastInvOp<_Tp, 3, 3>
123{
124 bool operator()(const Matx<_Tp, 3, 3>& a, Matx<_Tp, 3, 3>& b, int /*method*/) const
125 {
126 _Tp d = (_Tp)determinant(a);
127 if (d == 0)
128 return false;
129 d = 1/d;
130 b(0,0) = (a(1,1) * a(2,2) - a(1,2) * a(2,1)) * d;
131 b(0,1) = (a(0,2) * a(2,1) - a(0,1) * a(2,2)) * d;
132 b(0,2) = (a(0,1) * a(1,2) - a(0,2) * a(1,1)) * d;
133
134 b(1,0) = (a(1,2) * a(2,0) - a(1,0) * a(2,2)) * d;
135 b(1,1) = (a(0,0) * a(2,2) - a(0,2) * a(2,0)) * d;
136 b(1,2) = (a(0,2) * a(1,0) - a(0,0) * a(1,2)) * d;
137
138 b(2,0) = (a(1,0) * a(2,1) - a(1,1) * a(2,0)) * d;
139 b(2,1) = (a(0,1) * a(2,0) - a(0,0) * a(2,1)) * d;
140 b(2,2) = (a(0,0) * a(1,1) - a(0,1) * a(1,0)) * d;
141 return true;
142 }
143};
144
145
146template<typename _Tp, int m, int l, int n> struct Matx_FastSolveOp
147{
148 bool operator()(const Matx<_Tp, m, l>& a, const Matx<_Tp, m, n>& b,
149 Matx<_Tp, l, n>& x, int method) const
150 {
151 return cv::solve(a, b, x, method);
152 }
153};
154
155template<typename _Tp, int m, int n> struct Matx_FastSolveOp<_Tp, m, m, n>
156{
157 bool operator()(const Matx<_Tp, m, m>& a, const Matx<_Tp, m, n>& b,
158 Matx<_Tp, m, n>& x, int method) const
159 {
160 if (method == DECOMP_LU || method == DECOMP_CHOLESKY)
161 {
162 Matx<_Tp, m, m> temp = a;
163 x = b;
164 if( method == DECOMP_CHOLESKY )
165 return Cholesky(temp.val, m*sizeof(_Tp), m, x.val, n*sizeof(_Tp), n);
166
167 return LU(temp.val, m*sizeof(_Tp), m, x.val, n*sizeof(_Tp), n) != 0;
168 }
169 else
170 {
171 return cv::solve(a, b, x, method);
172 }
173 }
174};
175
176template<typename _Tp> struct Matx_FastSolveOp<_Tp, 2, 2, 1>
177{
178 bool operator()(const Matx<_Tp, 2, 2>& a, const Matx<_Tp, 2, 1>& b,
179 Matx<_Tp, 2, 1>& x, int) const
180 {
181 _Tp d = (_Tp)determinant(a);
182 if (d == 0)
183 return false;
184 d = 1/d;
185 x(0) = (b(0)*a(1,1) - b(1)*a(0,1))*d;
186 x(1) = (b(1)*a(0,0) - b(0)*a(1,0))*d;
187 return true;
188 }
189};
190
191template<typename _Tp> struct Matx_FastSolveOp<_Tp, 3, 3, 1>
192{
193 bool operator()(const Matx<_Tp, 3, 3>& a, const Matx<_Tp, 3, 1>& b,
194 Matx<_Tp, 3, 1>& x, int) const
195 {
196 _Tp d = (_Tp)determinant(a);
197 if (d == 0)
198 return false;
199 d = 1/d;
200 x(0) = d*(b(0)*(a(1,1)*a(2,2) - a(1,2)*a(2,1)) -
201 a(0,1)*(b(1)*a(2,2) - a(1,2)*b(2)) +
202 a(0,2)*(b(1)*a(2,1) - a(1,1)*b(2)));
203
204 x(1) = d*(a(0,0)*(b(1)*a(2,2) - a(1,2)*b(2)) -
205 b(0)*(a(1,0)*a(2,2) - a(1,2)*a(2,0)) +
206 a(0,2)*(a(1,0)*b(2) - b(1)*a(2,0)));
207
208 x(2) = d*(a(0,0)*(a(1,1)*b(2) - b(1)*a(2,1)) -
209 a(0,1)*(a(1,0)*b(2) - b(1)*a(2,0)) +
210 b(0)*(a(1,0)*a(2,1) - a(1,1)*a(2,0)));
211 return true;
212 }
213};
214
215} // internal
216
217template<typename _Tp, int m, int n> inline
218Matx<_Tp,m,n> Matx<_Tp,m,n>::randu(_Tp a, _Tp b)
219{
220 Matx<_Tp,m,n> M;
221 cv::randu(M, Scalar(a), Scalar(b));
222 return M;
223}
224
225template<typename _Tp, int m, int n> inline
226Matx<_Tp,m,n> Matx<_Tp,m,n>::randn(_Tp a, _Tp b)
227{
228 Matx<_Tp,m,n> M;
229 cv::randn(M, Scalar(a), Scalar(b));
230 return M;
231}
232
233template<typename _Tp, int cn> inline
234Vec<_Tp, cn> Vec<_Tp, cn>::randu(_Tp a, _Tp b)
235{
236 Vec<_Tp,cn> V;
237 cv::randu(V, Scalar(a), Scalar(b));
238 return V;
239}
240
241template<typename _Tp, int cn> inline
242Vec<_Tp, cn> Vec<_Tp, cn>::randn(_Tp a, _Tp b)
243{
244 Vec<_Tp,cn> V;
245 cv::randn(V, Scalar(a), Scalar(b));
246 return V;
247}
248
249template<typename _Tp, int m, int n> inline
250Matx<_Tp, n, m> Matx<_Tp, m, n>::inv(int method, bool *p_is_ok /*= NULL*/) const
251{
252 Matx<_Tp, n, m> b;
253 bool ok = cv::internal::Matx_FastInvOp<_Tp, m, n>()(*this, b, method);
254 if (p_is_ok) *p_is_ok = ok;
255 return ok ? b : Matx<_Tp, n, m>::zeros();
256}
257
258template<typename _Tp, int m, int n> template<int l> inline
259Matx<_Tp, n, l> Matx<_Tp, m, n>::solve(const Matx<_Tp, m, l>& rhs, int method) const
260{
261 Matx<_Tp, n, l> x;
262 bool ok = cv::internal::Matx_FastSolveOp<_Tp, m, n, l>()(*this, rhs, x, method);
263 return ok ? x : Matx<_Tp, n, l>::zeros();
264}
265
266
267
269
270#define CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
271 static inline A& operator op (A& a, const B& b) { cvop; return a; }
272
273#define CV_MAT_AUG_OPERATOR(op, cvop, A, B) \
274 CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
275 CV_MAT_AUG_OPERATOR1(op, cvop, const A, B)
276
277#define CV_MAT_AUG_OPERATOR_T(op, cvop, A, B) \
278 template<typename _Tp> CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
279 template<typename _Tp> CV_MAT_AUG_OPERATOR1(op, cvop, const A, B)
280
281#define CV_MAT_AUG_OPERATOR_TN(op, cvop, A) \
282 template<typename _Tp, int m, int n> static inline A& operator op (A& a, const Matx<_Tp,m,n>& b) { cvop; return a; } \
283 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; }
284
285CV_MAT_AUG_OPERATOR (+=, cv::add(a, b, (const Mat&)a), Mat, Mat)
286CV_MAT_AUG_OPERATOR (+=, cv::add(a, b, (const Mat&)a), Mat, Scalar)
287CV_MAT_AUG_OPERATOR_T(+=, cv::add(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
288CV_MAT_AUG_OPERATOR_T(+=, cv::add(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
289CV_MAT_AUG_OPERATOR_T(+=, cv::add(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
290CV_MAT_AUG_OPERATOR_TN(+=, cv::add(a, Mat(b), (const Mat&)a), Mat)
291CV_MAT_AUG_OPERATOR_TN(+=, cv::add(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
292
293CV_MAT_AUG_OPERATOR (-=, cv::subtract(a, b, (const Mat&)a), Mat, Mat)
294CV_MAT_AUG_OPERATOR (-=, cv::subtract(a, b, (const Mat&)a), Mat, Scalar)
295CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
296CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
297CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
298CV_MAT_AUG_OPERATOR_TN(-=, cv::subtract(a, Mat(b), (const Mat&)a), Mat)
299CV_MAT_AUG_OPERATOR_TN(-=, cv::subtract(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
300
301CV_MAT_AUG_OPERATOR (*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat, Mat)
302CV_MAT_AUG_OPERATOR_T(*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat)
303CV_MAT_AUG_OPERATOR_T(*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat_<_Tp>)
304CV_MAT_AUG_OPERATOR (*=, a.convertTo(a, -1, b), Mat, double)
305CV_MAT_AUG_OPERATOR_T(*=, a.convertTo(a, -1, b), Mat_<_Tp>, double)
306CV_MAT_AUG_OPERATOR_TN(*=, cv::gemm(a, Mat(b), 1, Mat(), 0, a, 0), Mat)
307CV_MAT_AUG_OPERATOR_TN(*=, cv::gemm(a, Mat(b), 1, Mat(), 0, a, 0), Mat_<_Tp>)
308
309CV_MAT_AUG_OPERATOR (/=, cv::divide(a, b, (const Mat&)a), Mat, Mat)
310CV_MAT_AUG_OPERATOR_T(/=, cv::divide(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
311CV_MAT_AUG_OPERATOR_T(/=, cv::divide(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
312CV_MAT_AUG_OPERATOR (/=, a.convertTo((Mat&)a, -1, 1./b), Mat, double)
313CV_MAT_AUG_OPERATOR_T(/=, a.convertTo((Mat&)a, -1, 1./b), Mat_<_Tp>, double)
314CV_MAT_AUG_OPERATOR_TN(/=, cv::divide(a, Mat(b), (const Mat&)a), Mat)
315CV_MAT_AUG_OPERATOR_TN(/=, cv::divide(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
316
317CV_MAT_AUG_OPERATOR (&=, cv::bitwise_and(a, b, (const Mat&)a), Mat, Mat)
318CV_MAT_AUG_OPERATOR (&=, cv::bitwise_and(a, b, (const Mat&)a), Mat, Scalar)
319CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
320CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
321CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
322CV_MAT_AUG_OPERATOR_TN(&=, cv::bitwise_and(a, Mat(b), (const Mat&)a), Mat)
323CV_MAT_AUG_OPERATOR_TN(&=, cv::bitwise_and(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
324
325CV_MAT_AUG_OPERATOR (|=, cv::bitwise_or(a, b, (const Mat&)a), Mat, Mat)
326CV_MAT_AUG_OPERATOR (|=, cv::bitwise_or(a, b, (const Mat&)a), Mat, Scalar)
327CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
328CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
329CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
330CV_MAT_AUG_OPERATOR_TN(|=, cv::bitwise_or(a, Mat(b), (const Mat&)a), Mat)
331CV_MAT_AUG_OPERATOR_TN(|=, cv::bitwise_or(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
332
333CV_MAT_AUG_OPERATOR (^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat, Mat)
334CV_MAT_AUG_OPERATOR (^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat, Scalar)
335CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
336CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
337CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
338CV_MAT_AUG_OPERATOR_TN(^=, cv::bitwise_xor(a, Mat(b), (const Mat&)a), Mat)
339CV_MAT_AUG_OPERATOR_TN(^=, cv::bitwise_xor(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
340
341#undef CV_MAT_AUG_OPERATOR_TN
342#undef CV_MAT_AUG_OPERATOR_T
343#undef CV_MAT_AUG_OPERATOR
344#undef CV_MAT_AUG_OPERATOR1
345
346
347
349
350inline SVD::SVD() {}
351inline SVD::SVD( InputArray m, int flags ) { operator ()(m, flags); }
352inline void SVD::solveZ( InputArray m, OutputArray _dst )
353{
354 Mat mtx = m.getMat();
355 SVD svd(mtx, (mtx.rows >= mtx.cols ? 0 : SVD::FULL_UV));
356 _dst.create(svd.vt.cols, 1, svd.vt.type());
357 Mat dst = _dst.getMat();
358 svd.vt.row(svd.vt.rows-1).reshape(1,svd.vt.cols).copyTo(dst);
359}
360
361template<typename _Tp, int m, int n, int nm> inline void
362 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w, Matx<_Tp, m, nm>& u, Matx<_Tp, n, nm>& vt )
363{
364 CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
365 Mat _a(a, false), _u(u, false), _w(w, false), _vt(vt, false);
366 SVD::compute(_a, _w, _u, _vt);
367 CV_Assert(_w.data == (uchar*)&w.val[0] && _u.data == (uchar*)&u.val[0] && _vt.data == (uchar*)&vt.val[0]);
368}
369
370template<typename _Tp, int m, int n, int nm> inline void
371SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w )
372{
373 CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
374 Mat _a(a, false), _w(w, false);
375 SVD::compute(_a, _w);
376 CV_Assert(_w.data == (uchar*)&w.val[0]);
377}
378
379template<typename _Tp, int m, int n, int nm, int nb> inline void
380SVD::backSubst( const Matx<_Tp, nm, 1>& w, const Matx<_Tp, m, nm>& u,
381 const Matx<_Tp, n, nm>& vt, const Matx<_Tp, m, nb>& rhs,
382 Matx<_Tp, n, nb>& dst )
383{
384 CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
385 Mat _u(u, false), _w(w, false), _vt(vt, false), _rhs(rhs, false), _dst(dst, false);
386 SVD::backSubst(_w, _u, _vt, _rhs, _dst);
387 CV_Assert(_dst.data == (uchar*)&dst.val[0]);
388}
389
390
391
393
394inline RNG::RNG() { state = 0xffffffff; }
395inline RNG::RNG(uint64 _state) { state = _state ? _state : 0xffffffff; }
396
397inline RNG::operator uchar() { return (uchar)next(); }
398inline RNG::operator schar() { return (schar)next(); }
399inline RNG::operator ushort() { return (ushort)next(); }
400inline RNG::operator short() { return (short)next(); }
401inline RNG::operator int() { return (int)next(); }
402inline RNG::operator unsigned() { return next(); }
403inline RNG::operator float() { return next()*2.3283064365386962890625e-10f; }
404inline RNG::operator double() { unsigned t = next(); return (((uint64)t << 32) | next()) * 5.4210108624275221700372640043497e-20; }
405
406inline unsigned RNG::operator ()(unsigned N) { return (unsigned)uniform(0,N); }
407inline unsigned RNG::operator ()() { return next(); }
408
409inline int RNG::uniform(int a, int b) { return a == b ? a : (int)(next() % (b - a) + a); }
410inline float RNG::uniform(float a, float b) { return ((float)*this)*(b - a) + a; }
411inline double RNG::uniform(double a, double b) { return ((double)*this)*(b - a) + a; }
412
413inline bool RNG::operator ==(const RNG& other) const { return state == other.state; }
414
415inline unsigned RNG::next()
416{
417 state = (uint64)(unsigned)state* /*CV_RNG_COEFF*/ 4164903690U + (unsigned)(state >> 32);
418 return (unsigned)state;
419}
420
422template<typename _Tp> static inline _Tp randu()
423{
424 return (_Tp)theRNG();
425}
426
427
429
430static inline
431Ptr<Formatted> format(InputArray mtx, Formatter::FormatType fmt)
432{
433 return Formatter::get(fmt)->format(mtx.getMat());
434}
435
436static inline
437int print(Ptr<Formatted> fmtd, FILE* stream = stdout)
438{
439 int written = 0;
440 fmtd->reset();
441 for(const char* str = fmtd->next(); str; str = fmtd->next())
442 written += fputs(str, stream);
443
444 return written;
445}
446
447static inline
448int print(const Mat& mtx, FILE* stream = stdout)
449{
450 return print(Formatter::get()->format(mtx), stream);
451}
452
453static inline
454int print(const UMat& mtx, FILE* stream = stdout)
455{
456 return print(Formatter::get()->format(mtx.getMat(ACCESS_READ)), stream);
457}
458
459template<typename _Tp> static inline
460int print(const std::vector<Point_<_Tp> >& vec, FILE* stream = stdout)
461{
462 return print(Formatter::get()->format(Mat(vec)), stream);
463}
464
465template<typename _Tp> static inline
466int print(const std::vector<Point3_<_Tp> >& vec, FILE* stream = stdout)
467{
468 return print(Formatter::get()->format(Mat(vec)), stream);
469}
470
471template<typename _Tp, int m, int n> static inline
472int print(const Matx<_Tp, m, n>& matx, FILE* stream = stdout)
473{
474 return print(Formatter::get()->format(cv::Mat(matx)), stream);
475}
476
478
480
499CV_EXPORTS String format(const char* fmt, ...) CV_FORMAT_PRINTF(1, 2);
500
501/****************************************************************************************\
502* Auxiliary algorithms *
503\****************************************************************************************/
504
520template<typename _Tp, class _EqPredicate> int
521partition( const std::vector<_Tp>& _vec, std::vector<int>& labels,
522 _EqPredicate predicate=_EqPredicate())
523{
524 int i, j, N = (int)_vec.size();
525 const _Tp* vec = &_vec[0];
526
527 const int PARENT=0;
528 const int RANK=1;
529
530 std::vector<int> _nodes(N*2);
531 int (*nodes)[2] = (int(*)[2])&_nodes[0];
532
533 // The first O(N) pass: create N single-vertex trees
534 for(i = 0; i < N; i++)
535 {
536 nodes[i][PARENT]=-1;
537 nodes[i][RANK] = 0;
538 }
539
540 // The main O(N^2) pass: merge connected components
541 for( i = 0; i < N; i++ )
542 {
543 int root = i;
544
545 // find root
546 while( nodes[root][PARENT] >= 0 )
547 root = nodes[root][PARENT];
548
549 for( j = 0; j < N; j++ )
550 {
551 if( i == j || !predicate(vec[i], vec[j]))
552 continue;
553 int root2 = j;
554
555 while( nodes[root2][PARENT] >= 0 )
556 root2 = nodes[root2][PARENT];
557
558 if( root2 != root )
559 {
560 // unite both trees
561 int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
562 if( rank > rank2 )
563 nodes[root2][PARENT] = root;
564 else
565 {
566 nodes[root][PARENT] = root2;
567 nodes[root2][RANK] += rank == rank2;
568 root = root2;
569 }
570 CV_Assert( nodes[root][PARENT] < 0 );
571
572 int k = j, parent;
573
574 // compress the path from node2 to root
575 while( (parent = nodes[k][PARENT]) >= 0 )
576 {
577 nodes[k][PARENT] = root;
578 k = parent;
579 }
580
581 // compress the path from node to root
582 k = i;
583 while( (parent = nodes[k][PARENT]) >= 0 )
584 {
585 nodes[k][PARENT] = root;
586 k = parent;
587 }
588 }
589 }
590 }
591
592 // Final O(N) pass: enumerate classes
593 labels.resize(N);
594 int nclasses = 0;
595
596 for( i = 0; i < N; i++ )
597 {
598 int root = i;
599 while( nodes[root][PARENT] >= 0 )
600 root = nodes[root][PARENT];
601 // re-use the rank as the class label
602 if( nodes[root][RANK] >= 0 )
603 nodes[root][RANK] = ~nclasses++;
604 labels[i] = ~nodes[root][RANK];
605 }
606
607 return nclasses;
608}
609
610} // cv
611
612#endif
static Ptr< Formatter > get(Formatter::FormatType fmt=FMT_DEFAULT)
FormatType
Definition core.hpp:3141
n-dimensional dense array class
Definition mat.hpp:812
void copyTo(OutputArray m) const
Copies the matrix to another one.
Mat row(int y) const
Creates a matrix header for the specified matrix row.
uchar * data
pointer to the data
Definition mat.hpp:2140
Mat reshape(int cn, int rows=0) const
Changes the shape and/or the number of channels of a 2D matrix without copying the data.
Matx< _Tp, n, m > inv(int method=DECOMP_LU, bool *p_is_ok=NULL) const
invert the matrix
static CV_NODISCARD_STD Matx randn(_Tp a, _Tp b)
Generates normally distributed random numbers.
static CV_NODISCARD_STD Matx zeros()
Definition matx.inl.hpp:307
Matx< _Tp, n, l > solve(const Matx< _Tp, m, l > &rhs, int flags=DECOMP_LU) const
solve linear system
static CV_NODISCARD_STD Matx randu(_Tp a, _Tp b)
Generates uniformly distributed random numbers.
RNG()
constructor
unsigned operator()()
returns a random integer sampled uniformly from [0, N).
bool operator==(const RNG &other) const
uint64 state
Definition core.hpp:3035
int uniform(int a, int b)
returns uniformly distributed integer random number from [a,b) range
unsigned next()
SVD()
the default constructor
Mat w
Definition core.hpp:2873
@ FULL_UV
Definition core.hpp:2762
static void compute(InputArray src, OutputArray w, OutputArray u, OutputArray vt, int flags=0)
decomposes matrix and stores the results to user-provided matrices
static void backSubst(InputArray w, InputArray u, InputArray vt, InputArray rhs, OutputArray dst)
performs back substitution
Mat vt
Definition core.hpp:2873
static void solveZ(InputArray src, OutputArray dst)
solves an under-determined singular linear system
Mat u
Definition core.hpp:2873
SVD & operator()(InputArray src, int flags=0)
the operator that performs SVD. The previously allocated u, w and vt are released.
static Vec randn(_Tp a, _Tp b)
static Vec randu(_Tp a, _Tp b)
Mat getMat(int idx=-1) const
T fputs(T... args)
InputArrayOfArrays Size InputOutputArray InputOutputArray OutputArrayOfArrays OutputArrayOfArrays OutputArray OutputArray OutputArray int flags
Definition calib3d.hpp:1617
CV_EXPORTS_W void bitwise_xor(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())
Calculates the per-element bit-wise "exclusive or" operation on two arrays or an array and a scalar.
CV_EXPORTS_W void gemm(InputArray src1, InputArray src2, double alpha, InputArray src3, double beta, OutputArray dst, int flags=0)
Performs generalized matrix multiplication.
CV_EXPORTS RNG & theRNG()
Returns the default random number generator.
CV_EXPORTS_W void bitwise_or(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())
Calculates the per-element bit-wise disjunction of two arrays or an array and a scalar.
CV_EXPORTS_W double invert(InputArray src, OutputArray dst, int flags=DECOMP_LU)
Finds the inverse or pseudo-inverse of a matrix.
CV_EXPORTS_W bool solve(InputArray src1, InputArray src2, OutputArray dst, int flags=DECOMP_LU)
Solves one or more linear systems or least-squares problems.
CV_EXPORTS_W void randu(InputOutputArray dst, InputArray low, InputArray high)
Generates a single uniformly-distributed random number or an array of random numbers.
CV_EXPORTS_W void add(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-1)
Calculates the per-element sum of two arrays or an array and a scalar.
CV_EXPORTS_W void divide(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1)
Performs per-element division of two arrays or a scalar by an array.
CV_EXPORTS_W void bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())
computes bitwise conjunction of the two arrays (dst = src1 & src2) Calculates the per-element bit-wis...
CV_EXPORTS_W double determinant(InputArray mtx)
Returns the determinant of a square floating-point matrix.
CV_EXPORTS_W void randn(InputOutputArray dst, InputArray mean, InputArray stddev)
Fills the array with normally distributed random numbers.
CV_EXPORTS_W void subtract(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-1)
Calculates the per-element difference between two arrays or array and a scalar.
Scalar_< double > Scalar
Definition types.hpp:702
const _OutputArray & OutputArray
Definition mat.hpp:444
CV__DEBUG_NS_END typedef const _InputArray & InputArray
Definition mat.hpp:442
@ ACCESS_READ
Definition mat.hpp:65
void * parent
Definition core_c.h:1913
const CvArr CvArr * x
Definition core_c.h:1195
const CvArr const CvArr * V
Definition core_c.h:1341
CV_EXPORTS String int partition(const std::vector< _Tp > &_vec, std::vector< int > &labels, _EqPredicate predicate=_EqPredicate())
Splits an element set into equivalency classes.
Definition operations.hpp:521
signed char schar
Definition interface.h:48
unsigned char uchar
Definition interface.h:51
unsigned short ushort
Definition interface.h:52
uint64_t uint64
Definition interface.h:62
#define CV_EXPORTS
Definition cvdef.h:435
CV_EXPORTS int LU(float *A, size_t astep, int m, float *b, size_t bstep, int n)
#define MIN(a, b)
Definition cvdef.h:513
CV_EXPORTS bool Cholesky(float *A, size_t astep, int m, float *b, size_t bstep, int n)
CV_EXPORTS String format(const char *fmt,...) CV_FORMAT_PRINTF(1
Returns a text string formatted using the printf-like expression.
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition base.hpp:342
CvArr CvPoint2D32f double M
Definition imgproc_c.h:270
const CvArr CvArr int method
Definition imgproc_c.h:384
CvArr CvArr * temp
Definition imgproc_c.h:329
CV_EXPORTS OutputArray int double double InputArray OutputArray int int bool double k
Definition imgproc.hpp:2133
OutputArray dst
Definition imgproc.hpp:3564
OutputArray OutputArray labels
Definition imgproc.hpp:3565
T internal(T... args)
"black box" representation of the file storage associated with a file on disk.
Definition calib3d.hpp:441
T next(T... args)
T size(T... args)