opencv on mbed

Dependencies:   mbed

Committer:
joeverbout
Date:
Thu Mar 31 21:16:38 2016 +0000
Revision:
0:ea44dc9ed014
OpenCV on mbed attempt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeverbout 0:ea44dc9ed014 1 /*M///////////////////////////////////////////////////////////////////////////////////////
joeverbout 0:ea44dc9ed014 2 //
joeverbout 0:ea44dc9ed014 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
joeverbout 0:ea44dc9ed014 4 //
joeverbout 0:ea44dc9ed014 5 // By downloading, copying, installing or using the software you agree to this license.
joeverbout 0:ea44dc9ed014 6 // If you do not agree to this license, do not download, install,
joeverbout 0:ea44dc9ed014 7 // copy or use the software.
joeverbout 0:ea44dc9ed014 8 //
joeverbout 0:ea44dc9ed014 9 //
joeverbout 0:ea44dc9ed014 10 // License Agreement
joeverbout 0:ea44dc9ed014 11 // For Open Source Computer Vision Library
joeverbout 0:ea44dc9ed014 12 //
joeverbout 0:ea44dc9ed014 13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
joeverbout 0:ea44dc9ed014 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
joeverbout 0:ea44dc9ed014 15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
joeverbout 0:ea44dc9ed014 16 // Copyright (C) 2015, Itseez Inc., all rights reserved.
joeverbout 0:ea44dc9ed014 17 // Third party copyrights are property of their respective owners.
joeverbout 0:ea44dc9ed014 18 //
joeverbout 0:ea44dc9ed014 19 // Redistribution and use in source and binary forms, with or without modification,
joeverbout 0:ea44dc9ed014 20 // are permitted provided that the following conditions are met:
joeverbout 0:ea44dc9ed014 21 //
joeverbout 0:ea44dc9ed014 22 // * Redistribution's of source code must retain the above copyright notice,
joeverbout 0:ea44dc9ed014 23 // this list of conditions and the following disclaimer.
joeverbout 0:ea44dc9ed014 24 //
joeverbout 0:ea44dc9ed014 25 // * Redistribution's in binary form must reproduce the above copyright notice,
joeverbout 0:ea44dc9ed014 26 // this list of conditions and the following disclaimer in the documentation
joeverbout 0:ea44dc9ed014 27 // and/or other materials provided with the distribution.
joeverbout 0:ea44dc9ed014 28 //
joeverbout 0:ea44dc9ed014 29 // * The name of the copyright holders may not be used to endorse or promote products
joeverbout 0:ea44dc9ed014 30 // derived from this software without specific prior written permission.
joeverbout 0:ea44dc9ed014 31 //
joeverbout 0:ea44dc9ed014 32 // This software is provided by the copyright holders and contributors "as is" and
joeverbout 0:ea44dc9ed014 33 // any express or implied warranties, including, but not limited to, the implied
joeverbout 0:ea44dc9ed014 34 // warranties of merchantability and fitness for a particular purpose are disclaimed.
joeverbout 0:ea44dc9ed014 35 // In no event shall the Intel Corporation or contributors be liable for any direct,
joeverbout 0:ea44dc9ed014 36 // indirect, incidental, special, exemplary, or consequential damages
joeverbout 0:ea44dc9ed014 37 // (including, but not limited to, procurement of substitute goods or services;
joeverbout 0:ea44dc9ed014 38 // loss of use, data, or profits; or business interruption) however caused
joeverbout 0:ea44dc9ed014 39 // and on any theory of liability, whether in contract, strict liability,
joeverbout 0:ea44dc9ed014 40 // or tort (including negligence or otherwise) arising in any way out of
joeverbout 0:ea44dc9ed014 41 // the use of this software, even if advised of the possibility of such damage.
joeverbout 0:ea44dc9ed014 42 //
joeverbout 0:ea44dc9ed014 43 //M*/
joeverbout 0:ea44dc9ed014 44
joeverbout 0:ea44dc9ed014 45 #ifndef __OPENCV_CORE_OPERATIONS_HPP__
joeverbout 0:ea44dc9ed014 46 #define __OPENCV_CORE_OPERATIONS_HPP__
joeverbout 0:ea44dc9ed014 47
joeverbout 0:ea44dc9ed014 48 #ifndef __cplusplus
joeverbout 0:ea44dc9ed014 49 # error operations.hpp header must be compiled as C++
joeverbout 0:ea44dc9ed014 50 #endif
joeverbout 0:ea44dc9ed014 51
joeverbout 0:ea44dc9ed014 52 #include <cstdio>
joeverbout 0:ea44dc9ed014 53
joeverbout 0:ea44dc9ed014 54 //! @cond IGNORED
joeverbout 0:ea44dc9ed014 55
joeverbout 0:ea44dc9ed014 56 namespace cv
joeverbout 0:ea44dc9ed014 57 {
joeverbout 0:ea44dc9ed014 58
joeverbout 0:ea44dc9ed014 59 ////////////////////////////// Matx methods depending on core API /////////////////////////////
joeverbout 0:ea44dc9ed014 60
joeverbout 0:ea44dc9ed014 61 namespace internal
joeverbout 0:ea44dc9ed014 62 {
joeverbout 0:ea44dc9ed014 63
joeverbout 0:ea44dc9ed014 64 template<typename _Tp, int m> struct Matx_FastInvOp
joeverbout 0:ea44dc9ed014 65 {
joeverbout 0:ea44dc9ed014 66 bool operator()(const Matx<_Tp, m, m>& a, Matx<_Tp, m, m>& b, int method) const
joeverbout 0:ea44dc9ed014 67 {
joeverbout 0:ea44dc9ed014 68 Matx<_Tp, m, m> temp = a;
joeverbout 0:ea44dc9ed014 69
joeverbout 0:ea44dc9ed014 70 // assume that b is all 0's on input => make it a unity matrix
joeverbout 0:ea44dc9ed014 71 for( int i = 0; i < m; i++ )
joeverbout 0:ea44dc9ed014 72 b(i, i) = (_Tp)1;
joeverbout 0:ea44dc9ed014 73
joeverbout 0:ea44dc9ed014 74 if( method == DECOMP_CHOLESKY )
joeverbout 0:ea44dc9ed014 75 return Cholesky(temp.val, m*sizeof(_Tp), m, b.val, m*sizeof(_Tp), m);
joeverbout 0:ea44dc9ed014 76
joeverbout 0:ea44dc9ed014 77 return LU(temp.val, m*sizeof(_Tp), m, b.val, m*sizeof(_Tp), m) != 0;
joeverbout 0:ea44dc9ed014 78 }
joeverbout 0:ea44dc9ed014 79 };
joeverbout 0:ea44dc9ed014 80
joeverbout 0:ea44dc9ed014 81 template<typename _Tp> struct Matx_FastInvOp<_Tp, 2>
joeverbout 0:ea44dc9ed014 82 {
joeverbout 0:ea44dc9ed014 83 bool operator()(const Matx<_Tp, 2, 2>& a, Matx<_Tp, 2, 2>& b, int) const
joeverbout 0:ea44dc9ed014 84 {
joeverbout 0:ea44dc9ed014 85 _Tp d = determinant(a);
joeverbout 0:ea44dc9ed014 86 if( d == 0 )
joeverbout 0:ea44dc9ed014 87 return false;
joeverbout 0:ea44dc9ed014 88 d = 1/d;
joeverbout 0:ea44dc9ed014 89 b(1,1) = a(0,0)*d;
joeverbout 0:ea44dc9ed014 90 b(0,0) = a(1,1)*d;
joeverbout 0:ea44dc9ed014 91 b(0,1) = -a(0,1)*d;
joeverbout 0:ea44dc9ed014 92 b(1,0) = -a(1,0)*d;
joeverbout 0:ea44dc9ed014 93 return true;
joeverbout 0:ea44dc9ed014 94 }
joeverbout 0:ea44dc9ed014 95 };
joeverbout 0:ea44dc9ed014 96
joeverbout 0:ea44dc9ed014 97 template<typename _Tp> struct Matx_FastInvOp<_Tp, 3>
joeverbout 0:ea44dc9ed014 98 {
joeverbout 0:ea44dc9ed014 99 bool operator()(const Matx<_Tp, 3, 3>& a, Matx<_Tp, 3, 3>& b, int) const
joeverbout 0:ea44dc9ed014 100 {
joeverbout 0:ea44dc9ed014 101 _Tp d = (_Tp)determinant(a);
joeverbout 0:ea44dc9ed014 102 if( d == 0 )
joeverbout 0:ea44dc9ed014 103 return false;
joeverbout 0:ea44dc9ed014 104 d = 1/d;
joeverbout 0:ea44dc9ed014 105 b(0,0) = (a(1,1) * a(2,2) - a(1,2) * a(2,1)) * d;
joeverbout 0:ea44dc9ed014 106 b(0,1) = (a(0,2) * a(2,1) - a(0,1) * a(2,2)) * d;
joeverbout 0:ea44dc9ed014 107 b(0,2) = (a(0,1) * a(1,2) - a(0,2) * a(1,1)) * d;
joeverbout 0:ea44dc9ed014 108
joeverbout 0:ea44dc9ed014 109 b(1,0) = (a(1,2) * a(2,0) - a(1,0) * a(2,2)) * d;
joeverbout 0:ea44dc9ed014 110 b(1,1) = (a(0,0) * a(2,2) - a(0,2) * a(2,0)) * d;
joeverbout 0:ea44dc9ed014 111 b(1,2) = (a(0,2) * a(1,0) - a(0,0) * a(1,2)) * d;
joeverbout 0:ea44dc9ed014 112
joeverbout 0:ea44dc9ed014 113 b(2,0) = (a(1,0) * a(2,1) - a(1,1) * a(2,0)) * d;
joeverbout 0:ea44dc9ed014 114 b(2,1) = (a(0,1) * a(2,0) - a(0,0) * a(2,1)) * d;
joeverbout 0:ea44dc9ed014 115 b(2,2) = (a(0,0) * a(1,1) - a(0,1) * a(1,0)) * d;
joeverbout 0:ea44dc9ed014 116 return true;
joeverbout 0:ea44dc9ed014 117 }
joeverbout 0:ea44dc9ed014 118 };
joeverbout 0:ea44dc9ed014 119
joeverbout 0:ea44dc9ed014 120
joeverbout 0:ea44dc9ed014 121 template<typename _Tp, int m, int n> struct Matx_FastSolveOp
joeverbout 0:ea44dc9ed014 122 {
joeverbout 0:ea44dc9ed014 123 bool operator()(const Matx<_Tp, m, m>& a, const Matx<_Tp, m, n>& b,
joeverbout 0:ea44dc9ed014 124 Matx<_Tp, m, n>& x, int method) const
joeverbout 0:ea44dc9ed014 125 {
joeverbout 0:ea44dc9ed014 126 Matx<_Tp, m, m> temp = a;
joeverbout 0:ea44dc9ed014 127 x = b;
joeverbout 0:ea44dc9ed014 128 if( method == DECOMP_CHOLESKY )
joeverbout 0:ea44dc9ed014 129 return Cholesky(temp.val, m*sizeof(_Tp), m, x.val, n*sizeof(_Tp), n);
joeverbout 0:ea44dc9ed014 130
joeverbout 0:ea44dc9ed014 131 return LU(temp.val, m*sizeof(_Tp), m, x.val, n*sizeof(_Tp), n) != 0;
joeverbout 0:ea44dc9ed014 132 }
joeverbout 0:ea44dc9ed014 133 };
joeverbout 0:ea44dc9ed014 134
joeverbout 0:ea44dc9ed014 135 template<typename _Tp> struct Matx_FastSolveOp<_Tp, 2, 1>
joeverbout 0:ea44dc9ed014 136 {
joeverbout 0:ea44dc9ed014 137 bool operator()(const Matx<_Tp, 2, 2>& a, const Matx<_Tp, 2, 1>& b,
joeverbout 0:ea44dc9ed014 138 Matx<_Tp, 2, 1>& x, int) const
joeverbout 0:ea44dc9ed014 139 {
joeverbout 0:ea44dc9ed014 140 _Tp d = determinant(a);
joeverbout 0:ea44dc9ed014 141 if( d == 0 )
joeverbout 0:ea44dc9ed014 142 return false;
joeverbout 0:ea44dc9ed014 143 d = 1/d;
joeverbout 0:ea44dc9ed014 144 x(0) = (b(0)*a(1,1) - b(1)*a(0,1))*d;
joeverbout 0:ea44dc9ed014 145 x(1) = (b(1)*a(0,0) - b(0)*a(1,0))*d;
joeverbout 0:ea44dc9ed014 146 return true;
joeverbout 0:ea44dc9ed014 147 }
joeverbout 0:ea44dc9ed014 148 };
joeverbout 0:ea44dc9ed014 149
joeverbout 0:ea44dc9ed014 150 template<typename _Tp> struct Matx_FastSolveOp<_Tp, 3, 1>
joeverbout 0:ea44dc9ed014 151 {
joeverbout 0:ea44dc9ed014 152 bool operator()(const Matx<_Tp, 3, 3>& a, const Matx<_Tp, 3, 1>& b,
joeverbout 0:ea44dc9ed014 153 Matx<_Tp, 3, 1>& x, int) const
joeverbout 0:ea44dc9ed014 154 {
joeverbout 0:ea44dc9ed014 155 _Tp d = (_Tp)determinant(a);
joeverbout 0:ea44dc9ed014 156 if( d == 0 )
joeverbout 0:ea44dc9ed014 157 return false;
joeverbout 0:ea44dc9ed014 158 d = 1/d;
joeverbout 0:ea44dc9ed014 159 x(0) = d*(b(0)*(a(1,1)*a(2,2) - a(1,2)*a(2,1)) -
joeverbout 0:ea44dc9ed014 160 a(0,1)*(b(1)*a(2,2) - a(1,2)*b(2)) +
joeverbout 0:ea44dc9ed014 161 a(0,2)*(b(1)*a(2,1) - a(1,1)*b(2)));
joeverbout 0:ea44dc9ed014 162
joeverbout 0:ea44dc9ed014 163 x(1) = d*(a(0,0)*(b(1)*a(2,2) - a(1,2)*b(2)) -
joeverbout 0:ea44dc9ed014 164 b(0)*(a(1,0)*a(2,2) - a(1,2)*a(2,0)) +
joeverbout 0:ea44dc9ed014 165 a(0,2)*(a(1,0)*b(2) - b(1)*a(2,0)));
joeverbout 0:ea44dc9ed014 166
joeverbout 0:ea44dc9ed014 167 x(2) = d*(a(0,0)*(a(1,1)*b(2) - b(1)*a(2,1)) -
joeverbout 0:ea44dc9ed014 168 a(0,1)*(a(1,0)*b(2) - b(1)*a(2,0)) +
joeverbout 0:ea44dc9ed014 169 b(0)*(a(1,0)*a(2,1) - a(1,1)*a(2,0)));
joeverbout 0:ea44dc9ed014 170 return true;
joeverbout 0:ea44dc9ed014 171 }
joeverbout 0:ea44dc9ed014 172 };
joeverbout 0:ea44dc9ed014 173
joeverbout 0:ea44dc9ed014 174 } // internal
joeverbout 0:ea44dc9ed014 175
joeverbout 0:ea44dc9ed014 176 template<typename _Tp, int m, int n> inline
joeverbout 0:ea44dc9ed014 177 Matx<_Tp,m,n> Matx<_Tp,m,n>::randu(_Tp a, _Tp b)
joeverbout 0:ea44dc9ed014 178 {
joeverbout 0:ea44dc9ed014 179 Matx<_Tp,m,n> M;
joeverbout 0:ea44dc9ed014 180 cv::randu(M, Scalar(a), Scalar(b));
joeverbout 0:ea44dc9ed014 181 return M;
joeverbout 0:ea44dc9ed014 182 }
joeverbout 0:ea44dc9ed014 183
joeverbout 0:ea44dc9ed014 184 template<typename _Tp, int m, int n> inline
joeverbout 0:ea44dc9ed014 185 Matx<_Tp,m,n> Matx<_Tp,m,n>::randn(_Tp a, _Tp b)
joeverbout 0:ea44dc9ed014 186 {
joeverbout 0:ea44dc9ed014 187 Matx<_Tp,m,n> M;
joeverbout 0:ea44dc9ed014 188 cv::randn(M, Scalar(a), Scalar(b));
joeverbout 0:ea44dc9ed014 189 return M;
joeverbout 0:ea44dc9ed014 190 }
joeverbout 0:ea44dc9ed014 191
joeverbout 0:ea44dc9ed014 192 template<typename _Tp, int m, int n> inline
joeverbout 0:ea44dc9ed014 193 Matx<_Tp, n, m> Matx<_Tp, m, n>::inv(int method, bool *p_is_ok /*= NULL*/) const
joeverbout 0:ea44dc9ed014 194 {
joeverbout 0:ea44dc9ed014 195 Matx<_Tp, n, m> b;
joeverbout 0:ea44dc9ed014 196 bool ok;
joeverbout 0:ea44dc9ed014 197 if( method == DECOMP_LU || method == DECOMP_CHOLESKY )
joeverbout 0:ea44dc9ed014 198 ok = cv::internal::Matx_FastInvOp<_Tp, m>()(*this, b, method);
joeverbout 0:ea44dc9ed014 199 else
joeverbout 0:ea44dc9ed014 200 {
joeverbout 0:ea44dc9ed014 201 Mat A(*this, false), B(b, false);
joeverbout 0:ea44dc9ed014 202 ok = (invert(A, B, method) != 0);
joeverbout 0:ea44dc9ed014 203 }
joeverbout 0:ea44dc9ed014 204 if( NULL != p_is_ok ) { *p_is_ok = ok; }
joeverbout 0:ea44dc9ed014 205 return ok ? b : Matx<_Tp, n, m>::zeros();
joeverbout 0:ea44dc9ed014 206 }
joeverbout 0:ea44dc9ed014 207
joeverbout 0:ea44dc9ed014 208 template<typename _Tp, int m, int n> template<int l> inline
joeverbout 0:ea44dc9ed014 209 Matx<_Tp, n, l> Matx<_Tp, m, n>::solve(const Matx<_Tp, m, l>& rhs, int method) const
joeverbout 0:ea44dc9ed014 210 {
joeverbout 0:ea44dc9ed014 211 Matx<_Tp, n, l> x;
joeverbout 0:ea44dc9ed014 212 bool ok;
joeverbout 0:ea44dc9ed014 213 if( method == DECOMP_LU || method == DECOMP_CHOLESKY )
joeverbout 0:ea44dc9ed014 214 ok = cv::internal::Matx_FastSolveOp<_Tp, m, l>()(*this, rhs, x, method);
joeverbout 0:ea44dc9ed014 215 else
joeverbout 0:ea44dc9ed014 216 {
joeverbout 0:ea44dc9ed014 217 Mat A(*this, false), B(rhs, false), X(x, false);
joeverbout 0:ea44dc9ed014 218 ok = cv::solve(A, B, X, method);
joeverbout 0:ea44dc9ed014 219 }
joeverbout 0:ea44dc9ed014 220
joeverbout 0:ea44dc9ed014 221 return ok ? x : Matx<_Tp, n, l>::zeros();
joeverbout 0:ea44dc9ed014 222 }
joeverbout 0:ea44dc9ed014 223
joeverbout 0:ea44dc9ed014 224
joeverbout 0:ea44dc9ed014 225
joeverbout 0:ea44dc9ed014 226 ////////////////////////// Augmenting algebraic & logical operations //////////////////////////
joeverbout 0:ea44dc9ed014 227
joeverbout 0:ea44dc9ed014 228 #define CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
joeverbout 0:ea44dc9ed014 229 static inline A& operator op (A& a, const B& b) { cvop; return a; }
joeverbout 0:ea44dc9ed014 230
joeverbout 0:ea44dc9ed014 231 #define CV_MAT_AUG_OPERATOR(op, cvop, A, B) \
joeverbout 0:ea44dc9ed014 232 CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
joeverbout 0:ea44dc9ed014 233 CV_MAT_AUG_OPERATOR1(op, cvop, const A, B)
joeverbout 0:ea44dc9ed014 234
joeverbout 0:ea44dc9ed014 235 #define CV_MAT_AUG_OPERATOR_T(op, cvop, A, B) \
joeverbout 0:ea44dc9ed014 236 template<typename _Tp> CV_MAT_AUG_OPERATOR1(op, cvop, A, B) \
joeverbout 0:ea44dc9ed014 237 template<typename _Tp> CV_MAT_AUG_OPERATOR1(op, cvop, const A, B)
joeverbout 0:ea44dc9ed014 238
joeverbout 0:ea44dc9ed014 239 CV_MAT_AUG_OPERATOR (+=, cv::add(a,b,a), Mat, Mat)
joeverbout 0:ea44dc9ed014 240 CV_MAT_AUG_OPERATOR (+=, cv::add(a,b,a), Mat, Scalar)
joeverbout 0:ea44dc9ed014 241 CV_MAT_AUG_OPERATOR_T(+=, cv::add(a,b,a), Mat_<_Tp>, Mat)
joeverbout 0:ea44dc9ed014 242 CV_MAT_AUG_OPERATOR_T(+=, cv::add(a,b,a), Mat_<_Tp>, Scalar)
joeverbout 0:ea44dc9ed014 243 CV_MAT_AUG_OPERATOR_T(+=, cv::add(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
joeverbout 0:ea44dc9ed014 244
joeverbout 0:ea44dc9ed014 245 CV_MAT_AUG_OPERATOR (-=, cv::subtract(a,b,a), Mat, Mat)
joeverbout 0:ea44dc9ed014 246 CV_MAT_AUG_OPERATOR (-=, cv::subtract(a,b,a), Mat, Scalar)
joeverbout 0:ea44dc9ed014 247 CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a,b,a), Mat_<_Tp>, Mat)
joeverbout 0:ea44dc9ed014 248 CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a,b,a), Mat_<_Tp>, Scalar)
joeverbout 0:ea44dc9ed014 249 CV_MAT_AUG_OPERATOR_T(-=, cv::subtract(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
joeverbout 0:ea44dc9ed014 250
joeverbout 0:ea44dc9ed014 251 CV_MAT_AUG_OPERATOR (*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat, Mat)
joeverbout 0:ea44dc9ed014 252 CV_MAT_AUG_OPERATOR_T(*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat)
joeverbout 0:ea44dc9ed014 253 CV_MAT_AUG_OPERATOR_T(*=, cv::gemm(a, b, 1, Mat(), 0, a, 0), Mat_<_Tp>, Mat_<_Tp>)
joeverbout 0:ea44dc9ed014 254 CV_MAT_AUG_OPERATOR (*=, a.convertTo(a, -1, b), Mat, double)
joeverbout 0:ea44dc9ed014 255 CV_MAT_AUG_OPERATOR_T(*=, a.convertTo(a, -1, b), Mat_<_Tp>, double)
joeverbout 0:ea44dc9ed014 256
joeverbout 0:ea44dc9ed014 257 CV_MAT_AUG_OPERATOR (/=, cv::divide(a,b,a), Mat, Mat)
joeverbout 0:ea44dc9ed014 258 CV_MAT_AUG_OPERATOR_T(/=, cv::divide(a,b,a), Mat_<_Tp>, Mat)
joeverbout 0:ea44dc9ed014 259 CV_MAT_AUG_OPERATOR_T(/=, cv::divide(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
joeverbout 0:ea44dc9ed014 260 CV_MAT_AUG_OPERATOR (/=, a.convertTo((Mat&)a, -1, 1./b), Mat, double)
joeverbout 0:ea44dc9ed014 261 CV_MAT_AUG_OPERATOR_T(/=, a.convertTo((Mat&)a, -1, 1./b), Mat_<_Tp>, double)
joeverbout 0:ea44dc9ed014 262
joeverbout 0:ea44dc9ed014 263 CV_MAT_AUG_OPERATOR (&=, cv::bitwise_and(a,b,a), Mat, Mat)
joeverbout 0:ea44dc9ed014 264 CV_MAT_AUG_OPERATOR (&=, cv::bitwise_and(a,b,a), Mat, Scalar)
joeverbout 0:ea44dc9ed014 265 CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a,b,a), Mat_<_Tp>, Mat)
joeverbout 0:ea44dc9ed014 266 CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a,b,a), Mat_<_Tp>, Scalar)
joeverbout 0:ea44dc9ed014 267 CV_MAT_AUG_OPERATOR_T(&=, cv::bitwise_and(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
joeverbout 0:ea44dc9ed014 268
joeverbout 0:ea44dc9ed014 269 CV_MAT_AUG_OPERATOR (|=, cv::bitwise_or(a,b,a), Mat, Mat)
joeverbout 0:ea44dc9ed014 270 CV_MAT_AUG_OPERATOR (|=, cv::bitwise_or(a,b,a), Mat, Scalar)
joeverbout 0:ea44dc9ed014 271 CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a,b,a), Mat_<_Tp>, Mat)
joeverbout 0:ea44dc9ed014 272 CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a,b,a), Mat_<_Tp>, Scalar)
joeverbout 0:ea44dc9ed014 273 CV_MAT_AUG_OPERATOR_T(|=, cv::bitwise_or(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
joeverbout 0:ea44dc9ed014 274
joeverbout 0:ea44dc9ed014 275 CV_MAT_AUG_OPERATOR (^=, cv::bitwise_xor(a,b,a), Mat, Mat)
joeverbout 0:ea44dc9ed014 276 CV_MAT_AUG_OPERATOR (^=, cv::bitwise_xor(a,b,a), Mat, Scalar)
joeverbout 0:ea44dc9ed014 277 CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a,b,a), Mat_<_Tp>, Mat)
joeverbout 0:ea44dc9ed014 278 CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a,b,a), Mat_<_Tp>, Scalar)
joeverbout 0:ea44dc9ed014 279 CV_MAT_AUG_OPERATOR_T(^=, cv::bitwise_xor(a,b,a), Mat_<_Tp>, Mat_<_Tp>)
joeverbout 0:ea44dc9ed014 280
joeverbout 0:ea44dc9ed014 281 #undef CV_MAT_AUG_OPERATOR_T
joeverbout 0:ea44dc9ed014 282 #undef CV_MAT_AUG_OPERATOR
joeverbout 0:ea44dc9ed014 283 #undef CV_MAT_AUG_OPERATOR1
joeverbout 0:ea44dc9ed014 284
joeverbout 0:ea44dc9ed014 285
joeverbout 0:ea44dc9ed014 286
joeverbout 0:ea44dc9ed014 287 ///////////////////////////////////////////// SVD /////////////////////////////////////////////
joeverbout 0:ea44dc9ed014 288
joeverbout 0:ea44dc9ed014 289 inline SVD::SVD() {}
joeverbout 0:ea44dc9ed014 290 inline SVD::SVD( InputArray m, int flags ) { operator ()(m, flags); }
joeverbout 0:ea44dc9ed014 291 inline void SVD::solveZ( InputArray m, OutputArray _dst )
joeverbout 0:ea44dc9ed014 292 {
joeverbout 0:ea44dc9ed014 293 Mat mtx = m.getMat();
joeverbout 0:ea44dc9ed014 294 SVD svd(mtx, (mtx.rows >= mtx.cols ? 0 : SVD::FULL_UV));
joeverbout 0:ea44dc9ed014 295 _dst.create(svd.vt.cols, 1, svd.vt.type());
joeverbout 0:ea44dc9ed014 296 Mat dst = _dst.getMat();
joeverbout 0:ea44dc9ed014 297 svd.vt.row(svd.vt.rows-1).reshape(1,svd.vt.cols).copyTo(dst);
joeverbout 0:ea44dc9ed014 298 }
joeverbout 0:ea44dc9ed014 299
joeverbout 0:ea44dc9ed014 300 template<typename _Tp, int m, int n, int nm> inline void
joeverbout 0:ea44dc9ed014 301 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w, Matx<_Tp, m, nm>& u, Matx<_Tp, n, nm>& vt )
joeverbout 0:ea44dc9ed014 302 {
joeverbout 0:ea44dc9ed014 303 CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
joeverbout 0:ea44dc9ed014 304 Mat _a(a, false), _u(u, false), _w(w, false), _vt(vt, false);
joeverbout 0:ea44dc9ed014 305 SVD::compute(_a, _w, _u, _vt);
joeverbout 0:ea44dc9ed014 306 CV_Assert(_w.data == (uchar*)&w.val[0] && _u.data == (uchar*)&u.val[0] && _vt.data == (uchar*)&vt.val[0]);
joeverbout 0:ea44dc9ed014 307 }
joeverbout 0:ea44dc9ed014 308
joeverbout 0:ea44dc9ed014 309 template<typename _Tp, int m, int n, int nm> inline void
joeverbout 0:ea44dc9ed014 310 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w )
joeverbout 0:ea44dc9ed014 311 {
joeverbout 0:ea44dc9ed014 312 CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
joeverbout 0:ea44dc9ed014 313 Mat _a(a, false), _w(w, false);
joeverbout 0:ea44dc9ed014 314 SVD::compute(_a, _w);
joeverbout 0:ea44dc9ed014 315 CV_Assert(_w.data == (uchar*)&w.val[0]);
joeverbout 0:ea44dc9ed014 316 }
joeverbout 0:ea44dc9ed014 317
joeverbout 0:ea44dc9ed014 318 template<typename _Tp, int m, int n, int nm, int nb> inline void
joeverbout 0:ea44dc9ed014 319 SVD::backSubst( const Matx<_Tp, nm, 1>& w, const Matx<_Tp, m, nm>& u,
joeverbout 0:ea44dc9ed014 320 const Matx<_Tp, n, nm>& vt, const Matx<_Tp, m, nb>& rhs,
joeverbout 0:ea44dc9ed014 321 Matx<_Tp, n, nb>& dst )
joeverbout 0:ea44dc9ed014 322 {
joeverbout 0:ea44dc9ed014 323 CV_StaticAssert( nm == MIN(m, n), "Invalid size of output vector.");
joeverbout 0:ea44dc9ed014 324 Mat _u(u, false), _w(w, false), _vt(vt, false), _rhs(rhs, false), _dst(dst, false);
joeverbout 0:ea44dc9ed014 325 SVD::backSubst(_w, _u, _vt, _rhs, _dst);
joeverbout 0:ea44dc9ed014 326 CV_Assert(_dst.data == (uchar*)&dst.val[0]);
joeverbout 0:ea44dc9ed014 327 }
joeverbout 0:ea44dc9ed014 328
joeverbout 0:ea44dc9ed014 329
joeverbout 0:ea44dc9ed014 330
joeverbout 0:ea44dc9ed014 331 /////////////////////////////////// Multiply-with-Carry RNG ///////////////////////////////////
joeverbout 0:ea44dc9ed014 332
joeverbout 0:ea44dc9ed014 333 inline RNG::RNG() { state = 0xffffffff; }
joeverbout 0:ea44dc9ed014 334 inline RNG::RNG(uint64 _state) { state = _state ? _state : 0xffffffff; }
joeverbout 0:ea44dc9ed014 335
joeverbout 0:ea44dc9ed014 336 inline RNG::operator uchar() { return (uchar)next(); }
joeverbout 0:ea44dc9ed014 337 inline RNG::operator schar() { return (schar)next(); }
joeverbout 0:ea44dc9ed014 338 inline RNG::operator ushort() { return (ushort)next(); }
joeverbout 0:ea44dc9ed014 339 inline RNG::operator short() { return (short)next(); }
joeverbout 0:ea44dc9ed014 340 inline RNG::operator int() { return (int)next(); }
joeverbout 0:ea44dc9ed014 341 inline RNG::operator unsigned() { return next(); }
joeverbout 0:ea44dc9ed014 342 inline RNG::operator float() { return next()*2.3283064365386962890625e-10f; }
joeverbout 0:ea44dc9ed014 343 inline RNG::operator double() { unsigned t = next(); return (((uint64)t << 32) | next()) * 5.4210108624275221700372640043497e-20; }
joeverbout 0:ea44dc9ed014 344
joeverbout 0:ea44dc9ed014 345 inline unsigned RNG::operator ()(unsigned N) { return (unsigned)uniform(0,N); }
joeverbout 0:ea44dc9ed014 346 inline unsigned RNG::operator ()() { return next(); }
joeverbout 0:ea44dc9ed014 347
joeverbout 0:ea44dc9ed014 348 inline int RNG::uniform(int a, int b) { return a == b ? a : (int)(next() % (b - a) + a); }
joeverbout 0:ea44dc9ed014 349 inline float RNG::uniform(float a, float b) { return ((float)*this)*(b - a) + a; }
joeverbout 0:ea44dc9ed014 350 inline double RNG::uniform(double a, double b) { return ((double)*this)*(b - a) + a; }
joeverbout 0:ea44dc9ed014 351
joeverbout 0:ea44dc9ed014 352 inline unsigned RNG::next()
joeverbout 0:ea44dc9ed014 353 {
joeverbout 0:ea44dc9ed014 354 state = (uint64)(unsigned)state* /*CV_RNG_COEFF*/ 4164903690U + (unsigned)(state >> 32);
joeverbout 0:ea44dc9ed014 355 return (unsigned)state;
joeverbout 0:ea44dc9ed014 356 }
joeverbout 0:ea44dc9ed014 357
joeverbout 0:ea44dc9ed014 358 //! returns the next unifomly-distributed random number of the specified type
joeverbout 0:ea44dc9ed014 359 template<typename _Tp> static inline _Tp randu()
joeverbout 0:ea44dc9ed014 360 {
joeverbout 0:ea44dc9ed014 361 return (_Tp)theRNG();
joeverbout 0:ea44dc9ed014 362 }
joeverbout 0:ea44dc9ed014 363
joeverbout 0:ea44dc9ed014 364 ///////////////////////////////// Formatted string generation /////////////////////////////////
joeverbout 0:ea44dc9ed014 365
joeverbout 0:ea44dc9ed014 366 CV_EXPORTS String format( const char* fmt, ... );
joeverbout 0:ea44dc9ed014 367
joeverbout 0:ea44dc9ed014 368 ///////////////////////////////// Formatted output of cv::Mat /////////////////////////////////
joeverbout 0:ea44dc9ed014 369
joeverbout 0:ea44dc9ed014 370 static inline
joeverbout 0:ea44dc9ed014 371 Ptr<Formatted> format(InputArray mtx, int fmt)
joeverbout 0:ea44dc9ed014 372 {
joeverbout 0:ea44dc9ed014 373 return Formatter::get(fmt)->format(mtx.getMat());
joeverbout 0:ea44dc9ed014 374 }
joeverbout 0:ea44dc9ed014 375
joeverbout 0:ea44dc9ed014 376 static inline
joeverbout 0:ea44dc9ed014 377 int print(Ptr<Formatted> fmtd, FILE* stream = stdout)
joeverbout 0:ea44dc9ed014 378 {
joeverbout 0:ea44dc9ed014 379 int written = 0;
joeverbout 0:ea44dc9ed014 380 fmtd->reset();
joeverbout 0:ea44dc9ed014 381 for(const char* str = fmtd->next(); str; str = fmtd->next())
joeverbout 0:ea44dc9ed014 382 written += fputs(str, stream);
joeverbout 0:ea44dc9ed014 383
joeverbout 0:ea44dc9ed014 384 return written;
joeverbout 0:ea44dc9ed014 385 }
joeverbout 0:ea44dc9ed014 386
joeverbout 0:ea44dc9ed014 387 static inline
joeverbout 0:ea44dc9ed014 388 int print(const Mat& mtx, FILE* stream = stdout)
joeverbout 0:ea44dc9ed014 389 {
joeverbout 0:ea44dc9ed014 390 return print(Formatter::get()->format(mtx), stream);
joeverbout 0:ea44dc9ed014 391 }
joeverbout 0:ea44dc9ed014 392
joeverbout 0:ea44dc9ed014 393 static inline
joeverbout 0:ea44dc9ed014 394 int print(const UMat& mtx, FILE* stream = stdout)
joeverbout 0:ea44dc9ed014 395 {
joeverbout 0:ea44dc9ed014 396 return print(Formatter::get()->format(mtx.getMat(ACCESS_READ)), stream);
joeverbout 0:ea44dc9ed014 397 }
joeverbout 0:ea44dc9ed014 398
joeverbout 0:ea44dc9ed014 399 template<typename _Tp> static inline
joeverbout 0:ea44dc9ed014 400 int print(const std::vector<Point_<_Tp> >& vec, FILE* stream = stdout)
joeverbout 0:ea44dc9ed014 401 {
joeverbout 0:ea44dc9ed014 402 return print(Formatter::get()->format(Mat(vec)), stream);
joeverbout 0:ea44dc9ed014 403 }
joeverbout 0:ea44dc9ed014 404
joeverbout 0:ea44dc9ed014 405 template<typename _Tp> static inline
joeverbout 0:ea44dc9ed014 406 int print(const std::vector<Point3_<_Tp> >& vec, FILE* stream = stdout)
joeverbout 0:ea44dc9ed014 407 {
joeverbout 0:ea44dc9ed014 408 return print(Formatter::get()->format(Mat(vec)), stream);
joeverbout 0:ea44dc9ed014 409 }
joeverbout 0:ea44dc9ed014 410
joeverbout 0:ea44dc9ed014 411 template<typename _Tp, int m, int n> static inline
joeverbout 0:ea44dc9ed014 412 int print(const Matx<_Tp, m, n>& matx, FILE* stream = stdout)
joeverbout 0:ea44dc9ed014 413 {
joeverbout 0:ea44dc9ed014 414 return print(Formatter::get()->format(cv::Mat(matx)), stream);
joeverbout 0:ea44dc9ed014 415 }
joeverbout 0:ea44dc9ed014 416
joeverbout 0:ea44dc9ed014 417 //! @endcond
joeverbout 0:ea44dc9ed014 418
joeverbout 0:ea44dc9ed014 419 /****************************************************************************************\
joeverbout 0:ea44dc9ed014 420 * Auxiliary algorithms *
joeverbout 0:ea44dc9ed014 421 \****************************************************************************************/
joeverbout 0:ea44dc9ed014 422
joeverbout 0:ea44dc9ed014 423 /** @brief Splits an element set into equivalency classes.
joeverbout 0:ea44dc9ed014 424
joeverbout 0:ea44dc9ed014 425 The generic function partition implements an \f$O(N^2)\f$ algorithm for splitting a set of \f$N\f$ elements
joeverbout 0:ea44dc9ed014 426 into one or more equivalency classes, as described in
joeverbout 0:ea44dc9ed014 427 <http://en.wikipedia.org/wiki/Disjoint-set_data_structure> . The function returns the number of
joeverbout 0:ea44dc9ed014 428 equivalency classes.
joeverbout 0:ea44dc9ed014 429 @param _vec Set of elements stored as a vector.
joeverbout 0:ea44dc9ed014 430 @param labels Output vector of labels. It contains as many elements as vec. Each label labels[i] is
joeverbout 0:ea44dc9ed014 431 a 0-based cluster index of `vec[i]`.
joeverbout 0:ea44dc9ed014 432 @param predicate Equivalence predicate (pointer to a boolean function of two arguments or an
joeverbout 0:ea44dc9ed014 433 instance of the class that has the method bool operator()(const _Tp& a, const _Tp& b) ). The
joeverbout 0:ea44dc9ed014 434 predicate returns true when the elements are certainly in the same class, and returns false if they
joeverbout 0:ea44dc9ed014 435 may or may not be in the same class.
joeverbout 0:ea44dc9ed014 436 @ingroup core_cluster
joeverbout 0:ea44dc9ed014 437 */
joeverbout 0:ea44dc9ed014 438 template<typename _Tp, class _EqPredicate> int
joeverbout 0:ea44dc9ed014 439 partition( const std::vector<_Tp>& _vec, std::vector<int>& labels,
joeverbout 0:ea44dc9ed014 440 _EqPredicate predicate=_EqPredicate())
joeverbout 0:ea44dc9ed014 441 {
joeverbout 0:ea44dc9ed014 442 int i, j, N = (int)_vec.size();
joeverbout 0:ea44dc9ed014 443 const _Tp* vec = &_vec[0];
joeverbout 0:ea44dc9ed014 444
joeverbout 0:ea44dc9ed014 445 const int PARENT=0;
joeverbout 0:ea44dc9ed014 446 const int RANK=1;
joeverbout 0:ea44dc9ed014 447
joeverbout 0:ea44dc9ed014 448 std::vector<int> _nodes(N*2);
joeverbout 0:ea44dc9ed014 449 int (*nodes)[2] = (int(*)[2])&_nodes[0];
joeverbout 0:ea44dc9ed014 450
joeverbout 0:ea44dc9ed014 451 // The first O(N) pass: create N single-vertex trees
joeverbout 0:ea44dc9ed014 452 for(i = 0; i < N; i++)
joeverbout 0:ea44dc9ed014 453 {
joeverbout 0:ea44dc9ed014 454 nodes[i][PARENT]=-1;
joeverbout 0:ea44dc9ed014 455 nodes[i][RANK] = 0;
joeverbout 0:ea44dc9ed014 456 }
joeverbout 0:ea44dc9ed014 457
joeverbout 0:ea44dc9ed014 458 // The main O(N^2) pass: merge connected components
joeverbout 0:ea44dc9ed014 459 for( i = 0; i < N; i++ )
joeverbout 0:ea44dc9ed014 460 {
joeverbout 0:ea44dc9ed014 461 int root = i;
joeverbout 0:ea44dc9ed014 462
joeverbout 0:ea44dc9ed014 463 // find root
joeverbout 0:ea44dc9ed014 464 while( nodes[root][PARENT] >= 0 )
joeverbout 0:ea44dc9ed014 465 root = nodes[root][PARENT];
joeverbout 0:ea44dc9ed014 466
joeverbout 0:ea44dc9ed014 467 for( j = 0; j < N; j++ )
joeverbout 0:ea44dc9ed014 468 {
joeverbout 0:ea44dc9ed014 469 if( i == j || !predicate(vec[i], vec[j]))
joeverbout 0:ea44dc9ed014 470 continue;
joeverbout 0:ea44dc9ed014 471 int root2 = j;
joeverbout 0:ea44dc9ed014 472
joeverbout 0:ea44dc9ed014 473 while( nodes[root2][PARENT] >= 0 )
joeverbout 0:ea44dc9ed014 474 root2 = nodes[root2][PARENT];
joeverbout 0:ea44dc9ed014 475
joeverbout 0:ea44dc9ed014 476 if( root2 != root )
joeverbout 0:ea44dc9ed014 477 {
joeverbout 0:ea44dc9ed014 478 // unite both trees
joeverbout 0:ea44dc9ed014 479 int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
joeverbout 0:ea44dc9ed014 480 if( rank > rank2 )
joeverbout 0:ea44dc9ed014 481 nodes[root2][PARENT] = root;
joeverbout 0:ea44dc9ed014 482 else
joeverbout 0:ea44dc9ed014 483 {
joeverbout 0:ea44dc9ed014 484 nodes[root][PARENT] = root2;
joeverbout 0:ea44dc9ed014 485 nodes[root2][RANK] += rank == rank2;
joeverbout 0:ea44dc9ed014 486 root = root2;
joeverbout 0:ea44dc9ed014 487 }
joeverbout 0:ea44dc9ed014 488 CV_Assert( nodes[root][PARENT] < 0 );
joeverbout 0:ea44dc9ed014 489
joeverbout 0:ea44dc9ed014 490 int k = j, parent;
joeverbout 0:ea44dc9ed014 491
joeverbout 0:ea44dc9ed014 492 // compress the path from node2 to root
joeverbout 0:ea44dc9ed014 493 while( (parent = nodes[k][PARENT]) >= 0 )
joeverbout 0:ea44dc9ed014 494 {
joeverbout 0:ea44dc9ed014 495 nodes[k][PARENT] = root;
joeverbout 0:ea44dc9ed014 496 k = parent;
joeverbout 0:ea44dc9ed014 497 }
joeverbout 0:ea44dc9ed014 498
joeverbout 0:ea44dc9ed014 499 // compress the path from node to root
joeverbout 0:ea44dc9ed014 500 k = i;
joeverbout 0:ea44dc9ed014 501 while( (parent = nodes[k][PARENT]) >= 0 )
joeverbout 0:ea44dc9ed014 502 {
joeverbout 0:ea44dc9ed014 503 nodes[k][PARENT] = root;
joeverbout 0:ea44dc9ed014 504 k = parent;
joeverbout 0:ea44dc9ed014 505 }
joeverbout 0:ea44dc9ed014 506 }
joeverbout 0:ea44dc9ed014 507 }
joeverbout 0:ea44dc9ed014 508 }
joeverbout 0:ea44dc9ed014 509
joeverbout 0:ea44dc9ed014 510 // Final O(N) pass: enumerate classes
joeverbout 0:ea44dc9ed014 511 labels.resize(N);
joeverbout 0:ea44dc9ed014 512 int nclasses = 0;
joeverbout 0:ea44dc9ed014 513
joeverbout 0:ea44dc9ed014 514 for( i = 0; i < N; i++ )
joeverbout 0:ea44dc9ed014 515 {
joeverbout 0:ea44dc9ed014 516 int root = i;
joeverbout 0:ea44dc9ed014 517 while( nodes[root][PARENT] >= 0 )
joeverbout 0:ea44dc9ed014 518 root = nodes[root][PARENT];
joeverbout 0:ea44dc9ed014 519 // re-use the rank as the class label
joeverbout 0:ea44dc9ed014 520 if( nodes[root][RANK] >= 0 )
joeverbout 0:ea44dc9ed014 521 nodes[root][RANK] = ~nclasses++;
joeverbout 0:ea44dc9ed014 522 labels[i] = ~nodes[root][RANK];
joeverbout 0:ea44dc9ed014 523 }
joeverbout 0:ea44dc9ed014 524
joeverbout 0:ea44dc9ed014 525 return nclasses;
joeverbout 0:ea44dc9ed014 526 }
joeverbout 0:ea44dc9ed014 527
joeverbout 0:ea44dc9ed014 528 } // cv
joeverbout 0:ea44dc9ed014 529
joeverbout 0:ea44dc9ed014 530 #endif
joeverbout 0:ea44dc9ed014 531