45 #ifndef OPENCV_CORE_OPERATIONS_HPP
46 #define OPENCV_CORE_OPERATIONS_HPP
49 # error operations.hpp header must be compiled as C++
54 #if defined(__GNUC__) || defined(__clang__)
55 # if defined(__MINGW_PRINTF_FORMAT)
56 # define CV_FORMAT_PRINTF(string_idx, first_to_check) __attribute__ ((format (__MINGW_PRINTF_FORMAT, string_idx, first_to_check)))
58 # define CV_FORMAT_PRINTF(string_idx, first_to_check) __attribute__ ((format (printf, string_idx, first_to_check)))
61 # define CV_FORMAT_PRINTF(A, B)
74 template<
typename _Tp,
int m,
int n>
struct Matx_FastInvOp
76 bool operator()(
const Matx<_Tp, m, n>& a, Matx<_Tp, n, m>& b,
int method)
const
82 template<
typename _Tp,
int m>
struct Matx_FastInvOp<_Tp, m, m>
84 bool operator()(
const Matx<_Tp, m, m>& a, Matx<_Tp, m, m>& b,
int method)
const
88 Matx<_Tp, m, m>
temp = a;
91 for (
int i = 0; i < m; i++)
95 return Cholesky(
temp.val, m*
sizeof(_Tp), m, b.val, m*
sizeof(_Tp), m);
97 return LU(
temp.val, m*
sizeof(_Tp), m, b.val, m*
sizeof(_Tp), m) != 0;
106 template<
typename _Tp>
struct Matx_FastInvOp<_Tp, 2, 2>
108 bool operator()(
const Matx<_Tp, 2, 2>& a, Matx<_Tp, 2, 2>& b,
int )
const
122 template<
typename _Tp>
struct Matx_FastInvOp<_Tp, 3, 3>
124 bool operator()(
const Matx<_Tp, 3, 3>& a, Matx<_Tp, 3, 3>& b,
int )
const
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;
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;
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;
146 template<
typename _Tp,
int m,
int l,
int n>
struct Matx_FastSolveOp
148 bool operator()(
const Matx<_Tp, m, l>& a,
const Matx<_Tp, m, n>& b,
149 Matx<_Tp, l, n>&
x,
int method)
const
155 template<
typename _Tp,
int m,
int n>
struct Matx_FastSolveOp<_Tp, m, m, n>
157 bool operator()(
const Matx<_Tp, m, m>& a,
const Matx<_Tp, m, n>& b,
158 Matx<_Tp, m, n>&
x,
int method)
const
162 Matx<_Tp, m, m>
temp = a;
165 return Cholesky(
temp.val, m*
sizeof(_Tp), m,
x.val, n*
sizeof(_Tp), n);
167 return LU(
temp.val, m*
sizeof(_Tp), m,
x.val, n*
sizeof(_Tp), n) != 0;
176 template<
typename _Tp>
struct Matx_FastSolveOp<_Tp, 2, 2, 1>
178 bool operator()(
const Matx<_Tp, 2, 2>& a,
const Matx<_Tp, 2, 1>& b,
179 Matx<_Tp, 2, 1>&
x,
int)
const
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;
191 template<
typename _Tp>
struct Matx_FastSolveOp<_Tp, 3, 3, 1>
193 bool operator()(
const Matx<_Tp, 3, 3>& a,
const Matx<_Tp, 3, 1>& b,
194 Matx<_Tp, 3, 1>&
x,
int)
const
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)));
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)));
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)));
217 template<
typename _Tp,
int m,
int n>
inline
225 template<
typename _Tp,
int m,
int n>
inline
233 template<
typename _Tp,
int cn>
inline
241 template<
typename _Tp,
int cn>
inline
249 template<
typename _Tp,
int m,
int n>
inline
253 bool ok = cv::internal::Matx_FastInvOp<_Tp, m, n>()(*
this, b,
method);
254 if (p_is_ok) *p_is_ok = ok;
258 template<
typename _Tp,
int m,
int n>
template<
int l>
inline
262 bool ok = cv::internal::Matx_FastSolveOp<_Tp, m, n, l>()(*
this, rhs,
x,
method);
270 #define CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
271 static inline A& operator op (A& a, const B& b) { cvop; return a; }
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)
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)
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; }
285 CV_MAT_AUG_OPERATOR (+=,
cv::add(a, b, (
const Mat&)a), Mat, Mat)
286 CV_MAT_AUG_OPERATOR (+=,
cv::add(a, b, (
const Mat&)a), Mat,
Scalar)
287 CV_MAT_AUG_OPERATOR_T(+=,
cv::add(a, b, (
const Mat&)a), Mat_<_Tp>, Mat)
288 CV_MAT_AUG_OPERATOR_T(+=,
cv::add(a, b, (
const Mat&)a), Mat_<_Tp>,
Scalar)
289 CV_MAT_AUG_OPERATOR_T(+=,
cv::add(a, b, (
const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
290 CV_MAT_AUG_OPERATOR_TN(+=,
cv::add(a, Mat(b), (
const Mat&)a), Mat)
291 CV_MAT_AUG_OPERATOR_TN(+=,
cv::add(a, Mat(b), (
const Mat&)a), Mat_<_Tp>)
293 CV_MAT_AUG_OPERATOR (-=,
cv::subtract(a, b, (
const Mat&)a), Mat, Mat)
295 CV_MAT_AUG_OPERATOR_T(-=,
cv::subtract(a, b, (
const Mat&)a), Mat_<_Tp>, Mat)
297 CV_MAT_AUG_OPERATOR_T(-=,
cv::subtract(a, b, (
const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
298 CV_MAT_AUG_OPERATOR_TN(-=,
cv::subtract(a, Mat(b), (
const Mat&)a), Mat)
299 CV_MAT_AUG_OPERATOR_TN(-=,
cv::subtract(a, Mat(b), (
const Mat&)a), Mat_<_Tp>)
301 CV_MAT_AUG_OPERATOR (*=,
cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat, Mat)
302 CV_MAT_AUG_OPERATOR_T(*=,
cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat)
303 CV_MAT_AUG_OPERATOR_T(*=,
cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat_<_Tp>)
304 CV_MAT_AUG_OPERATOR (*=, a.convertTo(a, -1, b), Mat,
double)
305 CV_MAT_AUG_OPERATOR_T(*=, a.convertTo(a, -1, b), Mat_<_Tp>,
double)
306 CV_MAT_AUG_OPERATOR_TN(*=,
cv::gemm(a, Mat(b), 1, Mat(), 0, a, 0), Mat)
307 CV_MAT_AUG_OPERATOR_TN(*=,
cv::gemm(a, Mat(b), 1, Mat(), 0, a, 0), Mat_<_Tp>)
309 CV_MAT_AUG_OPERATOR (/=,
cv::divide(a, b, (
const Mat&)a), Mat, Mat)
310 CV_MAT_AUG_OPERATOR_T(/=,
cv::divide(a, b, (
const Mat&)a), Mat_<_Tp>, Mat)
311 CV_MAT_AUG_OPERATOR_T(/=,
cv::divide(a, b, (
const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
312 CV_MAT_AUG_OPERATOR (/=, a.convertTo((Mat&)a, -1, 1./b), Mat,
double)
313 CV_MAT_AUG_OPERATOR_T(/=, a.convertTo((Mat&)a, -1, 1./b), Mat_<_Tp>,
double)
314 CV_MAT_AUG_OPERATOR_TN(/=,
cv::divide(a, Mat(b), (
const Mat&)a), Mat)
315 CV_MAT_AUG_OPERATOR_TN(/=,
cv::divide(a, Mat(b), (
const Mat&)a), Mat_<_Tp>)
317 CV_MAT_AUG_OPERATOR (&=,
cv::bitwise_and(a, b, (
const Mat&)a), Mat, Mat)
319 CV_MAT_AUG_OPERATOR_T(&=,
cv::bitwise_and(a, b, (
const Mat&)a), Mat_<_Tp>, Mat)
321 CV_MAT_AUG_OPERATOR_T(&=,
cv::bitwise_and(a, b, (
const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
322 CV_MAT_AUG_OPERATOR_TN(&=,
cv::bitwise_and(a, Mat(b), (
const Mat&)a), Mat)
323 CV_MAT_AUG_OPERATOR_TN(&=,
cv::bitwise_and(a, Mat(b), (
const Mat&)a), Mat_<_Tp>)
325 CV_MAT_AUG_OPERATOR (|=,
cv::bitwise_or(a, b, (
const Mat&)a), Mat, Mat)
327 CV_MAT_AUG_OPERATOR_T(|=,
cv::bitwise_or(a, b, (
const Mat&)a), Mat_<_Tp>, Mat)
329 CV_MAT_AUG_OPERATOR_T(|=,
cv::bitwise_or(a, b, (
const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
330 CV_MAT_AUG_OPERATOR_TN(|=,
cv::bitwise_or(a, Mat(b), (
const Mat&)a), Mat)
331 CV_MAT_AUG_OPERATOR_TN(|=,
cv::bitwise_or(a, Mat(b), (
const Mat&)a), Mat_<_Tp>)
333 CV_MAT_AUG_OPERATOR (^=,
cv::bitwise_xor(a, b, (
const Mat&)a), Mat, Mat)
335 CV_MAT_AUG_OPERATOR_T(^=,
cv::bitwise_xor(a, b, (
const Mat&)a), Mat_<_Tp>, Mat)
337 CV_MAT_AUG_OPERATOR_T(^=,
cv::bitwise_xor(a, b, (
const Mat&)a), Mat_<_Tp>, Mat_<_Tp>)
338 CV_MAT_AUG_OPERATOR_TN(^=,
cv::bitwise_xor(a, Mat(b), (
const Mat&)a), Mat)
339 CV_MAT_AUG_OPERATOR_TN(^=,
cv::bitwise_xor(a, Mat(b), (
const Mat&)a), Mat_<_Tp>)
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
354 Mat mtx = m.getMat();
356 _dst.create(svd.vt.cols, 1, svd.vt.type());
361 template<
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 )
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);
370 template<
typename _Tp,
int m,
int n,
int nm>
inline void
371 SVD::compute(
const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w )
373 CV_StaticAssert( nm ==
MIN(m, n),
"Invalid size of output vector.");
374 Mat _a(a,
false), _w(
w,
false);
379 template<
typename _Tp,
int m,
int n,
int nm,
int nb>
inline void
380 SVD::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 )
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);
400 inline RNG::operator short() {
return (
short)
next(); }
401 inline RNG::operator int() {
return (
int)
next(); }
402 inline RNG::operator unsigned() {
return next(); }
403 inline RNG::operator float() {
return next()*2.3283064365386962890625e-10f; }
404 inline RNG::operator double() {
unsigned t =
next();
return (((
uint64)t << 32) |
next()) * 5.4210108624275221700372640043497e-20; }
409 inline int RNG::uniform(
int a,
int b) {
return a == b ? a : (int)(
next() % (b - a) + a); }
410 inline float RNG::uniform(
float a,
float b) {
return ((
float)*
this)*(b - a) + a; }
411 inline double RNG::uniform(
double a,
double b) {
return ((
double)*
this)*(b - a) + a; }
418 return (
unsigned)
state;
422 template<
typename _Tp>
static inline _Tp
randu()
437 int print(Ptr<Formatted> fmtd, FILE* stream = stdout)
441 for(
const char* str = fmtd->next(); str; str = fmtd->next())
442 written +=
fputs(str, stream);
448 int print(
const Mat& mtx, FILE* stream = stdout)
454 int print(
const UMat& mtx, FILE* stream = stdout)
459 template<
typename _Tp>
static inline
460 int print(
const std::vector<Point_<_Tp> >& vec, FILE* stream = stdout)
465 template<
typename _Tp>
static inline
466 int print(
const std::vector<Point3_<_Tp> >& vec, FILE* stream = stdout)
471 template<
typename _Tp,
int m,
int n>
static inline
472 int print(
const Matx<_Tp, m, n>& matx, FILE* stream = stdout)
520 template<typename _Tp, class _EqPredicate>
int
522 _EqPredicate predicate=_EqPredicate())
524 int i, j, N = (int)_vec.
size();
525 const _Tp* vec = &_vec[0];
531 int (*nodes)[2] = (int(*)[2])&_nodes[0];
534 for(i = 0; i < N; i++)
541 for( i = 0; i < N; i++ )
546 while( nodes[root][PARENT] >= 0 )
547 root = nodes[root][PARENT];
549 for( j = 0; j < N; j++ )
551 if( i == j || !predicate(vec[i], vec[j]))
555 while( nodes[root2][PARENT] >= 0 )
556 root2 = nodes[root2][PARENT];
561 int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
563 nodes[root2][PARENT] = root;
566 nodes[root][PARENT] = root2;
567 nodes[root2][RANK] += rank == rank2;
575 while( (
parent = nodes[
k][PARENT]) >= 0 )
577 nodes[
k][PARENT] = root;
583 while( (
parent = nodes[
k][PARENT]) >= 0 )
585 nodes[
k][PARENT] = root;
596 for( i = 0; i < N; i++ )
599 while( nodes[root][PARENT] >= 0 )
600 root = nodes[root][PARENT];
602 if( nodes[root][RANK] >= 0 )
603 nodes[root][RANK] = ~nclasses++;
604 labels[i] = ~nodes[root][RANK];
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
Matx< _Tp, n, l > solve(const Matx< _Tp, m, l > &rhs, int flags=DECOMP_LU) const
solve linear system
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
static CV_NODISCARD_STD Matx randu(_Tp a, _Tp b)
Generates uniformly distributed random numbers.
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
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)
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_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 RNG & theRNG()
Returns the default random number generator.
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 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
static double determinant(const Matx< _Tp, m, m > &a)
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
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441