OpenCV 4.5.3(日本語機械翻訳)
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
65
66 namespace cv
67{
68
70
71 namespace internal
72{
73
74 template<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
82 template<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
106 template<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
122 template<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
146 template<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
155 template<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
176 template<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
191 template<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
217 template<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
225 template<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
233 template<typename _Tp, int m, int n> inline
234Matx<_Tp, n, m> Matx<_Tp, m, n>::inv(int method, bool *p_is_ok /*= NULL*/) const
235 {
236 Matx<_Tp, n, m> b;
237 bool ok = cv::internal::Matx_FastInvOp<_Tp, m, n>()(*this, b, method);
238 if (p_is_ok) *p_is_ok = ok;
239 return ok ? b : Matx<_Tp, n, m>::zeros();
240}
241
242 template<typename _Tp, int m, int n> template<int l> inline
243Matx<_Tp, n, l> Matx<_Tp, m, n>::solve(const Matx<_Tp, m, l>& rhs, int method) const
244 {
245 Matx<_Tp, n, l> x;
246 bool ok = cv::internal::Matx_FastSolveOp<_Tp, m, n, l>()(*this, rhs, x, method);
247 return ok ? x : Matx<_Tp, n, l>::zeros();
248}
249
250
251
253
254 #define CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
255 static inline A& operator op (A& a, const B& b) { cvop; return a; }
256
257 #define CV_MAT_AUG_OPERATOR(op, cvop, A, B) \
258 CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
259 CV_MAT_AUG_OPERATOR1(op, cvop, const A, B)
260
261 #define CV_MAT_AUG_OPERATOR_T(op, cvop, A, B) \
262 template<typename _Tp> CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
263 template<typename _Tp> CV_MAT_AUG_OPERATOR1(op, cvop, const A, B)
264
265 #define CV_MAT_AUG_OPERATOR_TN(op, cvop, A) \
266 template<typename _Tp, int m, int n> static inline A& operator op (A& a, const Matx<_Tp,m,n>& b) { cvop; return a; } \
267 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; }
268
269CV_MAT_AUG_OPERATOR (+=, cv::add(a, b, (const Mat&)a), Mat, Mat)
270CV_MAT_AUG_OPERATOR (+=, cv::add(a, b, (const Mat&)a), Mat, Scalar)
271CV_MAT_AUG_OPERATOR_T(+=, cv::add(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
272CV_MAT_AUG_OPERATOR_T(+=, cv::add(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
273CV_MAT_AUG_OPERATOR_T(+=, cv::add(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
274CV_MAT_AUG_OPERATOR_TN(+=, cv::add(a, Mat(b), (const Mat&)a), Mat)
275CV_MAT_AUG_OPERATOR_TN(+=, cv::add(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
276
277CV_MAT_AUG_OPERATOR (-=, cv::subtract(a, b, (const Mat&)a), Mat, Mat)
278CV_MAT_AUG_OPERATOR (-=, cv::subtract(a, b, (const Mat&)a), Mat, Scalar)
279CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
280CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
281CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
282CV_MAT_AUG_OPERATOR_TN(-=, cv::subtract(a, Mat(b), (const Mat&)a), Mat)
283CV_MAT_AUG_OPERATOR_TN(-=, cv::subtract(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
284
285CV_MAT_AUG_OPERATOR (*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat, Mat)
286CV_MAT_AUG_OPERATOR_T(*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat)
287CV_MAT_AUG_OPERATOR_T(*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat_<_Tp>)
288CV_MAT_AUG_OPERATOR (*=, a.convertTo(a, -1, b), Mat, double)
289CV_MAT_AUG_OPERATOR_T(*=, a.convertTo(a, -1, b), Mat_<_Tp>, double)
290CV_MAT_AUG_OPERATOR_TN(*=, cv::gemm(a, Mat(b), 1, Mat(), 0, a, 0), Mat)
291CV_MAT_AUG_OPERATOR_TN(*=, cv::gemm(a, Mat(b), 1, Mat(), 0, a, 0), Mat_<_Tp>)
292
293CV_MAT_AUG_OPERATOR (/=, cv::divide(a, b, (const Mat&)a), Mat, Mat)
294CV_MAT_AUG_OPERATOR_T(/=, cv::divide(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
295CV_MAT_AUG_OPERATOR_T(/=, cv::divide(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
296CV_MAT_AUG_OPERATOR (/=, a.convertTo((Mat&)a, -1, 1./b), Mat, double)
297CV_MAT_AUG_OPERATOR_T(/=, a.convertTo((Mat&)a, -1, 1./b), Mat_<_Tp>, double)
298CV_MAT_AUG_OPERATOR_TN(/=, cv::divide(a, Mat(b), (const Mat&)a), Mat)
299CV_MAT_AUG_OPERATOR_TN(/=, cv::divide(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
300
301CV_MAT_AUG_OPERATOR (&=, cv::bitwise_and(a, b, (const Mat&)a), Mat, Mat)
302CV_MAT_AUG_OPERATOR (&=, cv::bitwise_and(a, b, (const Mat&)a), Mat, Scalar)
303CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
304CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
305CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
306CV_MAT_AUG_OPERATOR_TN(&=, cv::bitwise_and(a, Mat(b), (const Mat&)a), Mat)
307CV_MAT_AUG_OPERATOR_TN(&=, cv::bitwise_and(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
308
309CV_MAT_AUG_OPERATOR (|=, cv::bitwise_or(a, b, (const Mat&)a), Mat, Mat)
310CV_MAT_AUG_OPERATOR (|=, cv::bitwise_or(a, b, (const Mat&)a), Mat, Scalar)
311CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
312CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
313CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
314CV_MAT_AUG_OPERATOR_TN(|=, cv::bitwise_or(a, Mat(b), (const Mat&)a), Mat)
315CV_MAT_AUG_OPERATOR_TN(|=, cv::bitwise_or(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
316
317CV_MAT_AUG_OPERATOR (^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat, Mat)
318CV_MAT_AUG_OPERATOR (^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat, Scalar)
319CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat_<_Tp>, Mat)
320CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat_<_Tp>, Scalar)
321CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a, b, (const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
322CV_MAT_AUG_OPERATOR_TN(^=, cv::bitwise_xor(a, Mat(b), (const Mat&)a), Mat)
323CV_MAT_AUG_OPERATOR_TN(^=, cv::bitwise_xor(a, Mat(b), (const Mat&)a), Mat_<_Tp>)
324
325 #undef CV_MAT_AUG_OPERATOR_TN
326 #undef CV_MAT_AUG_OPERATOR_T
327 #undef CV_MAT_AUG_OPERATOR
328 #undef CV_MAT_AUG_OPERATOR1
329
330
331
333
334 inline SVD::SVD() {}
335 inline SVD::SVD( InputArray m, int flags ) { operator ()(m, flags); }
336 inline void SVD::solveZ( InputArray m, OutputArray _dst )
337{
338 Mat mtx = m.getMat();
339 SVD svd(mtx, (mtx.rows >= mtx.cols ? 0 : SVD::FULL_UV));
340 _dst.create(svd.vt.cols, 1, svd.vt.type());
341 Mat dst = _dst.getMat();
342 svd.vt.row(svd.vt.rows-1).reshape(1,svd.vt.cols).copyTo(dst);
343}
344
345 template<typename _Tp, int m, int n, int nm> inline void
346 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w, Matx<_Tp, m, nm>& u, Matx<_Tp, n, nm>& vt )
347{
348 CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
349 Mat _a(a, false), _u(u, false), _w(w, false), _vt(vt, false);
350 SVD::compute(_a, _w, _u, _vt);
351 CV_Assert(_w.data == (uchar*)&w.val[0] && _u.data == (uchar*)&u.val[0] && _vt.data == (uchar*)&vt.val[0]);
352}
353
354 template<typename _Tp, int m, int n, int nm> inline void
355 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w )
356{
357 CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
358 Mat _a(a, false), _w(w, false);
359 SVD::compute(_a, _w);
360 CV_Assert(_w.data == (uchar*)&w.val[0]);
361}
362
363 template<typename _Tp, int m, int n, int nm, int nb> inline void
364 SVD::backSubst( const Matx<_Tp, nm, 1>& w, const Matx<_Tp, m, nm>& u,
365 const Matx<_Tp, n, nm>& vt, const Matx<_Tp, m, nb>& rhs,
366 Matx<_Tp, n, nb>& dst )
367{
368 CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
369 Mat _u(u, false), _w(w, false), _vt(vt, false), _rhs(rhs, false), _dst(dst, false);
370 SVD::backSubst(_w, _u, _vt, _rhs, _dst);
371 CV_Assert(_dst.data == (uchar*)&dst.val[0]);
372}
373
374
375
377
378 inline RNG::RNG() { state = 0xffffffff; }
379 inline RNG::RNG(uint64 _state) { state = _state ? _state : 0xffffffff; }
380
381 inline RNG::operator uchar() { return (uchar)next(); }
382 inline RNG::operator schar() { return (schar)next(); }
383 inline RNG::operator ushort() { return (ushort)next(); }
384 inline RNG::operator short() { return (short)next(); }
385 inline RNG::operator int() { return (int)next(); }
386 inline RNG::operator unsigned() { return next(); }
387 inline RNG::operator float() { return next()*2.3283064365386962890625e-10f; }
388 inline RNG::operator double() { unsigned t = next(); return (((uint64)t << 32) | next()) * 5.4210108624275221700372640043497e-20; }
389
390 inline unsigned RNG::operator ()(unsigned N) { return (unsigned)uniform(0,N); }
391 inline unsigned RNG::operator ()() { return next(); }
392
393 inline int RNG::uniform(int a, int b) { return a == b ? a : (int)(next() % (b - a) + a); }
394 inline float RNG::uniform(float a, float b) { return ((float)*this)*(b - a) + a; }
395 inline double RNG::uniform(double a, double b) { return ((double)*this)*(b - a) + a; }
396
397 inline bool RNG::operator ==(const RNG& other) const { return state == other.state; }
398
399 inline unsigned RNG::next()
400{
401 state = (uint64)(unsigned)state* /*CV_RNG_COEFF*/ 4164903690U + (unsigned)(state >> 32);
402 return (unsigned)state;
403}
404
406 template<typename _Tp> static inline _Tp randu()
407{
408 return (_Tp)theRNG();
409}
410
412
430CV_EXPORTS String format( const char* fmt, ... ) CV_FORMAT_PRINTF(1, 2);
431
433
434static inline
435Ptr<Formatted> format(InputArray mtx, Formatter::FormatType fmt)
436{
437 return Formatter::get(fmt)->format(mtx.getMat());
438}
439
440 static inline
441 int print(Ptr<Formatted> fmtd, FILE* stream = stdout)
442{
443 int written = 0;
444 fmtd->reset();
445 for(const char* str = fmtd->next(); str; str = fmtd->next())
446 written += fputs(str, stream);
447
448 return written;
449}
450
451 static inline
452 int print(const Mat& mtx, FILE* stream = stdout)
453{
454 return print(Formatter::get()->format(mtx), stream);
455}
456
457 static inline
458 int print(const UMat& mtx, FILE* stream = stdout)
459{
460 return print(Formatter::get()->format(mtx.getMat(ACCESS_READ)), stream);
461}
462
463 template<typename _Tp> static inline
464 int print(const std::vector<Point_<_Tp> >& vec, FILE* stream = stdout)
465{
466 return print(Formatter::get()->format(Mat(vec)), stream);
467}
468
469 template<typename _Tp> static inline
470 int print(const std::vector<Point3_<_Tp> >& vec, FILE* stream = stdout)
471{
472 return print(Formatter::get()->format(Mat(vec)), stream);
473}
474
475 template<typename _Tp, int m, int n> static inline
476 int print(const Matx<_Tp, m, n>& matx, FILE* stream = stdout)
477{
478 return print(Formatter::get()->format(cv::Mat(matx)), stream);
479}
480
482
483 /****************************************************************************************\
484 * Auxiliary algorithms *
485 \****************************************************************************************/
486
502 template<typename _Tp, class _EqPredicate> int
503 partition( const std::vector<_Tp>& _vec, std::vector<int>& labels,
504 _EqPredicate predicate=_EqPredicate())
505{
506 int i, j, N = (int)_vec.size();
507 const _Tp* vec = &_vec[0];
508
509 const int PARENT=0;
510 const int RANK=1;
511
512 std::vector<int> _nodes(N*2);
513 int (*nodes)[2] = (int(*)[2])&_nodes[0];
514
515 // The first O(N) pass: create N single-vertex trees
516 for(i = 0; i < N; i++)
517 {
518 nodes[i][PARENT]=-1;
519 nodes[i][RANK] = 0;
520 }
521
522 // The main O(N^2) pass: merge connected components
523 for( i = 0; i < N; i++ )
524 {
525 int root = i;
526
527 // find root
528 while( nodes[root][PARENT] >= 0 )
529 root = nodes[root][PARENT];
530
531 for( j = 0; j < N; j++ )
532 {
533 if( i == j || !predicate(vec[i], vec[j]))
534 continue;
535 int root2 = j;
536
537 while( nodes[root2][PARENT] >= 0 )
538 root2 = nodes[root2][PARENT];
539
540 if( root2 != root )
541 {
542 // unite both trees
543 int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
544 if( rank > rank2 )
545 nodes[root2][PARENT] = root;
546 else
547 {
548 nodes[root][PARENT] = root2;
549 nodes[root2][RANK] += rank == rank2;
550 root = root2;
551 }
552 CV_Assert( nodes[root][PARENT] < 0 );
553
554 int k = j, parent;
555
556 // compress the path from node2 to root
557 while( (parent = nodes[k][PARENT]) >= 0 )
558 {
559 nodes[k][PARENT] = root;
560 k = parent;
561 }
562
563 // compress the path from node to root
564 k = i;
565 while( (parent = nodes[k][PARENT]) >= 0 )
566 {
567 nodes[k][PARENT] = root;
568 k = parent;
569 }
570 }
571 }
572 }
573
574 // Final O(N) pass: enumerate classes
575 labels.resize(N);
576 int nclasses = 0;
577
578 for( i = 0; i < N; i++ )
579 {
580 int root = i;
581 while( nodes[root][PARENT] >= 0 )
582 root = nodes[root][PARENT];
583 // re-use the rank as the class label
584 if( nodes[root][RANK] >= 0 )
585 nodes[root][RANK] = ~nclasses++;
586 labels[i] = ~nodes[root][RANK];
587 }
588
589 return nclasses;
590}
591
592} // cv
593
594 #endif
n-dimensional dense array class
Definition: mat.hpp:802
static Matx randn(_Tp a, _Tp b)
Generates normally distributed random numbers
Matx< _Tp, n, m > inv(int method=DECOMP_LU, bool *p_is_ok=NULL) const
invert the matrix
static Matx randu(_Tp a, _Tp b)
Generates uniformly distributed random numbers
Matx< _Tp, n, l > solve(const Matx< _Tp, m, l > &rhs, int flags=DECOMP_LU) const
solve linear system
RNG()
constructor
unsigned operator()()
returns a random integer sampled uniformly from [0, N).
int uniform(int a, int b)
returns uniformly distributed integer random number from [a,b) range
unsigned next()
SVD()
the default constructor
@ FULL_UV
Definition: core.hpp:2656
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
static void solveZ(InputArray src, OutputArray dst)
solves an under-determined singular linear system
SVD & operator()(InputArray src, int flags=0)
the operator that performs SVD. The previously allocated u, w and vt are released.
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.
@ DECOMP_LU
Definition: base.hpp:135
@ DECOMP_CHOLESKY
Definition: base.hpp:143
int partition(const std::vector< _Tp > &_vec, std::vector< int > &labels, _EqPredicate predicate=_EqPredicate())
Splits an element set into equivalency classes.
Definition: operations.hpp:503
CV_EXPORTS int LU(float *A, size_t astep, int m, float *b, size_t bstep, int n)
CV_EXPORTS bool Cholesky(float *A, size_t astep, int m, float *b, size_t bstep, int n)
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails
Definition: base.hpp:342
cv
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75