| 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 | // Third party copyrights are property of their respective owners. |
| 17 | // |
| 18 | // Redistribution and use in source and binary forms, with or without modification, |
| 19 | // are permitted provided that the following conditions are met: |
| 20 | // |
| 21 | // * Redistribution's of source code must retain the above copyright notice, |
| 22 | // this list of conditions and the following disclaimer. |
| 23 | // |
| 24 | // * Redistribution's in binary form must reproduce the above copyright notice, |
| 25 | // this list of conditions and the following disclaimer in the documentation |
| 26 | // and/or other materials provided with the distribution. |
| 27 | // |
| 28 | // * The name of the copyright holders may not be used to endorse or promote products |
| 29 | // derived from this software without specific prior written permission. |
| 30 | // |
| 31 | // This software is provided by the copyright holders and contributors "as is" and |
| 32 | // any express or implied warranties, including, but not limited to, the implied |
| 33 | // warranties of merchantability and fitness for a particular purpose are disclaimed. |
| 34 | // In no event shall the Intel Corporation or contributors be liable for any direct, |
| 35 | // indirect, incidental, special, exemplary, or consequential damages |
| 36 | // (including, but not limited to, procurement of substitute goods or services; |
| 37 | // loss of use, data, or profits; or business interruption) however caused |
| 38 | // and on any theory of liability, whether in contract, strict liability, |
| 39 | // or tort (including negligence or otherwise) arising in any way out of |
| 40 | // the use of this software, even if advised of the possibility of such damage. |
| 41 | // |
| 42 | //M*/ |
| 43 | |
| 44 | #ifndef OPENCV_CORE_MAT_HPP |
| 45 | #define OPENCV_CORE_MAT_HPP |
| 46 | |
| 47 | #ifndef __cplusplus |
| 48 | # error mat.hpp header must be compiled as C++ |
| 49 | #endif |
| 50 | |
| 51 | #include "opencv2/core/matx.hpp" |
| 52 | #include "opencv2/core/types.hpp" |
| 53 | |
| 54 | #include "opencv2/core/bufferpool.hpp" |
| 55 | |
| 56 | #include <array> |
| 57 | #include <type_traits> |
| 58 | |
| 59 | namespace cv |
| 60 | { |
| 61 | |
| 62 | //! @addtogroup core_basic |
| 63 | //! @{ |
| 64 | |
| 65 | enum AccessFlag { ACCESS_READ=1<<24, ACCESS_WRITE=1<<25, |
| 66 | ACCESS_RW=3<<24, ACCESS_MASK=ACCESS_RW, ACCESS_FAST=1<<26 }; |
| 67 | CV_ENUM_FLAGS(AccessFlag) |
| 68 | __CV_ENUM_FLAGS_BITWISE_AND(AccessFlag, int, AccessFlag) |
| 69 | |
| 70 | CV__DEBUG_NS_BEGIN |
| 71 | |
| 72 | class CV_EXPORTS _OutputArray; |
| 73 | |
| 74 | //////////////////////// Input/Output Array Arguments ///////////////////////////////// |
| 75 | |
| 76 | /** @brief This is the proxy class for passing read-only input arrays into OpenCV functions. |
| 77 | |
| 78 | It is defined as: |
| 79 | @code |
| 80 | typedef const _InputArray& InputArray; |
| 81 | @endcode |
| 82 | where \ref cv::_InputArray is a class that can be constructed from \ref cv::Mat, \ref cv::Mat_<T>, |
| 83 | \ref cv::Matx<T, m, n>, std::vector<T>, std::vector<std::vector<T>>, std::vector<Mat>, |
| 84 | std::vector<Mat_<T>>, \ref cv::UMat, std::vector<UMat> or `double`. It can also be constructed from |
| 85 | a matrix expression. |
| 86 | |
| 87 | Since this is mostly implementation-level class, and its interface may change in future versions, we |
| 88 | do not describe it in details. There are a few key things, though, that should be kept in mind: |
| 89 | |
| 90 | - When you see in the reference manual or in OpenCV source code a function that takes |
| 91 | InputArray, it means that you can actually pass `Mat`, `Matx`, `vector<T>` etc. (see above the |
| 92 | complete list). |
| 93 | - Optional input arguments: If some of the input arrays may be empty, pass cv::noArray() (or |
| 94 | simply cv::Mat() as you probably did before). |
| 95 | - The class is designed solely for passing parameters. That is, normally you *should not* |
| 96 | declare class members, local and global variables of this type. |
| 97 | - If you want to design your own function or a class method that can operate of arrays of |
| 98 | multiple types, you can use InputArray (or OutputArray) for the respective parameters. Inside |
| 99 | a function you should use _InputArray::getMat() method to construct a matrix header for the |
| 100 | array (without copying data). _InputArray::kind() can be used to distinguish Mat from |
| 101 | `vector<>` etc., but normally it is not needed. |
| 102 | |
| 103 | Here is how you can use a function that takes InputArray : |
| 104 | @code |
| 105 | std::vector<Point2f> vec; |
| 106 | // points or a circle |
| 107 | for( int i = 0; i < 30; i++ ) |
| 108 | vec.push_back(Point2f((float)(100 + 30*cos(i*CV_PI*2/5)), |
| 109 | (float)(100 - 30*sin(i*CV_PI*2/5)))); |
| 110 | cv::transform(vec, vec, cv::Matx23f(0.707, -0.707, 10, 0.707, 0.707, 20)); |
| 111 | @endcode |
| 112 | That is, we form an STL vector containing points, and apply in-place affine transformation to the |
| 113 | vector using the 2x3 matrix created inline as `Matx<float, 2, 3>` instance. |
| 114 | |
| 115 | Here is how such a function can be implemented (for simplicity, we implement a very specific case of |
| 116 | it, according to the assertion statement inside) : |
| 117 | @code |
| 118 | void myAffineTransform(InputArray _src, OutputArray _dst, InputArray _m) |
| 119 | { |
| 120 | // get Mat headers for input arrays. This is O(1) operation, |
| 121 | // unless _src and/or _m are matrix expressions. |
| 122 | Mat src = _src.getMat(), m = _m.getMat(); |
| 123 | CV_Assert( src.type() == CV_32FC2 && m.type() == CV_32F && m.size() == Size(3, 2) ); |
| 124 | |
| 125 | // [re]create the output array so that it has the proper size and type. |
| 126 | // In case of Mat it calls Mat::create, in case of STL vector it calls vector::resize. |
| 127 | _dst.create(src.size(), src.type()); |
| 128 | Mat dst = _dst.getMat(); |
| 129 | |
| 130 | for( int i = 0; i < src.rows; i++ ) |
| 131 | for( int j = 0; j < src.cols; j++ ) |
| 132 | { |
| 133 | Point2f pt = src.at<Point2f>(i, j); |
| 134 | dst.at<Point2f>(i, j) = Point2f(m.at<float>(0, 0)*pt.x + |
| 135 | m.at<float>(0, 1)*pt.y + |
| 136 | m.at<float>(0, 2), |
| 137 | m.at<float>(1, 0)*pt.x + |
| 138 | m.at<float>(1, 1)*pt.y + |
| 139 | m.at<float>(1, 2)); |
| 140 | } |
| 141 | } |
| 142 | @endcode |
| 143 | There is another related type, InputArrayOfArrays, which is currently defined as a synonym for |
| 144 | InputArray: |
| 145 | @code |
| 146 | typedef InputArray InputArrayOfArrays; |
| 147 | @endcode |
| 148 | It denotes function arguments that are either vectors of vectors or vectors of matrices. A separate |
| 149 | synonym is needed to generate Python/Java etc. wrappers properly. At the function implementation |
| 150 | level their use is similar, but _InputArray::getMat(idx) should be used to get header for the |
| 151 | idx-th component of the outer vector and _InputArray::size().area() should be used to find the |
| 152 | number of components (vectors/matrices) of the outer vector. |
| 153 | |
| 154 | In general, type support is limited to cv::Mat types. Other types are forbidden. |
| 155 | But in some cases we need to support passing of custom non-general Mat types, like arrays of cv::KeyPoint, cv::DMatch, etc. |
| 156 | This data is not intended to be interpreted as an image data, or processed somehow like regular cv::Mat. |
| 157 | To pass such custom type use rawIn() / rawOut() / rawInOut() wrappers. |
| 158 | Custom type is wrapped as Mat-compatible `CV_8UC<N>` values (N = sizeof(T), N <= CV_CN_MAX). |
| 159 | */ |
| 160 | class CV_EXPORTS _InputArray |
| 161 | { |
| 162 | public: |
| 163 | enum KindFlag { |
| 164 | KIND_SHIFT = 16, |
| 165 | FIXED_TYPE = 0x8000 << KIND_SHIFT, |
| 166 | FIXED_SIZE = 0x4000 << KIND_SHIFT, |
| 167 | KIND_MASK = 31 << KIND_SHIFT, |
| 168 | |
| 169 | NONE = 0 << KIND_SHIFT, |
| 170 | MAT = 1 << KIND_SHIFT, |
| 171 | MATX = 2 << KIND_SHIFT, |
| 172 | STD_VECTOR = 3 << KIND_SHIFT, |
| 173 | STD_VECTOR_VECTOR = 4 << KIND_SHIFT, |
| 174 | STD_VECTOR_MAT = 5 << KIND_SHIFT, |
| 175 | #if OPENCV_ABI_COMPATIBILITY < 500 |
| 176 | EXPR = 6 << KIND_SHIFT, //!< removed: https://github.com/opencv/opencv/pull/17046 |
| 177 | #endif |
| 178 | OPENGL_BUFFER = 7 << KIND_SHIFT, |
| 179 | CUDA_HOST_MEM = 8 << KIND_SHIFT, |
| 180 | CUDA_GPU_MAT = 9 << KIND_SHIFT, |
| 181 | UMAT =10 << KIND_SHIFT, |
| 182 | STD_VECTOR_UMAT =11 << KIND_SHIFT, |
| 183 | STD_BOOL_VECTOR =12 << KIND_SHIFT, |
| 184 | STD_VECTOR_CUDA_GPU_MAT = 13 << KIND_SHIFT, |
| 185 | #if OPENCV_ABI_COMPATIBILITY < 500 |
| 186 | STD_ARRAY =14 << KIND_SHIFT, //!< removed: https://github.com/opencv/opencv/issues/18897 |
| 187 | #endif |
| 188 | STD_ARRAY_MAT =15 << KIND_SHIFT |
| 189 | }; |
| 190 | |
| 191 | _InputArray(); |
| 192 | _InputArray(int _flags, void* _obj); |
| 193 | _InputArray(const Mat& m); |
| 194 | _InputArray(const MatExpr& expr); |
| 195 | _InputArray(const std::vector<Mat>& vec); |
| 196 | template<typename _Tp> _InputArray(const Mat_<_Tp>& m); |
| 197 | template<typename _Tp> _InputArray(const std::vector<_Tp>& vec); |
| 198 | _InputArray(const std::vector<bool>& vec); |
| 199 | template<typename _Tp> _InputArray(const std::vector<std::vector<_Tp> >& vec); |
| 200 | _InputArray(const std::vector<std::vector<bool> >&) = delete; // not supported |
| 201 | template<typename _Tp> _InputArray(const std::vector<Mat_<_Tp> >& vec); |
| 202 | template<typename _Tp> _InputArray(const _Tp* vec, int n); |
| 203 | template<typename _Tp, int m, int n> _InputArray(const Matx<_Tp, m, n>& matx); |
| 204 | _InputArray(const double& val); |
| 205 | _InputArray(const cuda::GpuMat& d_mat); |
| 206 | _InputArray(const std::vector<cuda::GpuMat>& d_mat_array); |
| 207 | _InputArray(const ogl::Buffer& buf); |
| 208 | _InputArray(const cuda::HostMem& cuda_mem); |
| 209 | template<typename _Tp> _InputArray(const cudev::GpuMat_<_Tp>& m); |
| 210 | _InputArray(const UMat& um); |
| 211 | _InputArray(const std::vector<UMat>& umv); |
| 212 | |
| 213 | template<typename _Tp, std::size_t _Nm> _InputArray(const std::array<_Tp, _Nm>& arr); |
| 214 | template<std::size_t _Nm> _InputArray(const std::array<Mat, _Nm>& arr); |
| 215 | |
| 216 | template<typename _Tp> static _InputArray rawIn(const std::vector<_Tp>& vec); |
| 217 | template<typename _Tp, std::size_t _Nm> static _InputArray rawIn(const std::array<_Tp, _Nm>& arr); |
| 218 | |
| 219 | Mat getMat(int idx=-1) const; |
| 220 | Mat getMat_(int idx=-1) const; |
| 221 | UMat getUMat(int idx=-1) const; |
| 222 | void getMatVector(std::vector<Mat>& mv) const; |
| 223 | void getUMatVector(std::vector<UMat>& umv) const; |
| 224 | void getGpuMatVector(std::vector<cuda::GpuMat>& gpumv) const; |
| 225 | cuda::GpuMat getGpuMat() const; |
| 226 | ogl::Buffer getOGlBuffer() const; |
| 227 | |
| 228 | int getFlags() const; |
| 229 | void* getObj() const; |
| 230 | Size getSz() const; |
| 231 | |
| 232 | _InputArray::KindFlag kind() const; |
| 233 | int dims(int i=-1) const; |
| 234 | int cols(int i=-1) const; |
| 235 | int rows(int i=-1) const; |
| 236 | Size size(int i=-1) const; |
| 237 | int sizend(int* sz, int i=-1) const; |
| 238 | bool sameSize(const _InputArray& arr) const; |
| 239 | size_t total(int i=-1) const; |
| 240 | int type(int i=-1) const; |
| 241 | int depth(int i=-1) const; |
| 242 | int channels(int i=-1) const; |
| 243 | bool isContinuous(int i=-1) const; |
| 244 | bool isSubmatrix(int i=-1) const; |
| 245 | bool empty() const; |
| 246 | void copyTo(const _OutputArray& arr) const; |
| 247 | void copyTo(const _OutputArray& arr, const _InputArray & mask) const; |
| 248 | size_t offset(int i=-1) const; |
| 249 | size_t step(int i=-1) const; |
| 250 | bool isMat() const; |
| 251 | bool isUMat() const; |
| 252 | bool isMatVector() const; |
| 253 | bool isUMatVector() const; |
| 254 | bool isMatx() const; |
| 255 | bool isVector() const; |
| 256 | bool isGpuMat() const; |
| 257 | bool isGpuMatVector() const; |
| 258 | ~_InputArray(); |
| 259 | |
| 260 | protected: |
| 261 | int flags; |
| 262 | void* obj; |
| 263 | Size sz; |
| 264 | |
| 265 | void init(int _flags, const void* _obj); |
| 266 | void init(int _flags, const void* _obj, Size _sz); |
| 267 | }; |
| 268 | CV_ENUM_FLAGS(_InputArray::KindFlag) |
| 269 | __CV_ENUM_FLAGS_BITWISE_AND(_InputArray::KindFlag, int, _InputArray::KindFlag) |
| 270 | |
| 271 | /** @brief This type is very similar to InputArray except that it is used for input/output and output function |
| 272 | parameters. |
| 273 | |
| 274 | Just like with InputArray, OpenCV users should not care about OutputArray, they just pass `Mat`, |
| 275 | `vector<T>` etc. to the functions. The same limitation as for `InputArray`: *Do not explicitly |
| 276 | create OutputArray instances* applies here too. |
| 277 | |
| 278 | If you want to make your function polymorphic (i.e. accept different arrays as output parameters), |
| 279 | it is also not very difficult. Take the sample above as the reference. Note that |
| 280 | _OutputArray::create() needs to be called before _OutputArray::getMat(). This way you guarantee |
| 281 | that the output array is properly allocated. |
| 282 | |
| 283 | Optional output parameters. If you do not need certain output array to be computed and returned to |
| 284 | you, pass cv::noArray(), just like you would in the case of optional input array. At the |
| 285 | implementation level, use _OutputArray::needed() to check if certain output array needs to be |
| 286 | computed or not. |
| 287 | |
| 288 | There are several synonyms for OutputArray that are used to assist automatic Python/Java/... wrapper |
| 289 | generators: |
| 290 | @code |
| 291 | typedef OutputArray OutputArrayOfArrays; |
| 292 | typedef OutputArray InputOutputArray; |
| 293 | typedef OutputArray InputOutputArrayOfArrays; |
| 294 | @endcode |
| 295 | */ |
| 296 | class CV_EXPORTS _OutputArray : public _InputArray |
| 297 | { |
| 298 | public: |
| 299 | enum DepthMask |
| 300 | { |
| 301 | DEPTH_MASK_8U = 1 << CV_8U, |
| 302 | DEPTH_MASK_8S = 1 << CV_8S, |
| 303 | DEPTH_MASK_16U = 1 << CV_16U, |
| 304 | DEPTH_MASK_16S = 1 << CV_16S, |
| 305 | DEPTH_MASK_32S = 1 << CV_32S, |
| 306 | DEPTH_MASK_32F = 1 << CV_32F, |
| 307 | DEPTH_MASK_64F = 1 << CV_64F, |
| 308 | DEPTH_MASK_16F = 1 << CV_16F, |
| 309 | DEPTH_MASK_ALL = (DEPTH_MASK_64F<<1)-1, |
| 310 | DEPTH_MASK_ALL_BUT_8S = DEPTH_MASK_ALL & ~DEPTH_MASK_8S, |
| 311 | DEPTH_MASK_ALL_16F = (DEPTH_MASK_16F<<1)-1, |
| 312 | DEPTH_MASK_FLT = DEPTH_MASK_32F + DEPTH_MASK_64F |
| 313 | }; |
| 314 | |
| 315 | _OutputArray(); |
| 316 | _OutputArray(int _flags, void* _obj); |
| 317 | _OutputArray(Mat& m); |
| 318 | _OutputArray(std::vector<Mat>& vec); |
| 319 | _OutputArray(cuda::GpuMat& d_mat); |
| 320 | _OutputArray(std::vector<cuda::GpuMat>& d_mat); |
| 321 | _OutputArray(ogl::Buffer& buf); |
| 322 | _OutputArray(cuda::HostMem& cuda_mem); |
| 323 | template<typename _Tp> _OutputArray(cudev::GpuMat_<_Tp>& m); |
| 324 | template<typename _Tp> _OutputArray(std::vector<_Tp>& vec); |
| 325 | _OutputArray(std::vector<bool>& vec) = delete; // not supported |
| 326 | template<typename _Tp> _OutputArray(std::vector<std::vector<_Tp> >& vec); |
| 327 | _OutputArray(std::vector<std::vector<bool> >&) = delete; // not supported |
| 328 | template<typename _Tp> _OutputArray(std::vector<Mat_<_Tp> >& vec); |
| 329 | template<typename _Tp> _OutputArray(Mat_<_Tp>& m); |
| 330 | template<typename _Tp> _OutputArray(_Tp* vec, int n); |
| 331 | template<typename _Tp, int m, int n> _OutputArray(Matx<_Tp, m, n>& matx); |
| 332 | _OutputArray(UMat& m); |
| 333 | _OutputArray(std::vector<UMat>& vec); |
| 334 | |
| 335 | _OutputArray(const Mat& m); |
| 336 | _OutputArray(const std::vector<Mat>& vec); |
| 337 | _OutputArray(const cuda::GpuMat& d_mat); |
| 338 | _OutputArray(const std::vector<cuda::GpuMat>& d_mat); |
| 339 | _OutputArray(const ogl::Buffer& buf); |
| 340 | _OutputArray(const cuda::HostMem& cuda_mem); |
| 341 | template<typename _Tp> _OutputArray(const cudev::GpuMat_<_Tp>& m); |
| 342 | template<typename _Tp> _OutputArray(const std::vector<_Tp>& vec); |
| 343 | template<typename _Tp> _OutputArray(const std::vector<std::vector<_Tp> >& vec); |
| 344 | template<typename _Tp> _OutputArray(const std::vector<Mat_<_Tp> >& vec); |
| 345 | template<typename _Tp> _OutputArray(const Mat_<_Tp>& m); |
| 346 | template<typename _Tp> _OutputArray(const _Tp* vec, int n); |
| 347 | template<typename _Tp, int m, int n> _OutputArray(const Matx<_Tp, m, n>& matx); |
| 348 | _OutputArray(const UMat& m); |
| 349 | _OutputArray(const std::vector<UMat>& vec); |
| 350 | |
| 351 | template<typename _Tp, std::size_t _Nm> _OutputArray(std::array<_Tp, _Nm>& arr); |
| 352 | template<typename _Tp, std::size_t _Nm> _OutputArray(const std::array<_Tp, _Nm>& arr); |
| 353 | template<std::size_t _Nm> _OutputArray(std::array<Mat, _Nm>& arr); |
| 354 | template<std::size_t _Nm> _OutputArray(const std::array<Mat, _Nm>& arr); |
| 355 | |
| 356 | template<typename _Tp> static _OutputArray rawOut(std::vector<_Tp>& vec); |
| 357 | template<typename _Tp, std::size_t _Nm> static _OutputArray rawOut(std::array<_Tp, _Nm>& arr); |
| 358 | |
| 359 | bool fixedSize() const; |
| 360 | bool fixedType() const; |
| 361 | bool needed() const; |
| 362 | Mat& getMatRef(int i=-1) const; |
| 363 | UMat& getUMatRef(int i=-1) const; |
| 364 | cuda::GpuMat& getGpuMatRef() const; |
| 365 | std::vector<cuda::GpuMat>& getGpuMatVecRef() const; |
| 366 | ogl::Buffer& getOGlBufferRef() const; |
| 367 | cuda::HostMem& getHostMemRef() const; |
| 368 | void create(Size sz, int type, int i=-1, bool allowTransposed=false, _OutputArray::DepthMask fixedDepthMask=static_cast<_OutputArray::DepthMask>(0)) const; |
| 369 | void create(int rows, int cols, int type, int i=-1, bool allowTransposed=false, _OutputArray::DepthMask fixedDepthMask=static_cast<_OutputArray::DepthMask>(0)) const; |
| 370 | void create(int dims, const int* size, int type, int i=-1, bool allowTransposed=false, _OutputArray::DepthMask fixedDepthMask=static_cast<_OutputArray::DepthMask>(0)) const; |
| 371 | void createSameSize(const _InputArray& arr, int mtype) const; |
| 372 | void release() const; |
| 373 | void clear() const; |
| 374 | void setTo(const _InputArray& value, const _InputArray & mask = _InputArray()) const; |
| 375 | Mat reinterpret( int type ) const; |
| 376 | |
| 377 | void assign(const UMat& u) const; |
| 378 | void assign(const Mat& m) const; |
| 379 | |
| 380 | void assign(const std::vector<UMat>& v) const; |
| 381 | void assign(const std::vector<Mat>& v) const; |
| 382 | |
| 383 | void move(UMat& u) const; |
| 384 | void move(Mat& m) const; |
| 385 | }; |
| 386 | |
| 387 | |
| 388 | class CV_EXPORTS _InputOutputArray : public _OutputArray |
| 389 | { |
| 390 | public: |
| 391 | _InputOutputArray(); |
| 392 | _InputOutputArray(int _flags, void* _obj); |
| 393 | _InputOutputArray(Mat& m); |
| 394 | _InputOutputArray(std::vector<Mat>& vec); |
| 395 | _InputOutputArray(cuda::GpuMat& d_mat); |
| 396 | _InputOutputArray(ogl::Buffer& buf); |
| 397 | _InputOutputArray(cuda::HostMem& cuda_mem); |
| 398 | template<typename _Tp> _InputOutputArray(cudev::GpuMat_<_Tp>& m); |
| 399 | template<typename _Tp> _InputOutputArray(std::vector<_Tp>& vec); |
| 400 | _InputOutputArray(std::vector<bool>& vec) = delete; // not supported |
| 401 | template<typename _Tp> _InputOutputArray(std::vector<std::vector<_Tp> >& vec); |
| 402 | template<typename _Tp> _InputOutputArray(std::vector<Mat_<_Tp> >& vec); |
| 403 | template<typename _Tp> _InputOutputArray(Mat_<_Tp>& m); |
| 404 | template<typename _Tp> _InputOutputArray(_Tp* vec, int n); |
| 405 | template<typename _Tp, int m, int n> _InputOutputArray(Matx<_Tp, m, n>& matx); |
| 406 | _InputOutputArray(UMat& m); |
| 407 | _InputOutputArray(std::vector<UMat>& vec); |
| 408 | |
| 409 | _InputOutputArray(const Mat& m); |
| 410 | _InputOutputArray(const std::vector<Mat>& vec); |
| 411 | _InputOutputArray(const cuda::GpuMat& d_mat); |
| 412 | _InputOutputArray(const std::vector<cuda::GpuMat>& d_mat); |
| 413 | _InputOutputArray(const ogl::Buffer& buf); |
| 414 | _InputOutputArray(const cuda::HostMem& cuda_mem); |
| 415 | template<typename _Tp> _InputOutputArray(const cudev::GpuMat_<_Tp>& m); |
| 416 | template<typename _Tp> _InputOutputArray(const std::vector<_Tp>& vec); |
| 417 | template<typename _Tp> _InputOutputArray(const std::vector<std::vector<_Tp> >& vec); |
| 418 | template<typename _Tp> _InputOutputArray(const std::vector<Mat_<_Tp> >& vec); |
| 419 | template<typename _Tp> _InputOutputArray(const Mat_<_Tp>& m); |
| 420 | template<typename _Tp> _InputOutputArray(const _Tp* vec, int n); |
| 421 | template<typename _Tp, int m, int n> _InputOutputArray(const Matx<_Tp, m, n>& matx); |
| 422 | _InputOutputArray(const UMat& m); |
| 423 | _InputOutputArray(const std::vector<UMat>& vec); |
| 424 | |
| 425 | template<typename _Tp, std::size_t _Nm> _InputOutputArray(std::array<_Tp, _Nm>& arr); |
| 426 | template<typename _Tp, std::size_t _Nm> _InputOutputArray(const std::array<_Tp, _Nm>& arr); |
| 427 | template<std::size_t _Nm> _InputOutputArray(std::array<Mat, _Nm>& arr); |
| 428 | template<std::size_t _Nm> _InputOutputArray(const std::array<Mat, _Nm>& arr); |
| 429 | |
| 430 | template<typename _Tp> static _InputOutputArray rawInOut(std::vector<_Tp>& vec); |
| 431 | template<typename _Tp, std::size_t _Nm> _InputOutputArray rawInOut(std::array<_Tp, _Nm>& arr); |
| 432 | |
| 433 | }; |
| 434 | |
| 435 | /** Helper to wrap custom types. @see InputArray */ |
| 436 | template<typename _Tp> static inline _InputArray rawIn(_Tp& v); |
| 437 | /** Helper to wrap custom types. @see InputArray */ |
| 438 | template<typename _Tp> static inline _OutputArray rawOut(_Tp& v); |
| 439 | /** Helper to wrap custom types. @see InputArray */ |
| 440 | template<typename _Tp> static inline _InputOutputArray rawInOut(_Tp& v); |
| 441 | |
| 442 | CV__DEBUG_NS_END |
| 443 | |
| 444 | typedef const _InputArray& InputArray; |
| 445 | typedef InputArray InputArrayOfArrays; |
| 446 | typedef const _OutputArray& OutputArray; |
| 447 | typedef OutputArray OutputArrayOfArrays; |
| 448 | typedef const _InputOutputArray& InputOutputArray; |
| 449 | typedef InputOutputArray InputOutputArrayOfArrays; |
| 450 | |
| 451 | /** @brief Returns an empty InputArray or OutputArray. |
| 452 | |
| 453 | This function is used to provide an "empty" or "null" array when certain functions |
| 454 | take optional input or output arrays that you don't want to provide. |
| 455 | |
| 456 | Many OpenCV functions accept optional arguments as `cv::InputArray` or `cv::OutputArray`. |
| 457 | When you don't want to pass any data for these optional parameters, you can use `cv::noArray()` |
| 458 | to indicate that you are omitting them. |
| 459 | |
| 460 | @return An empty `cv::InputArray` or `cv::OutputArray` that can be used as a placeholder. |
| 461 | |
| 462 | @note This is often used when a function has optional arrays, and you do not want to |
| 463 | provide a specific input or output array. |
| 464 | |
| 465 | @see cv::InputArray, cv::OutputArray |
| 466 | */ |
| 467 | CV_EXPORTS InputOutputArray noArray(); |
| 468 | |
| 469 | /////////////////////////////////// MatAllocator ////////////////////////////////////// |
| 470 | |
| 471 | /** @brief Usage flags for allocator |
| 472 | |
| 473 | @warning All flags except `USAGE_DEFAULT` are experimental. |
| 474 | |
| 475 | @warning For the OpenCL allocator, `USAGE_ALLOCATE_SHARED_MEMORY` depends on |
| 476 | OpenCV's optional, experimental integration with OpenCL SVM. To enable this |
| 477 | integration, build OpenCV using the `WITH_OPENCL_SVM=ON` CMake option and, at |
| 478 | runtime, call `cv::ocl::Context::getDefault().setUseSVM(true);` or similar |
| 479 | code. Note that SVM is incompatible with OpenCL 1.x. |
| 480 | */ |
| 481 | enum UMatUsageFlags |
| 482 | { |
| 483 | USAGE_DEFAULT = 0, |
| 484 | |
| 485 | // buffer allocation policy is platform and usage specific |
| 486 | USAGE_ALLOCATE_HOST_MEMORY = 1 << 0, |
| 487 | USAGE_ALLOCATE_DEVICE_MEMORY = 1 << 1, |
| 488 | USAGE_ALLOCATE_SHARED_MEMORY = 1 << 2, // It is not equal to: USAGE_ALLOCATE_HOST_MEMORY | USAGE_ALLOCATE_DEVICE_MEMORY |
| 489 | |
| 490 | __UMAT_USAGE_FLAGS_32BIT = 0x7fffffff // Binary compatibility hint |
| 491 | }; |
| 492 | |
| 493 | struct CV_EXPORTS UMatData; |
| 494 | |
| 495 | /** @brief Custom array allocator |
| 496 | */ |
| 497 | class CV_EXPORTS MatAllocator |
| 498 | { |
| 499 | public: |
| 500 | MatAllocator() {} |
| 501 | virtual ~MatAllocator() {} |
| 502 | |
| 503 | // let's comment it off for now to detect and fix all the uses of allocator |
| 504 | //virtual void allocate(int dims, const int* sizes, int type, int*& refcount, |
| 505 | // uchar*& datastart, uchar*& data, size_t* step) = 0; |
| 506 | //virtual void deallocate(int* refcount, uchar* datastart, uchar* data) = 0; |
| 507 | virtual UMatData* allocate(int dims, const int* sizes, int type, |
| 508 | void* data, size_t* step, AccessFlag flags, UMatUsageFlags usageFlags) const = 0; |
| 509 | virtual bool allocate(UMatData* data, AccessFlag accessflags, UMatUsageFlags usageFlags) const = 0; |
| 510 | virtual void deallocate(UMatData* data) const = 0; |
| 511 | virtual void map(UMatData* data, AccessFlag accessflags) const; |
| 512 | virtual void unmap(UMatData* data) const; |
| 513 | virtual void download(UMatData* data, void* dst, int dims, const size_t sz[], |
| 514 | const size_t srcofs[], const size_t srcstep[], |
| 515 | const size_t dststep[]) const; |
| 516 | virtual void upload(UMatData* data, const void* src, int dims, const size_t sz[], |
| 517 | const size_t dstofs[], const size_t dststep[], |
| 518 | const size_t srcstep[]) const; |
| 519 | virtual void copy(UMatData* srcdata, UMatData* dstdata, int dims, const size_t sz[], |
| 520 | const size_t srcofs[], const size_t srcstep[], |
| 521 | const size_t dstofs[], const size_t dststep[], bool sync) const; |
| 522 | |
| 523 | // default implementation returns DummyBufferPoolController |
| 524 | virtual BufferPoolController* getBufferPoolController(const char* id = NULL) const; |
| 525 | }; |
| 526 | |
| 527 | |
| 528 | //////////////////////////////// MatCommaInitializer ////////////////////////////////// |
| 529 | |
| 530 | /** @brief Comma-separated Matrix Initializer |
| 531 | |
| 532 | The class instances are usually not created explicitly. |
| 533 | Instead, they are created on "matrix << firstValue" operator. |
| 534 | |
| 535 | The sample below initializes 2x2 rotation matrix: |
| 536 | |
| 537 | \code |
| 538 | double angle = 30, a = cos(angle*CV_PI/180), b = sin(angle*CV_PI/180); |
| 539 | Mat R = (Mat_<double>(2,2) << a, -b, b, a); |
| 540 | \endcode |
| 541 | */ |
| 542 | template<typename _Tp> class MatCommaInitializer_ |
| 543 | { |
| 544 | public: |
| 545 | //! the constructor, created by "matrix << firstValue" operator, where matrix is cv::Mat |
| 546 | MatCommaInitializer_(Mat_<_Tp>* _m); |
| 547 | //! the operator that takes the next value and put it to the matrix |
| 548 | template<typename T2> MatCommaInitializer_<_Tp>& operator , (T2 v); |
| 549 | //! another form of conversion operator |
| 550 | operator Mat_<_Tp>() const; |
| 551 | protected: |
| 552 | MatIterator_<_Tp> it; |
| 553 | }; |
| 554 | |
| 555 | |
| 556 | /////////////////////////////////////// Mat /////////////////////////////////////////// |
| 557 | |
| 558 | // note that umatdata might be allocated together |
| 559 | // with the matrix data, not as a separate object. |
| 560 | // therefore, it does not have constructor or destructor; |
| 561 | // it should be explicitly initialized using init(). |
| 562 | struct CV_EXPORTS UMatData |
| 563 | { |
| 564 | enum MemoryFlag { COPY_ON_MAP=1, HOST_COPY_OBSOLETE=2, |
| 565 | DEVICE_COPY_OBSOLETE=4, TEMP_UMAT=8, TEMP_COPIED_UMAT=24, |
| 566 | USER_ALLOCATED=32, DEVICE_MEM_MAPPED=64, |
| 567 | ASYNC_CLEANUP=128 |
| 568 | }; |
| 569 | UMatData(const MatAllocator* allocator); |
| 570 | ~UMatData(); |
| 571 | |
| 572 | // provide atomic access to the structure |
| 573 | void lock(); |
| 574 | void unlock(); |
| 575 | |
| 576 | bool hostCopyObsolete() const; |
| 577 | bool deviceCopyObsolete() const; |
| 578 | bool deviceMemMapped() const; |
| 579 | bool copyOnMap() const; |
| 580 | bool tempUMat() const; |
| 581 | bool tempCopiedUMat() const; |
| 582 | void markHostCopyObsolete(bool flag); |
| 583 | void markDeviceCopyObsolete(bool flag); |
| 584 | void markDeviceMemMapped(bool flag); |
| 585 | |
| 586 | const MatAllocator* prevAllocator; |
| 587 | const MatAllocator* currAllocator; |
| 588 | int urefcount; |
| 589 | int refcount; |
| 590 | uchar* data; |
| 591 | uchar* origdata; |
| 592 | size_t size; |
| 593 | |
| 594 | UMatData::MemoryFlag flags; |
| 595 | void* handle; |
| 596 | void* userdata; |
| 597 | int allocatorFlags_; |
| 598 | int mapcount; |
| 599 | UMatData* originalUMatData; |
| 600 | std::shared_ptr<void> allocatorContext; |
| 601 | }; |
| 602 | CV_ENUM_FLAGS(UMatData::MemoryFlag) |
| 603 | |
| 604 | |
| 605 | struct CV_EXPORTS MatSize |
| 606 | { |
| 607 | explicit MatSize(int* _p) CV_NOEXCEPT; |
| 608 | int dims() const CV_NOEXCEPT; |
| 609 | Size operator()() const; |
| 610 | const int& operator[](int i) const; |
| 611 | int& operator[](int i); |
| 612 | operator const int*() const CV_NOEXCEPT; // TODO OpenCV 4.0: drop this |
| 613 | bool operator == (const MatSize& sz) const CV_NOEXCEPT; |
| 614 | bool operator != (const MatSize& sz) const CV_NOEXCEPT; |
| 615 | |
| 616 | int* p; |
| 617 | }; |
| 618 | |
| 619 | struct CV_EXPORTS MatStep |
| 620 | { |
| 621 | MatStep() CV_NOEXCEPT; |
| 622 | explicit MatStep(size_t s) CV_NOEXCEPT; |
| 623 | const size_t& operator[](int i) const CV_NOEXCEPT; |
| 624 | size_t& operator[](int i) CV_NOEXCEPT; |
| 625 | operator size_t() const; |
| 626 | MatStep& operator = (size_t s); |
| 627 | |
| 628 | size_t* p; |
| 629 | size_t buf[2]; |
| 630 | protected: |
| 631 | MatStep& operator = (const MatStep&); |
| 632 | }; |
| 633 | |
| 634 | /** @example samples/cpp/cout_mat.cpp |
| 635 | An example demonstrating the serial out capabilities of cv::Mat |
| 636 | */ |
| 637 | |
| 638 | /** @brief n-dimensional dense array class \anchor CVMat_Details |
| 639 | |
| 640 | The class Mat represents an n-dimensional dense numerical single-channel or multi-channel array. It |
| 641 | can be used to store real or complex-valued vectors and matrices, grayscale or color images, voxel |
| 642 | volumes, vector fields, point clouds, tensors, histograms (though, very high-dimensional histograms |
| 643 | may be better stored in a SparseMat ). The data layout of the array `M` is defined by the array |
| 644 | `M.step[]`, so that the address of element \f$(i_0,...,i_{M.dims-1})\f$, where \f$0\leq i_k<M.size[k]\f$, is |
| 645 | computed as: |
| 646 | \f[addr(M_{i_0,...,i_{M.dims-1}}) = M.data + M.step[0]*i_0 + M.step[1]*i_1 + ... + M.step[M.dims-1]*i_{M.dims-1}\f] |
| 647 | In case of a 2-dimensional array, the above formula is reduced to: |
| 648 | \f[addr(M_{i,j}) = M.data + M.step[0]*i + M.step[1]*j\f] |
| 649 | Note that `M.step[i] >= M.step[i+1]` (in fact, `M.step[i] >= M.step[i+1]*M.size[i+1]` ). This means |
| 650 | that 2-dimensional matrices are stored row-by-row, 3-dimensional matrices are stored plane-by-plane, |
| 651 | and so on. M.step[M.dims-1] is minimal and always equal to the element size M.elemSize() . |
| 652 | |
| 653 | So, the data layout in Mat is compatible with the majority of dense array types from the standard |
| 654 | toolkits and SDKs, such as Numpy (ndarray), Win32 (independent device bitmaps), and others, |
| 655 | that is, with any array that uses *steps* (or *strides*) to compute the position of a pixel. |
| 656 | Due to this compatibility, it is possible to make a Mat header for user-allocated data and process |
| 657 | it in-place using OpenCV functions. |
| 658 | |
| 659 | There are many different ways to create a Mat object. The most popular options are listed below: |
| 660 | |
| 661 | - Use the create(nrows, ncols, type) method or the similar Mat(nrows, ncols, type[, fillValue]) |
| 662 | constructor. A new array of the specified size and type is allocated. type has the same meaning as |
| 663 | in the cvCreateMat method. For example, CV_8UC1 means a 8-bit single-channel array, CV_32FC2 |
| 664 | means a 2-channel (complex) floating-point array, and so on. |
| 665 | @code |
| 666 | // make a 7x7 complex matrix filled with 1+3j. |
| 667 | Mat M(7,7,CV_32FC2,Scalar(1,3)); |
| 668 | // and now turn M to a 100x60 15-channel 8-bit matrix. |
| 669 | // The old content will be deallocated |
| 670 | M.create(100,60,CV_8UC(15)); |
| 671 | @endcode |
| 672 | As noted in the introduction to this chapter, create() allocates only a new array when the shape |
| 673 | or type of the current array are different from the specified ones. |
| 674 | |
| 675 | - Create a multi-dimensional array: |
| 676 | @code |
| 677 | // create a 100x100x100 8-bit array |
| 678 | int sz[] = {100, 100, 100}; |
| 679 | Mat bigCube(3, sz, CV_8U, Scalar::all(0)); |
| 680 | @endcode |
| 681 | It passes the number of dimensions =1 to the Mat constructor but the created array will be |
| 682 | 2-dimensional with the number of columns set to 1. So, Mat::dims is always \>= 2 (can also be 0 |
| 683 | when the array is empty). |
| 684 | |
| 685 | - Use a copy constructor or assignment operator where there can be an array or expression on the |
| 686 | right side (see below). As noted in the introduction, the array assignment is an O(1) operation |
| 687 | because it only copies the header and increases the reference counter. The Mat::clone() method can |
| 688 | be used to get a full (deep) copy of the array when you need it. |
| 689 | |
| 690 | - Construct a header for a part of another array. It can be a single row, single column, several |
| 691 | rows, several columns, rectangular region in the array (called a *minor* in algebra) or a |
| 692 | diagonal. Such operations are also O(1) because the new header references the same data. You can |
| 693 | actually modify a part of the array using this feature, for example: |
| 694 | @code |
| 695 | // add the 5-th row, multiplied by 3 to the 3rd row |
| 696 | M.row(3) = M.row(3) + M.row(5)*3; |
| 697 | // now copy the 7-th column to the 1-st column |
| 698 | // M.col(1) = M.col(7); // this will not work |
| 699 | Mat M1 = M.col(1); |
| 700 | M.col(7).copyTo(M1); |
| 701 | // create a new 320x240 image |
| 702 | Mat img(Size(320,240),CV_8UC3); |
| 703 | // select a ROI |
| 704 | Mat roi(img, Rect(10,10,100,100)); |
| 705 | // fill the ROI with (0,255,0) (which is green in RGB space); |
| 706 | // the original 320x240 image will be modified |
| 707 | roi = Scalar(0,255,0); |
| 708 | @endcode |
| 709 | Due to the additional datastart and dataend members, it is possible to compute a relative |
| 710 | sub-array position in the main *container* array using locateROI(): |
| 711 | @code |
| 712 | Mat A = Mat::eye(10, 10, CV_32S); |
| 713 | // extracts A columns, 1 (inclusive) to 3 (exclusive). |
| 714 | Mat B = A(Range::all(), Range(1, 3)); |
| 715 | // extracts B rows, 5 (inclusive) to 9 (exclusive). |
| 716 | // that is, C \~ A(Range(5, 9), Range(1, 3)) |
| 717 | Mat C = B(Range(5, 9), Range::all()); |
| 718 | Size size; Point ofs; |
| 719 | C.locateROI(size, ofs); |
| 720 | // size will be (width=10,height=10) and the ofs will be (x=1, y=5) |
| 721 | @endcode |
| 722 | As in case of whole matrices, if you need a deep copy, use the `clone()` method of the extracted |
| 723 | sub-matrices. |
| 724 | |
| 725 | - Make a header for user-allocated data. It can be useful to do the following: |
| 726 | -# Process "foreign" data using OpenCV (for example, when you implement a DirectShow\* filter or |
| 727 | a processing module for gstreamer, and so on). For example: |
| 728 | @code |
| 729 | Mat process_video_frame(const unsigned char* pixels, |
| 730 | int width, int height, int step) |
| 731 | { |
| 732 | // wrap input buffer |
| 733 | Mat img(height, width, CV_8UC3, (unsigned char*)pixels, step); |
| 734 | |
| 735 | Mat result; |
| 736 | GaussianBlur(img, result, Size(7, 7), 1.5, 1.5); |
| 737 | |
| 738 | return result; |
| 739 | } |
| 740 | @endcode |
| 741 | -# Quickly initialize small matrices and/or get a super-fast element access. |
| 742 | @code |
| 743 | double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}}; |
| 744 | Mat M = Mat(3, 3, CV_64F, m).inv(); |
| 745 | @endcode |
| 746 | . |
| 747 | |
| 748 | - Use MATLAB-style array initializers, zeros(), ones(), eye(), for example: |
| 749 | @code |
| 750 | // create a double-precision identity matrix and add it to M. |
| 751 | M += Mat::eye(M.rows, M.cols, CV_64F); |
| 752 | @endcode |
| 753 | |
| 754 | - Use a comma-separated initializer: |
| 755 | @code |
| 756 | // create a 3x3 double-precision identity matrix |
| 757 | Mat M = (Mat_<double>(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1); |
| 758 | @endcode |
| 759 | With this approach, you first call a constructor of the Mat class with the proper parameters, and |
| 760 | then you just put `<< operator` followed by comma-separated values that can be constants, |
| 761 | variables, expressions, and so on. Also, note the extra parentheses required to avoid compilation |
| 762 | errors. |
| 763 | |
| 764 | Once the array is created, it is automatically managed via a reference-counting mechanism. If the |
| 765 | array header is built on top of user-allocated data, you should handle the data by yourself. The |
| 766 | array data is deallocated when no one points to it. If you want to release the data pointed by a |
| 767 | array header before the array destructor is called, use Mat::release(). |
| 768 | |
| 769 | The next important thing to learn about the array class is element access. This manual already |
| 770 | described how to compute an address of each array element. Normally, you are not required to use the |
| 771 | formula directly in the code. If you know the array element type (which can be retrieved using the |
| 772 | method Mat::type() ), you can access the element \f$M_{ij}\f$ of a 2-dimensional array as: |
| 773 | @code |
| 774 | M.at<double>(i,j) += 1.f; |
| 775 | @endcode |
| 776 | assuming that `M` is a double-precision floating-point array. There are several variants of the method |
| 777 | at for a different number of dimensions. |
| 778 | |
| 779 | If you need to process a whole row of a 2D array, the most efficient way is to get the pointer to |
| 780 | the row first, and then just use the plain C operator [] : |
| 781 | @code |
| 782 | // compute sum of positive matrix elements |
| 783 | // (assuming that M is a double-precision matrix) |
| 784 | double sum=0; |
| 785 | for(int i = 0; i < M.rows; i++) |
| 786 | { |
| 787 | const double* Mi = M.ptr<double>(i); |
| 788 | for(int j = 0; j < M.cols; j++) |
| 789 | sum += std::max(Mi[j], 0.); |
| 790 | } |
| 791 | @endcode |
| 792 | Some operations, like the one above, do not actually depend on the array shape. They just process |
| 793 | elements of an array one by one (or elements from multiple arrays that have the same coordinates, |
| 794 | for example, array addition). Such operations are called *element-wise*. It makes sense to check |
| 795 | whether all the input/output arrays are continuous, namely, have no gaps at the end of each row. If |
| 796 | yes, process them as a long single row: |
| 797 | @code |
| 798 | // compute the sum of positive matrix elements, optimized variant |
| 799 | double sum=0; |
| 800 | int cols = M.cols, rows = M.rows; |
| 801 | if(M.isContinuous()) |
| 802 | { |
| 803 | cols *= rows; |
| 804 | rows = 1; |
| 805 | } |
| 806 | for(int i = 0; i < rows; i++) |
| 807 | { |
| 808 | const double* Mi = M.ptr<double>(i); |
| 809 | for(int j = 0; j < cols; j++) |
| 810 | sum += std::max(Mi[j], 0.); |
| 811 | } |
| 812 | @endcode |
| 813 | In case of the continuous matrix, the outer loop body is executed just once. So, the overhead is |
| 814 | smaller, which is especially noticeable in case of small matrices. |
| 815 | |
| 816 | Finally, there are STL-style iterators that are smart enough to skip gaps between successive rows: |
| 817 | @code |
| 818 | // compute sum of positive matrix elements, iterator-based variant |
| 819 | double sum=0; |
| 820 | MatConstIterator_<double> it = M.begin<double>(), it_end = M.end<double>(); |
| 821 | for(; it != it_end; ++it) |
| 822 | sum += std::max(*it, 0.); |
| 823 | @endcode |
| 824 | The matrix iterators are random-access iterators, so they can be passed to any STL algorithm, |
| 825 | including std::sort(). |
| 826 | |
| 827 | @note Matrix Expressions and arithmetic see MatExpr |
| 828 | */ |
| 829 | class CV_EXPORTS Mat |
| 830 | { |
| 831 | public: |
| 832 | /** |
| 833 | These are various constructors that form a matrix. As noted in the AutomaticAllocation, often |
| 834 | the default constructor is enough, and the proper matrix will be allocated by an OpenCV function. |
| 835 | The constructed matrix can further be assigned to another matrix or matrix expression or can be |
| 836 | allocated with Mat::create . In the former case, the old content is de-referenced. |
| 837 | */ |
| 838 | Mat() CV_NOEXCEPT; |
| 839 | |
| 840 | /** @overload |
| 841 | @param rows Number of rows in a 2D array. |
| 842 | @param cols Number of columns in a 2D array. |
| 843 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 844 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 845 | */ |
| 846 | Mat(int rows, int cols, int type); |
| 847 | |
| 848 | /** @overload |
| 849 | @param size 2D array size: Size(cols, rows) . In the Size() constructor, the number of rows and the |
| 850 | number of columns go in the reverse order. |
| 851 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 852 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 853 | */ |
| 854 | Mat(Size size, int type); |
| 855 | |
| 856 | /** @overload |
| 857 | @param rows Number of rows in a 2D array. |
| 858 | @param cols Number of columns in a 2D array. |
| 859 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 860 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 861 | @param s An optional value to initialize each matrix element with. To set all the matrix elements to |
| 862 | the particular value after the construction, use the assignment operator |
| 863 | Mat::operator=(const Scalar& value) . |
| 864 | */ |
| 865 | Mat(int rows, int cols, int type, const Scalar& s); |
| 866 | |
| 867 | /** @overload |
| 868 | @param size 2D array size: Size(cols, rows) . In the Size() constructor, the number of rows and the |
| 869 | number of columns go in the reverse order. |
| 870 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 871 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 872 | @param s An optional value to initialize each matrix element with. To set all the matrix elements to |
| 873 | the particular value after the construction, use the assignment operator |
| 874 | Mat::operator=(const Scalar& value) . |
| 875 | */ |
| 876 | Mat(Size size, int type, const Scalar& s); |
| 877 | |
| 878 | /** @overload |
| 879 | @param ndims Array dimensionality. |
| 880 | @param sizes Array of integers specifying an n-dimensional array shape. |
| 881 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 882 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 883 | */ |
| 884 | Mat(int ndims, const int* sizes, int type); |
| 885 | |
| 886 | /** @overload |
| 887 | @param sizes Array of integers specifying an n-dimensional array shape. |
| 888 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 889 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 890 | */ |
| 891 | Mat(const std::vector<int>& sizes, int type); |
| 892 | |
| 893 | /** @overload |
| 894 | @param ndims Array dimensionality. |
| 895 | @param sizes Array of integers specifying an n-dimensional array shape. |
| 896 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 897 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 898 | @param s An optional value to initialize each matrix element with. To set all the matrix elements to |
| 899 | the particular value after the construction, use the assignment operator |
| 900 | Mat::operator=(const Scalar& value) . |
| 901 | */ |
| 902 | Mat(int ndims, const int* sizes, int type, const Scalar& s); |
| 903 | |
| 904 | /** @overload |
| 905 | @param sizes Array of integers specifying an n-dimensional array shape. |
| 906 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 907 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 908 | @param s An optional value to initialize each matrix element with. To set all the matrix elements to |
| 909 | the particular value after the construction, use the assignment operator |
| 910 | Mat::operator=(const Scalar& value) . |
| 911 | */ |
| 912 | Mat(const std::vector<int>& sizes, int type, const Scalar& s); |
| 913 | |
| 914 | |
| 915 | /** @overload |
| 916 | @param m Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied |
| 917 | by these constructors. Instead, the header pointing to m data or its sub-array is constructed and |
| 918 | associated with it. The reference counter, if any, is incremented. So, when you modify the matrix |
| 919 | formed using such a constructor, you also modify the corresponding elements of m . If you want to |
| 920 | have an independent copy of the sub-array, use Mat::clone() . |
| 921 | */ |
| 922 | Mat(const Mat& m); |
| 923 | |
| 924 | /** @overload |
| 925 | @param rows Number of rows in a 2D array. |
| 926 | @param cols Number of columns in a 2D array. |
| 927 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 928 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 929 | @param data Pointer to the user data. Matrix constructors that take data and step parameters do not |
| 930 | allocate matrix data. Instead, they just initialize the matrix header that points to the specified |
| 931 | data, which means that no data is copied. This operation is very efficient and can be used to |
| 932 | process external data using OpenCV functions. The external data is not automatically deallocated, so |
| 933 | you should take care of it. |
| 934 | @param step Number of bytes each matrix row occupies. The value should include the padding bytes at |
| 935 | the end of each row, if any. If the parameter is missing (set to AUTO_STEP ), no padding is assumed |
| 936 | and the actual step is calculated as cols*elemSize(). See Mat::elemSize. |
| 937 | */ |
| 938 | Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP); |
| 939 | |
| 940 | /** @overload |
| 941 | @param size 2D array size: Size(cols, rows) . In the Size() constructor, the number of rows and the |
| 942 | number of columns go in the reverse order. |
| 943 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 944 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 945 | @param data Pointer to the user data. Matrix constructors that take data and step parameters do not |
| 946 | allocate matrix data. Instead, they just initialize the matrix header that points to the specified |
| 947 | data, which means that no data is copied. This operation is very efficient and can be used to |
| 948 | process external data using OpenCV functions. The external data is not automatically deallocated, so |
| 949 | you should take care of it. |
| 950 | @param step Number of bytes each matrix row occupies. The value should include the padding bytes at |
| 951 | the end of each row, if any. If the parameter is missing (set to AUTO_STEP ), no padding is assumed |
| 952 | and the actual step is calculated as cols*elemSize(). See Mat::elemSize. |
| 953 | */ |
| 954 | Mat(Size size, int type, void* data, size_t step=AUTO_STEP); |
| 955 | |
| 956 | /** @overload |
| 957 | @param ndims Array dimensionality. |
| 958 | @param sizes Array of integers specifying an n-dimensional array shape. |
| 959 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 960 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 961 | @param data Pointer to the user data. Matrix constructors that take data and step parameters do not |
| 962 | allocate matrix data. Instead, they just initialize the matrix header that points to the specified |
| 963 | data, which means that no data is copied. This operation is very efficient and can be used to |
| 964 | process external data using OpenCV functions. The external data is not automatically deallocated, so |
| 965 | you should take care of it. |
| 966 | @param steps Array of ndims-1 steps in case of a multi-dimensional array (the last step is always |
| 967 | set to the element size). If not specified, the matrix is assumed to be continuous. |
| 968 | */ |
| 969 | Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0); |
| 970 | |
| 971 | /** @overload |
| 972 | @param sizes Array of integers specifying an n-dimensional array shape. |
| 973 | @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or |
| 974 | CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. |
| 975 | @param data Pointer to the user data. Matrix constructors that take data and step parameters do not |
| 976 | allocate matrix data. Instead, they just initialize the matrix header that points to the specified |
| 977 | data, which means that no data is copied. This operation is very efficient and can be used to |
| 978 | process external data using OpenCV functions. The external data is not automatically deallocated, so |
| 979 | you should take care of it. |
| 980 | @param steps Array of ndims-1 steps in case of a multi-dimensional array (the last step is always |
| 981 | set to the element size). If not specified, the matrix is assumed to be continuous. |
| 982 | */ |
| 983 | Mat(const std::vector<int>& sizes, int type, void* data, const size_t* steps=0); |
| 984 | |
| 985 | /** @overload |
| 986 | @param m Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied |
| 987 | by these constructors. Instead, the header pointing to m data or its sub-array is constructed and |
| 988 | associated with it. The reference counter, if any, is incremented. So, when you modify the matrix |
| 989 | formed using such a constructor, you also modify the corresponding elements of m . If you want to |
| 990 | have an independent copy of the sub-array, use Mat::clone() . |
| 991 | @param rowRange Range of the m rows to take. As usual, the range start is inclusive and the range |
| 992 | end is exclusive. Use Range::all() to take all the rows. |
| 993 | @param colRange Range of the m columns to take. Use Range::all() to take all the columns. |
| 994 | */ |
| 995 | Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all()); |
| 996 | |
| 997 | /** @overload |
| 998 | @param m Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied |
| 999 | by these constructors. Instead, the header pointing to m data or its sub-array is constructed and |
| 1000 | associated with it. The reference counter, if any, is incremented. So, when you modify the matrix |
| 1001 | formed using such a constructor, you also modify the corresponding elements of m . If you want to |
| 1002 | have an independent copy of the sub-array, use Mat::clone() . |
| 1003 | @param roi Region of interest. |
| 1004 | */ |
| 1005 | Mat(const Mat& m, const Rect& roi); |
| 1006 | |
| 1007 | /** @overload |
| 1008 | @param m Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied |
| 1009 | by these constructors. Instead, the header pointing to m data or its sub-array is constructed and |
| 1010 | associated with it. The reference counter, if any, is incremented. So, when you modify the matrix |
| 1011 | formed using such a constructor, you also modify the corresponding elements of m . If you want to |
| 1012 | have an independent copy of the sub-array, use Mat::clone() . |
| 1013 | @param ranges Array of selected ranges of m along each dimensionality. |
| 1014 | */ |
| 1015 | Mat(const Mat& m, const Range* ranges); |
| 1016 | |
| 1017 | /** @overload |
| 1018 | @param m Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied |
| 1019 | by these constructors. Instead, the header pointing to m data or its sub-array is constructed and |
| 1020 | associated with it. The reference counter, if any, is incremented. So, when you modify the matrix |
| 1021 | formed using such a constructor, you also modify the corresponding elements of m . If you want to |
| 1022 | have an independent copy of the sub-array, use Mat::clone() . |
| 1023 | @param ranges Array of selected ranges of m along each dimensionality. |
| 1024 | */ |
| 1025 | Mat(const Mat& m, const std::vector<Range>& ranges); |
| 1026 | |
| 1027 | /** @overload |
| 1028 | @param vec STL vector whose elements form the matrix. The matrix has a single column and the number |
| 1029 | of rows equal to the number of vector elements. Type of the matrix matches the type of vector |
| 1030 | elements. The constructor can handle arbitrary types, for which there is a properly declared |
| 1031 | DataType . This means that the vector elements must be primitive numbers or uni-type numerical |
| 1032 | tuples of numbers. Mixed-type structures are not supported. The corresponding constructor is |
| 1033 | explicit. Since STL vectors are not automatically converted to Mat instances, you should write |
| 1034 | Mat(vec) explicitly. Unless you copy the data into the matrix ( copyData=true ), no new elements |
| 1035 | will be added to the vector because it can potentially yield vector data reallocation, and, thus, |
| 1036 | the matrix data pointer will be invalid. |
| 1037 | @param copyData Flag to specify whether the underlying data of the STL vector should be copied |
| 1038 | to (true) or shared with (false) the newly constructed matrix. When the data is copied, the |
| 1039 | allocated buffer is managed using Mat reference counting mechanism. While the data is shared, |
| 1040 | the reference counter is NULL, and you should not deallocate the data until the matrix is |
| 1041 | destructed. |
| 1042 | */ |
| 1043 | template<typename _Tp> explicit Mat(const std::vector<_Tp>& vec, bool copyData=false); |
| 1044 | |
| 1045 | /** @overload |
| 1046 | */ |
| 1047 | template<typename _Tp, typename = typename std::enable_if<std::is_arithmetic<_Tp>::value>::type> |
| 1048 | explicit Mat(const std::initializer_list<_Tp> list); |
| 1049 | |
| 1050 | /** @overload |
| 1051 | */ |
| 1052 | template<typename _Tp> explicit Mat(const std::initializer_list<int> sizes, const std::initializer_list<_Tp> list); |
| 1053 | |
| 1054 | /** @overload |
| 1055 | */ |
| 1056 | template<typename _Tp, size_t _Nm> explicit Mat(const std::array<_Tp, _Nm>& arr, bool copyData=false); |
| 1057 | |
| 1058 | /** @overload |
| 1059 | */ |
| 1060 | template<typename _Tp, int n> explicit Mat(const Vec<_Tp, n>& vec, bool copyData=true); |
| 1061 | |
| 1062 | /** @overload |
| 1063 | */ |
| 1064 | template<typename _Tp, int m, int n> explicit Mat(const Matx<_Tp, m, n>& mtx, bool copyData=true); |
| 1065 | |
| 1066 | /** @overload |
| 1067 | */ |
| 1068 | template<typename _Tp> explicit Mat(const Point_<_Tp>& pt, bool copyData=true); |
| 1069 | |
| 1070 | /** @overload |
| 1071 | */ |
| 1072 | template<typename _Tp> explicit Mat(const Point3_<_Tp>& pt, bool copyData=true); |
| 1073 | |
| 1074 | /** @overload |
| 1075 | */ |
| 1076 | template<typename _Tp> explicit Mat(const MatCommaInitializer_<_Tp>& commaInitializer); |
| 1077 | |
| 1078 | //! download data from GpuMat |
| 1079 | explicit Mat(const cuda::GpuMat& m); |
| 1080 | |
| 1081 | //! destructor - calls release() |
| 1082 | ~Mat(); |
| 1083 | |
| 1084 | /** @brief assignment operators |
| 1085 | |
| 1086 | These are available assignment operators. Since they all are very different, make sure to read the |
| 1087 | operator parameters description. |
| 1088 | @param m Assigned, right-hand-side matrix. Matrix assignment is an O(1) operation. This means that |
| 1089 | no data is copied but the data is shared and the reference counter, if any, is incremented. Before |
| 1090 | assigning new data, the old data is de-referenced via Mat::release . |
| 1091 | */ |
| 1092 | Mat& operator = (const Mat& m); |
| 1093 | |
| 1094 | /** @overload |
| 1095 | @param expr Assigned matrix expression object. As opposite to the first form of the assignment |
| 1096 | operation, the second form can reuse already allocated matrix if it has the right size and type to |
| 1097 | fit the matrix expression result. It is automatically handled by the real function that the matrix |
| 1098 | expressions is expanded to. For example, C=A+B is expanded to add(A, B, C), and add takes care of |
| 1099 | automatic C reallocation. |
| 1100 | */ |
| 1101 | Mat& operator = (const MatExpr& expr); |
| 1102 | |
| 1103 | //! retrieve UMat from Mat |
| 1104 | UMat getUMat(AccessFlag accessFlags, UMatUsageFlags usageFlags = USAGE_DEFAULT) const; |
| 1105 | |
| 1106 | /** @brief Creates a matrix header for the specified matrix row. |
| 1107 | |
| 1108 | The method makes a new header for the specified matrix row and returns it. This is an O(1) |
| 1109 | operation, regardless of the matrix size. The underlying data of the new matrix is shared with the |
| 1110 | original matrix. Here is the example of one of the classical basic matrix processing operations, |
| 1111 | axpy, used by LU and many other algorithms: |
| 1112 | @code |
| 1113 | inline void matrix_axpy(Mat& A, int i, int j, double alpha) |
| 1114 | { |
| 1115 | A.row(i) += A.row(j)*alpha; |
| 1116 | } |
| 1117 | @endcode |
| 1118 | @note In the current implementation, the following code does not work as expected: |
| 1119 | @code |
| 1120 | Mat A; |
| 1121 | ... |
| 1122 | A.row(i) = A.row(j); // will not work |
| 1123 | @endcode |
| 1124 | This happens because A.row(i) forms a temporary header that is further assigned to another header. |
| 1125 | Remember that each of these operations is O(1), that is, no data is copied. Thus, the above |
| 1126 | assignment is not true if you may have expected the j-th row to be copied to the i-th row. To |
| 1127 | achieve that, you should either turn this simple assignment into an expression or use the |
| 1128 | Mat::copyTo method: |
| 1129 | @code |
| 1130 | Mat A; |
| 1131 | ... |
| 1132 | // works, but looks a bit obscure. |
| 1133 | A.row(i) = A.row(j) + 0; |
| 1134 | // this is a bit longer, but the recommended method. |
| 1135 | A.row(j).copyTo(A.row(i)); |
| 1136 | @endcode |
| 1137 | @param y A 0-based row index. |
| 1138 | */ |
| 1139 | Mat row(int y) const; |
| 1140 | |
| 1141 | /** @brief Creates a matrix header for the specified matrix column. |
| 1142 | |
| 1143 | The method makes a new header for the specified matrix column and returns it. This is an O(1) |
| 1144 | operation, regardless of the matrix size. The underlying data of the new matrix is shared with the |
| 1145 | original matrix. See also the Mat::row description. |
| 1146 | @param x A 0-based column index. |
| 1147 | */ |
| 1148 | Mat col(int x) const; |
| 1149 | |
| 1150 | /** @brief Creates a matrix header for the specified row span. |
| 1151 | |
| 1152 | The method makes a new header for the specified row span of the matrix. Similarly to Mat::row and |
| 1153 | Mat::col , this is an O(1) operation. |
| 1154 | @param startrow An inclusive 0-based start index of the row span. |
| 1155 | @param endrow An exclusive 0-based ending index of the row span. |
| 1156 | */ |
| 1157 | Mat rowRange(int startrow, int endrow) const; |
| 1158 | |
| 1159 | /** @overload |
| 1160 | @param r Range structure containing both the start and the end indices. |
| 1161 | */ |
| 1162 | Mat rowRange(const Range& r) const; |
| 1163 | |
| 1164 | /** @brief Creates a matrix header for the specified column span. |
| 1165 | |
| 1166 | The method makes a new header for the specified column span of the matrix. Similarly to Mat::row and |
| 1167 | Mat::col , this is an O(1) operation. |
| 1168 | @param startcol An inclusive 0-based start index of the column span. |
| 1169 | @param endcol An exclusive 0-based ending index of the column span. |
| 1170 | */ |
| 1171 | Mat colRange(int startcol, int endcol) const; |
| 1172 | |
| 1173 | /** @overload |
| 1174 | @param r Range structure containing both the start and the end indices. |
| 1175 | */ |
| 1176 | Mat colRange(const Range& r) const; |
| 1177 | |
| 1178 | /** @brief Extracts a diagonal from a matrix |
| 1179 | |
| 1180 | The method makes a new header for the specified matrix diagonal. The new matrix is represented as a |
| 1181 | single-column matrix. Similarly to Mat::row and Mat::col, this is an O(1) operation. |
| 1182 | @param d index of the diagonal, with the following values: |
| 1183 | - `d=0` is the main diagonal. |
| 1184 | - `d<0` is a diagonal from the lower half. For example, d=-1 means the diagonal is set |
| 1185 | immediately below the main one. |
| 1186 | - `d>0` is a diagonal from the upper half. For example, d=1 means the diagonal is set |
| 1187 | immediately above the main one. |
| 1188 | For example: |
| 1189 | @code |
| 1190 | Mat m = (Mat_<int>(3,3) << |
| 1191 | 1,2,3, |
| 1192 | 4,5,6, |
| 1193 | 7,8,9); |
| 1194 | Mat d0 = m.diag(0); |
| 1195 | Mat d1 = m.diag(1); |
| 1196 | Mat d_1 = m.diag(-1); |
| 1197 | @endcode |
| 1198 | The resulting matrices are |
| 1199 | @code |
| 1200 | d0 = |
| 1201 | [1; |
| 1202 | 5; |
| 1203 | 9] |
| 1204 | d1 = |
| 1205 | [2; |
| 1206 | 6] |
| 1207 | d_1 = |
| 1208 | [4; |
| 1209 | 8] |
| 1210 | @endcode |
| 1211 | */ |
| 1212 | Mat diag(int d=0) const; |
| 1213 | |
| 1214 | /** @brief creates a diagonal matrix |
| 1215 | |
| 1216 | The method creates a square diagonal matrix from specified main diagonal. |
| 1217 | @param d One-dimensional matrix that represents the main diagonal. |
| 1218 | */ |
| 1219 | CV_NODISCARD_STD static Mat diag(const Mat& d); |
| 1220 | |
| 1221 | /** @brief Creates a full copy of the array and the underlying data. |
| 1222 | |
| 1223 | The method creates a full copy of the array. The original step[] is not taken into account. So, the |
| 1224 | array copy is a continuous array occupying total()*elemSize() bytes. |
| 1225 | */ |
| 1226 | CV_NODISCARD_STD Mat clone() const; |
| 1227 | |
| 1228 | /** @brief Copies the matrix to another one. |
| 1229 | |
| 1230 | The method copies the matrix data to another matrix. Before copying the data, the method invokes : |
| 1231 | @code |
| 1232 | m.create(this->size(), this->type()); |
| 1233 | @endcode |
| 1234 | so that the destination matrix is reallocated if needed. While m.copyTo(m); works flawlessly, the |
| 1235 | function does not handle the case of a partial overlap between the source and the destination |
| 1236 | matrices. |
| 1237 | |
| 1238 | When the operation mask is specified, if the Mat::create call shown above reallocates the matrix, |
| 1239 | the newly allocated matrix is initialized with all zeros before copying the data. |
| 1240 | @param m Destination matrix. If it does not have a proper size or type before the operation, it is |
| 1241 | reallocated. |
| 1242 | */ |
| 1243 | void copyTo( OutputArray m ) const; |
| 1244 | |
| 1245 | /** @overload |
| 1246 | @param m Destination matrix. If it does not have a proper size or type before the operation, it is |
| 1247 | reallocated. |
| 1248 | @param mask Operation mask of the same size as \*this. Its non-zero elements indicate which matrix |
| 1249 | elements need to be copied. The mask has to be of type CV_8U and can have 1 or multiple channels. |
| 1250 | */ |
| 1251 | void copyTo( OutputArray m, InputArray mask ) const; |
| 1252 | |
| 1253 | /** @brief Converts an array to another data type with optional scaling. |
| 1254 | |
| 1255 | The method converts source pixel values to the target data type. saturate_cast\<\> is applied at |
| 1256 | the end to avoid possible overflows: |
| 1257 | |
| 1258 | \f[m(x,y) = saturate \_ cast<rType>( \alpha (*this)(x,y) + \beta )\f] |
| 1259 | @param m output matrix; if it does not have a proper size or type before the operation, it is |
| 1260 | reallocated. |
| 1261 | @param rtype desired output matrix type or, rather, the depth since the number of channels are the |
| 1262 | same as the input has; if rtype is negative, the output matrix will have the same type as the input. |
| 1263 | @param alpha optional scale factor. |
| 1264 | @param beta optional delta added to the scaled values. |
| 1265 | */ |
| 1266 | void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const; |
| 1267 | |
| 1268 | /** @brief Provides a functional form of convertTo. |
| 1269 | |
| 1270 | This is an internally used method called by the @ref MatrixExpressions engine. |
| 1271 | @param m Destination array. |
| 1272 | @param type Desired destination array depth (or -1 if it should be the same as the source type). |
| 1273 | */ |
| 1274 | void assignTo( Mat& m, int type=-1 ) const; |
| 1275 | |
| 1276 | /** @brief Sets all or some of the array elements to the specified value. |
| 1277 | @param s Assigned scalar converted to the actual array type. |
| 1278 | */ |
| 1279 | Mat& operator = (const Scalar& s); |
| 1280 | |
| 1281 | /** @brief Sets all or some of the array elements to the specified value. |
| 1282 | |
| 1283 | This is an advanced variant of the Mat::operator=(const Scalar& s) operator. |
| 1284 | @param value Assigned scalar converted to the actual array type. |
| 1285 | @param mask Operation mask of the same size as \*this. Its non-zero elements indicate which matrix |
| 1286 | elements need to be copied. The mask has to be of type CV_8U and can have 1 or multiple channels |
| 1287 | */ |
| 1288 | Mat& setTo(InputArray value, InputArray mask=noArray()); |
| 1289 | |
| 1290 | /** @brief Changes the shape and/or the number of channels of a 2D matrix without copying the data. |
| 1291 | |
| 1292 | The method makes a new matrix header for \*this elements. The new matrix may have a different size |
| 1293 | and/or different number of channels. Any combination is possible if: |
| 1294 | - No extra elements are included into the new matrix and no elements are excluded. Consequently, |
| 1295 | the product rows\*cols\*channels() must stay the same after the transformation. |
| 1296 | - No data is copied. That is, this is an O(1) operation. Consequently, if you change the number of |
| 1297 | rows, or the operation changes the indices of elements row in some other way, the matrix must be |
| 1298 | continuous. See Mat::isContinuous . |
| 1299 | |
| 1300 | For example, if there is a set of 3D points stored as an STL vector, and you want to represent the |
| 1301 | points as a 3xN matrix, do the following: |
| 1302 | @code |
| 1303 | std::vector<Point3f> vec; |
| 1304 | ... |
| 1305 | Mat pointMat = Mat(vec). // convert vector to Mat, O(1) operation |
| 1306 | reshape(1). // make Nx3 1-channel matrix out of Nx1 3-channel. |
| 1307 | // Also, an O(1) operation |
| 1308 | t(); // finally, transpose the Nx3 matrix. |
| 1309 | // This involves copying all the elements |
| 1310 | @endcode |
| 1311 | 3-channel 2x2 matrix reshaped to 1-channel 4x3 matrix, each column has values from one of original channels: |
| 1312 | @code |
| 1313 | Mat m(Size(2, 2), CV_8UC3, Scalar(1, 2, 3)); |
| 1314 | vector<int> new_shape {4, 3}; |
| 1315 | m = m.reshape(1, new_shape); |
| 1316 | @endcode |
| 1317 | or: |
| 1318 | @code |
| 1319 | Mat m(Size(2, 2), CV_8UC3, Scalar(1, 2, 3)); |
| 1320 | const int new_shape[] = {4, 3}; |
| 1321 | m = m.reshape(1, 2, new_shape); |
| 1322 | @endcode |
| 1323 | @param cn New number of channels. If the parameter is 0, the number of channels remains the same. |
| 1324 | @param rows New number of rows. If the parameter is 0, the number of rows remains the same. |
| 1325 | */ |
| 1326 | Mat reshape(int cn, int rows=0) const; |
| 1327 | |
| 1328 | /** @overload |
| 1329 | * @param cn New number of channels. If the parameter is 0, the number of channels remains the same. |
| 1330 | * @param newndims New number of dimentions. |
| 1331 | * @param newsz Array with new matrix size by all dimentions. If some sizes are zero, |
| 1332 | * the original sizes in those dimensions are presumed. |
| 1333 | */ |
| 1334 | Mat reshape(int cn, int newndims, const int* newsz) const; |
| 1335 | |
| 1336 | /** @overload |
| 1337 | * @param cn New number of channels. If the parameter is 0, the number of channels remains the same. |
| 1338 | * @param newshape Vector with new matrix size by all dimentions. If some sizes are zero, |
| 1339 | * the original sizes in those dimensions are presumed. |
| 1340 | */ |
| 1341 | Mat reshape(int cn, const std::vector<int>& newshape) const; |
| 1342 | |
| 1343 | /** @brief Reset the type of matrix. |
| 1344 | |
| 1345 | The methods reset the data type of matrix. If the new type and the old type of the matrix |
| 1346 | have the same element size, the current buffer can be reused. The method needs to consider whether the |
| 1347 | current mat is a submatrix or has any references. |
| 1348 | @param type New data type. |
| 1349 | */ |
| 1350 | Mat reinterpret( int type ) const; |
| 1351 | |
| 1352 | /** @brief Transposes a matrix. |
| 1353 | |
| 1354 | The method performs matrix transposition by means of matrix expressions. It does not perform the |
| 1355 | actual transposition but returns a temporary matrix transposition object that can be further used as |
| 1356 | a part of more complex matrix expressions or can be assigned to a matrix: |
| 1357 | @code |
| 1358 | Mat A1 = A + Mat::eye(A.size(), A.type())*lambda; |
| 1359 | Mat C = A1.t()*A1; // compute (A + lambda*I)^t * (A + lamda*I) |
| 1360 | @endcode |
| 1361 | */ |
| 1362 | MatExpr t() const; |
| 1363 | |
| 1364 | /** @brief Inverses a matrix. |
| 1365 | |
| 1366 | The method performs a matrix inversion by means of matrix expressions. This means that a temporary |
| 1367 | matrix inversion object is returned by the method and can be used further as a part of more complex |
| 1368 | matrix expressions or can be assigned to a matrix. |
| 1369 | @param method Matrix inversion method. One of cv::DecompTypes |
| 1370 | */ |
| 1371 | MatExpr inv(int method=DECOMP_LU) const; |
| 1372 | |
| 1373 | /** @brief Performs an element-wise multiplication or division of the two matrices. |
| 1374 | |
| 1375 | The method returns a temporary object encoding per-element array multiplication, with optional |
| 1376 | scale. Note that this is not a matrix multiplication that corresponds to a simpler "\*" operator. |
| 1377 | |
| 1378 | Example: |
| 1379 | @code |
| 1380 | Mat C = A.mul(5/B); // equivalent to divide(A, B, C, 5) |
| 1381 | @endcode |
| 1382 | @param m Another array of the same type and the same size as \*this, or a matrix expression. |
| 1383 | @param scale Optional scale factor. |
| 1384 | */ |
| 1385 | MatExpr mul(InputArray m, double scale=1) const; |
| 1386 | |
| 1387 | /** @brief Computes a cross-product of two 3-element vectors. |
| 1388 | |
| 1389 | The method computes a cross-product of two 3-element vectors. The vectors must be 3-element |
| 1390 | floating-point vectors of the same shape and size. The result is another 3-element vector of the |
| 1391 | same shape and type as operands. |
| 1392 | @param m Another cross-product operand. |
| 1393 | */ |
| 1394 | Mat cross(InputArray m) const; |
| 1395 | |
| 1396 | /** @brief Computes a dot-product of two vectors. |
| 1397 | |
| 1398 | The method computes a dot-product of two matrices. If the matrices are not single-column or |
| 1399 | single-row vectors, the top-to-bottom left-to-right scan ordering is used to treat them as 1D |
| 1400 | vectors. The vectors must have the same size and type. If the matrices have more than one channel, |
| 1401 | the dot products from all the channels are summed together. |
| 1402 | @param m another dot-product operand. |
| 1403 | */ |
| 1404 | double dot(InputArray m) const; |
| 1405 | |
| 1406 | /** @brief Returns a zero array of the specified size and type. |
| 1407 | |
| 1408 | The method returns a Matlab-style zero array initializer. It can be used to quickly form a constant |
| 1409 | array as a function parameter, part of a matrix expression, or as a matrix initializer: |
| 1410 | @code |
| 1411 | Mat A; |
| 1412 | A = Mat::zeros(3, 3, CV_32F); |
| 1413 | @endcode |
| 1414 | In the example above, a new matrix is allocated only if A is not a 3x3 floating-point matrix. |
| 1415 | Otherwise, the existing matrix A is filled with zeros. |
| 1416 | @param rows Number of rows. |
| 1417 | @param cols Number of columns. |
| 1418 | @param type Created matrix type. |
| 1419 | */ |
| 1420 | CV_NODISCARD_STD static MatExpr zeros(int rows, int cols, int type); |
| 1421 | |
| 1422 | /** @overload |
| 1423 | @param size Alternative to the matrix size specification Size(cols, rows) . |
| 1424 | @param type Created matrix type. |
| 1425 | */ |
| 1426 | CV_NODISCARD_STD static MatExpr zeros(Size size, int type); |
| 1427 | |
| 1428 | /** @overload |
| 1429 | @param ndims Array dimensionality. |
| 1430 | @param sz Array of integers specifying the array shape. |
| 1431 | @param type Created matrix type. |
| 1432 | */ |
| 1433 | CV_NODISCARD_STD static MatExpr zeros(int ndims, const int* sz, int type); |
| 1434 | |
| 1435 | /** @brief Returns an array of all 1's of the specified size and type. |
| 1436 | |
| 1437 | The method returns a Matlab-style 1's array initializer, similarly to Mat::zeros. Note that using |
| 1438 | this method you can initialize an array with an arbitrary value, using the following Matlab idiom: |
| 1439 | @code |
| 1440 | Mat A = Mat::ones(100, 100, CV_8U)*3; // make 100x100 matrix filled with 3. |
| 1441 | @endcode |
| 1442 | The above operation does not form a 100x100 matrix of 1's and then multiply it by 3. Instead, it |
| 1443 | just remembers the scale factor (3 in this case) and use it when actually invoking the matrix |
| 1444 | initializer. |
| 1445 | @note In case of multi-channels type, only the first channel will be initialized with 1's, the |
| 1446 | others will be set to 0's. |
| 1447 | @param rows Number of rows. |
| 1448 | @param cols Number of columns. |
| 1449 | @param type Created matrix type. |
| 1450 | */ |
| 1451 | CV_NODISCARD_STD static MatExpr ones(int rows, int cols, int type); |
| 1452 | |
| 1453 | /** @overload |
| 1454 | @param size Alternative to the matrix size specification Size(cols, rows) . |
| 1455 | @param type Created matrix type. |
| 1456 | */ |
| 1457 | CV_NODISCARD_STD static MatExpr ones(Size size, int type); |
| 1458 | |
| 1459 | /** @overload |
| 1460 | @param ndims Array dimensionality. |
| 1461 | @param sz Array of integers specifying the array shape. |
| 1462 | @param type Created matrix type. |
| 1463 | */ |
| 1464 | CV_NODISCARD_STD static MatExpr ones(int ndims, const int* sz, int type); |
| 1465 | |
| 1466 | /** @brief Returns an identity matrix of the specified size and type. |
| 1467 | |
| 1468 | The method returns a Matlab-style identity matrix initializer, similarly to Mat::zeros. Similarly to |
| 1469 | Mat::ones, you can use a scale operation to create a scaled identity matrix efficiently: |
| 1470 | @code |
| 1471 | // make a 4x4 diagonal matrix with 0.1's on the diagonal. |
| 1472 | Mat A = Mat::eye(4, 4, CV_32F)*0.1; |
| 1473 | @endcode |
| 1474 | @note In case of multi-channels type, identity matrix will be initialized only for the first channel, |
| 1475 | the others will be set to 0's |
| 1476 | @param rows Number of rows. |
| 1477 | @param cols Number of columns. |
| 1478 | @param type Created matrix type. |
| 1479 | */ |
| 1480 | CV_NODISCARD_STD static MatExpr eye(int rows, int cols, int type); |
| 1481 | |
| 1482 | /** @overload |
| 1483 | @param size Alternative matrix size specification as Size(cols, rows) . |
| 1484 | @param type Created matrix type. |
| 1485 | */ |
| 1486 | CV_NODISCARD_STD static MatExpr eye(Size size, int type); |
| 1487 | |
| 1488 | /** @brief Allocates new array data if needed. |
| 1489 | |
| 1490 | This is one of the key Mat methods. Most new-style OpenCV functions and methods that produce arrays |
| 1491 | call this method for each output array. The method uses the following algorithm: |
| 1492 | |
| 1493 | -# If the current array shape and the type match the new ones, return immediately. Otherwise, |
| 1494 | de-reference the previous data by calling Mat::release. |
| 1495 | -# Initialize the new header. |
| 1496 | -# Allocate the new data of total()\*elemSize() bytes. |
| 1497 | -# Allocate the new, associated with the data, reference counter and set it to 1. |
| 1498 | |
| 1499 | Such a scheme makes the memory management robust and efficient at the same time and helps avoid |
| 1500 | extra typing for you. This means that usually there is no need to explicitly allocate output arrays. |
| 1501 | That is, instead of writing: |
| 1502 | @code |
| 1503 | Mat color; |
| 1504 | ... |
| 1505 | Mat gray(color.rows, color.cols, color.depth()); |
| 1506 | cvtColor(color, gray, COLOR_BGR2GRAY); |
| 1507 | @endcode |
| 1508 | you can simply write: |
| 1509 | @code |
| 1510 | Mat color; |
| 1511 | ... |
| 1512 | Mat gray; |
| 1513 | cvtColor(color, gray, COLOR_BGR2GRAY); |
| 1514 | @endcode |
| 1515 | because cvtColor, as well as the most of OpenCV functions, calls Mat::create() for the output array |
| 1516 | internally. |
| 1517 | @param rows New number of rows. |
| 1518 | @param cols New number of columns. |
| 1519 | @param type New matrix type. |
| 1520 | */ |
| 1521 | void create(int rows, int cols, int type); |
| 1522 | |
| 1523 | /** @overload |
| 1524 | @param size Alternative new matrix size specification: Size(cols, rows) |
| 1525 | @param type New matrix type. |
| 1526 | */ |
| 1527 | void create(Size size, int type); |
| 1528 | |
| 1529 | /** @overload |
| 1530 | @param ndims New array dimensionality. |
| 1531 | @param sizes Array of integers specifying a new array shape. |
| 1532 | @param type New matrix type. |
| 1533 | */ |
| 1534 | void create(int ndims, const int* sizes, int type); |
| 1535 | |
| 1536 | /** @overload |
| 1537 | @param sizes Array of integers specifying a new array shape. |
| 1538 | @param type New matrix type. |
| 1539 | */ |
| 1540 | void create(const std::vector<int>& sizes, int type); |
| 1541 | |
| 1542 | /** @brief Increments the reference counter. |
| 1543 | |
| 1544 | The method increments the reference counter associated with the matrix data. If the matrix header |
| 1545 | points to an external data set (see Mat::Mat ), the reference counter is NULL, and the method has no |
| 1546 | effect in this case. Normally, to avoid memory leaks, the method should not be called explicitly. It |
| 1547 | is called implicitly by the matrix assignment operator. The reference counter increment is an atomic |
| 1548 | operation on the platforms that support it. Thus, it is safe to operate on the same matrices |
| 1549 | asynchronously in different threads. |
| 1550 | */ |
| 1551 | void addref(); |
| 1552 | |
| 1553 | /** @brief Decrements the reference counter and deallocates the matrix if needed. |
| 1554 | |
| 1555 | The method decrements the reference counter associated with the matrix data. When the reference |
| 1556 | counter reaches 0, the matrix data is deallocated and the data and the reference counter pointers |
| 1557 | are set to NULL's. If the matrix header points to an external data set (see Mat::Mat ), the |
| 1558 | reference counter is NULL, and the method has no effect in this case. |
| 1559 | |
| 1560 | This method can be called manually to force the matrix data deallocation. But since this method is |
| 1561 | automatically called in the destructor, or by any other method that changes the data pointer, it is |
| 1562 | usually not needed. The reference counter decrement and check for 0 is an atomic operation on the |
| 1563 | platforms that support it. Thus, it is safe to operate on the same matrices asynchronously in |
| 1564 | different threads. |
| 1565 | */ |
| 1566 | void release(); |
| 1567 | |
| 1568 | //! internal use function, consider to use 'release' method instead; deallocates the matrix data |
| 1569 | void deallocate(); |
| 1570 | //! internal use function; properly re-allocates _size, _step arrays |
| 1571 | void copySize(const Mat& m); |
| 1572 | |
| 1573 | /** @brief Reserves space for the certain number of rows. |
| 1574 | |
| 1575 | The method reserves space for sz rows. If the matrix already has enough space to store sz rows, |
| 1576 | nothing happens. If the matrix is reallocated, the first Mat::rows rows are preserved. The method |
| 1577 | emulates the corresponding method of the STL vector class. |
| 1578 | @param sz Number of rows. |
| 1579 | */ |
| 1580 | void reserve(size_t sz); |
| 1581 | |
| 1582 | /** @brief Reserves space for the certain number of bytes. |
| 1583 | |
| 1584 | The method reserves space for sz bytes. If the matrix already has enough space to store sz bytes, |
| 1585 | nothing happens. If matrix has to be reallocated its previous content could be lost. |
| 1586 | @param sz Number of bytes. |
| 1587 | */ |
| 1588 | void reserveBuffer(size_t sz); |
| 1589 | |
| 1590 | /** @brief Changes the number of matrix rows. |
| 1591 | |
| 1592 | The methods change the number of matrix rows. If the matrix is reallocated, the first |
| 1593 | min(Mat::rows, sz) rows are preserved. The methods emulate the corresponding methods of the STL |
| 1594 | vector class. |
| 1595 | @param sz New number of rows. |
| 1596 | */ |
| 1597 | void resize(size_t sz); |
| 1598 | |
| 1599 | /** @overload |
| 1600 | @param sz New number of rows. |
| 1601 | @param s Value assigned to the newly added elements. |
| 1602 | */ |
| 1603 | void resize(size_t sz, const Scalar& s); |
| 1604 | |
| 1605 | //! internal function |
| 1606 | void push_back_(const void* elem); |
| 1607 | |
| 1608 | /** @brief Adds elements to the bottom of the matrix. |
| 1609 | |
| 1610 | The methods add one or more elements to the bottom of the matrix. They emulate the corresponding |
| 1611 | method of the STL vector class. When elem is Mat , its type and the number of columns must be the |
| 1612 | same as in the container matrix. |
| 1613 | @param elem Added element(s). |
| 1614 | */ |
| 1615 | template<typename _Tp> void push_back(const _Tp& elem); |
| 1616 | |
| 1617 | /** @overload |
| 1618 | @param elem Added element(s). |
| 1619 | */ |
| 1620 | template<typename _Tp> void push_back(const Mat_<_Tp>& elem); |
| 1621 | |
| 1622 | /** @overload |
| 1623 | @param elem Added element(s). |
| 1624 | */ |
| 1625 | template<typename _Tp> void push_back(const std::vector<_Tp>& elem); |
| 1626 | |
| 1627 | /** @overload |
| 1628 | @param m Added line(s). |
| 1629 | */ |
| 1630 | void push_back(const Mat& m); |
| 1631 | |
| 1632 | /** @brief Removes elements from the bottom of the matrix. |
| 1633 | |
| 1634 | The method removes one or more rows from the bottom of the matrix. |
| 1635 | @param nelems Number of removed rows. If it is greater than the total number of rows, an exception |
| 1636 | is thrown. |
| 1637 | */ |
| 1638 | void pop_back(size_t nelems=1); |
| 1639 | |
| 1640 | /** @brief Locates the matrix header within a parent matrix. |
| 1641 | |
| 1642 | After you extracted a submatrix from a matrix using Mat::row, Mat::col, Mat::rowRange, |
| 1643 | Mat::colRange, and others, the resultant submatrix points just to the part of the original big |
| 1644 | matrix. However, each submatrix contains information (represented by datastart and dataend |
| 1645 | fields) that helps reconstruct the original matrix size and the position of the extracted |
| 1646 | submatrix within the original matrix. The method locateROI does exactly that. |
| 1647 | @param wholeSize Output parameter that contains the size of the whole matrix containing *this* |
| 1648 | as a part. |
| 1649 | @param ofs Output parameter that contains an offset of *this* inside the whole matrix. |
| 1650 | */ |
| 1651 | void locateROI( Size& wholeSize, Point& ofs ) const; |
| 1652 | |
| 1653 | /** @brief Adjusts a submatrix size and position within the parent matrix. |
| 1654 | |
| 1655 | The method is complimentary to Mat::locateROI . The typical use of these functions is to determine |
| 1656 | the submatrix position within the parent matrix and then shift the position somehow. Typically, it |
| 1657 | can be required for filtering operations when pixels outside of the ROI should be taken into |
| 1658 | account. When all the method parameters are positive, the ROI needs to grow in all directions by the |
| 1659 | specified amount, for example: |
| 1660 | @code |
| 1661 | A.adjustROI(2, 2, 2, 2); |
| 1662 | @endcode |
| 1663 | In this example, the matrix size is increased by 4 elements in each direction. The matrix is shifted |
| 1664 | by 2 elements to the left and 2 elements up, which brings in all the necessary pixels for the |
| 1665 | filtering with the 5x5 kernel. |
| 1666 | |
| 1667 | adjustROI forces the adjusted ROI to be inside of the parent matrix that is boundaries of the |
| 1668 | adjusted ROI are constrained by boundaries of the parent matrix. For example, if the submatrix A is |
| 1669 | located in the first row of a parent matrix and you called A.adjustROI(2, 2, 2, 2) then A will not |
| 1670 | be increased in the upward direction. |
| 1671 | |
| 1672 | The function is used internally by the OpenCV filtering functions, like filter2D , morphological |
| 1673 | operations, and so on. |
| 1674 | @param dtop Shift of the top submatrix boundary upwards. |
| 1675 | @param dbottom Shift of the bottom submatrix boundary downwards. |
| 1676 | @param dleft Shift of the left submatrix boundary to the left. |
| 1677 | @param dright Shift of the right submatrix boundary to the right. |
| 1678 | @sa copyMakeBorder |
| 1679 | */ |
| 1680 | Mat& adjustROI( int dtop, int dbottom, int dleft, int dright ); |
| 1681 | |
| 1682 | /** @brief Extracts a rectangular submatrix. |
| 1683 | |
| 1684 | The operators make a new header for the specified sub-array of \*this . They are the most |
| 1685 | generalized forms of Mat::row, Mat::col, Mat::rowRange, and Mat::colRange . For example, |
| 1686 | `A(Range(0, 10), Range::all())` is equivalent to `A.rowRange(0, 10)`. Similarly to all of the above, |
| 1687 | the operators are O(1) operations, that is, no matrix data is copied. |
| 1688 | @param rowRange Start and end row of the extracted submatrix. The upper boundary is not included. To |
| 1689 | select all the rows, use Range::all(). |
| 1690 | @param colRange Start and end column of the extracted submatrix. The upper boundary is not included. |
| 1691 | To select all the columns, use Range::all(). |
| 1692 | */ |
| 1693 | Mat operator()( Range rowRange, Range colRange ) const; |
| 1694 | |
| 1695 | /** @overload |
| 1696 | @param roi Extracted submatrix specified as a rectangle. |
| 1697 | */ |
| 1698 | Mat operator()( const Rect& roi ) const; |
| 1699 | |
| 1700 | /** @overload |
| 1701 | @param ranges Array of selected ranges along each array dimension. |
| 1702 | */ |
| 1703 | Mat operator()( const Range* ranges ) const; |
| 1704 | |
| 1705 | /** @overload |
| 1706 | @param ranges Array of selected ranges along each array dimension. |
| 1707 | */ |
| 1708 | Mat operator()(const std::vector<Range>& ranges) const; |
| 1709 | |
| 1710 | template<typename _Tp> operator std::vector<_Tp>() const; |
| 1711 | template<typename _Tp, int n> operator Vec<_Tp, n>() const; |
| 1712 | template<typename _Tp, int m, int n> operator Matx<_Tp, m, n>() const; |
| 1713 | |
| 1714 | template<typename _Tp, std::size_t _Nm> operator std::array<_Tp, _Nm>() const; |
| 1715 | |
| 1716 | /** @brief Reports whether the matrix is continuous or not. |
| 1717 | |
| 1718 | The method returns true if the matrix elements are stored continuously without gaps at the end of |
| 1719 | each row. Otherwise, it returns false. Obviously, 1x1 or 1xN matrices are always continuous. |
| 1720 | Matrices created with Mat::create are always continuous. But if you extract a part of the matrix |
| 1721 | using Mat::col, Mat::diag, and so on, or constructed a matrix header for externally allocated data, |
| 1722 | such matrices may no longer have this property. |
| 1723 | |
| 1724 | The continuity flag is stored as a bit in the Mat::flags field and is computed automatically when |
| 1725 | you construct a matrix header. Thus, the continuity check is a very fast operation, though |
| 1726 | theoretically it could be done as follows: |
| 1727 | @code |
| 1728 | // alternative implementation of Mat::isContinuous() |
| 1729 | bool myCheckMatContinuity(const Mat& m) |
| 1730 | { |
| 1731 | //return (m.flags & Mat::CONTINUOUS_FLAG) != 0; |
| 1732 | return m.rows == 1 || m.step == m.cols*m.elemSize(); |
| 1733 | } |
| 1734 | @endcode |
| 1735 | The method is used in quite a few of OpenCV functions. The point is that element-wise operations |
| 1736 | (such as arithmetic and logical operations, math functions, alpha blending, color space |
| 1737 | transformations, and others) do not depend on the image geometry. Thus, if all the input and output |
| 1738 | arrays are continuous, the functions can process them as very long single-row vectors. The example |
| 1739 | below illustrates how an alpha-blending function can be implemented: |
| 1740 | @code |
| 1741 | template<typename T> |
| 1742 | void alphaBlendRGBA(const Mat& src1, const Mat& src2, Mat& dst) |
| 1743 | { |
| 1744 | const float alpha_scale = (float)std::numeric_limits<T>::max(), |
| 1745 | inv_scale = 1.f/alpha_scale; |
| 1746 | |
| 1747 | CV_Assert( src1.type() == src2.type() && |
| 1748 | src1.type() == CV_MAKETYPE(traits::Depth<T>::value, 4) && |
| 1749 | src1.size() == src2.size()); |
| 1750 | Size size = src1.size(); |
| 1751 | dst.create(size, src1.type()); |
| 1752 | |
| 1753 | // here is the idiom: check the arrays for continuity and, |
| 1754 | // if this is the case, |
| 1755 | // treat the arrays as 1D vectors |
| 1756 | if( src1.isContinuous() && src2.isContinuous() && dst.isContinuous() ) |
| 1757 | { |
| 1758 | size.width *= size.height; |
| 1759 | size.height = 1; |
| 1760 | } |
| 1761 | size.width *= 4; |
| 1762 | |
| 1763 | for( int i = 0; i < size.height; i++ ) |
| 1764 | { |
| 1765 | // when the arrays are continuous, |
| 1766 | // the outer loop is executed only once |
| 1767 | const T* ptr1 = src1.ptr<T>(i); |
| 1768 | const T* ptr2 = src2.ptr<T>(i); |
| 1769 | T* dptr = dst.ptr<T>(i); |
| 1770 | |
| 1771 | for( int j = 0; j < size.width; j += 4 ) |
| 1772 | { |
| 1773 | float alpha = ptr1[j+3]*inv_scale, beta = ptr2[j+3]*inv_scale; |
| 1774 | dptr[j] = saturate_cast<T>(ptr1[j]*alpha + ptr2[j]*beta); |
| 1775 | dptr[j+1] = saturate_cast<T>(ptr1[j+1]*alpha + ptr2[j+1]*beta); |
| 1776 | dptr[j+2] = saturate_cast<T>(ptr1[j+2]*alpha + ptr2[j+2]*beta); |
| 1777 | dptr[j+3] = saturate_cast<T>((1 - (1-alpha)*(1-beta))*alpha_scale); |
| 1778 | } |
| 1779 | } |
| 1780 | } |
| 1781 | @endcode |
| 1782 | This approach, while being very simple, can boost the performance of a simple element-operation by |
| 1783 | 10-20 percents, especially if the image is rather small and the operation is quite simple. |
| 1784 | |
| 1785 | Another OpenCV idiom in this function, a call of Mat::create for the destination array, that |
| 1786 | allocates the destination array unless it already has the proper size and type. And while the newly |
| 1787 | allocated arrays are always continuous, you still need to check the destination array because |
| 1788 | Mat::create does not always allocate a new matrix. |
| 1789 | */ |
| 1790 | bool isContinuous() const; |
| 1791 | |
| 1792 | //! returns true if the matrix is a submatrix of another matrix |
| 1793 | bool isSubmatrix() const; |
| 1794 | |
| 1795 | /** @brief Returns the matrix element size in bytes. |
| 1796 | |
| 1797 | The method returns the matrix element size in bytes. For example, if the matrix type is CV_16SC3 , |
| 1798 | the method returns 3\*sizeof(short) or 6. |
| 1799 | */ |
| 1800 | size_t elemSize() const; |
| 1801 | |
| 1802 | /** @brief Returns the size of each matrix element channel in bytes. |
| 1803 | |
| 1804 | The method returns the matrix element channel size in bytes, that is, it ignores the number of |
| 1805 | channels. For example, if the matrix type is CV_16SC3 , the method returns sizeof(short) or 2. |
| 1806 | */ |
| 1807 | size_t elemSize1() const; |
| 1808 | |
| 1809 | /** @brief Returns the type of a matrix element. |
| 1810 | |
| 1811 | The method returns a matrix element type. This is an identifier compatible with the CvMat type |
| 1812 | system, like CV_16SC3 or 16-bit signed 3-channel array, and so on. |
| 1813 | */ |
| 1814 | int type() const; |
| 1815 | |
| 1816 | /** @brief Returns the depth of a matrix element. |
| 1817 | |
| 1818 | The method returns the identifier of the matrix element depth (the type of each individual channel). |
| 1819 | For example, for a 16-bit signed element array, the method returns CV_16S . A complete list of |
| 1820 | matrix types contains the following values: |
| 1821 | - CV_8U - 8-bit unsigned integers ( 0..255 ) |
| 1822 | - CV_8S - 8-bit signed integers ( -128..127 ) |
| 1823 | - CV_16U - 16-bit unsigned integers ( 0..65535 ) |
| 1824 | - CV_16S - 16-bit signed integers ( -32768..32767 ) |
| 1825 | - CV_32S - 32-bit signed integers ( -2147483648..2147483647 ) |
| 1826 | - CV_32F - 32-bit floating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN ) |
| 1827 | - CV_64F - 64-bit floating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN ) |
| 1828 | */ |
| 1829 | int depth() const; |
| 1830 | |
| 1831 | /** @brief Returns the number of matrix channels. |
| 1832 | |
| 1833 | The method returns the number of matrix channels. |
| 1834 | */ |
| 1835 | int channels() const; |
| 1836 | |
| 1837 | /** @brief Returns a normalized step. |
| 1838 | |
| 1839 | The method returns a matrix step divided by Mat::elemSize1() . It can be useful to quickly access an |
| 1840 | arbitrary matrix element. |
| 1841 | */ |
| 1842 | size_t step1(int i=0) const; |
| 1843 | |
| 1844 | /** @brief Returns true if the array has no elements. |
| 1845 | |
| 1846 | The method returns true if Mat::total() is 0 or if Mat::data is NULL. Because of pop_back() and |
| 1847 | resize() methods `M.total() == 0` does not imply that `M.data == NULL`. |
| 1848 | */ |
| 1849 | bool empty() const; |
| 1850 | |
| 1851 | /** @brief Returns the total number of array elements. |
| 1852 | |
| 1853 | The method returns the number of array elements (a number of pixels if the array represents an |
| 1854 | image). |
| 1855 | */ |
| 1856 | size_t total() const; |
| 1857 | |
| 1858 | /** @brief Returns the total number of array elements. |
| 1859 | |
| 1860 | The method returns the number of elements within a certain sub-array slice with startDim <= dim < endDim |
| 1861 | */ |
| 1862 | size_t total(int startDim, int endDim=INT_MAX) const; |
| 1863 | |
| 1864 | /** |
| 1865 | * @param elemChannels Number of channels or number of columns the matrix should have. |
| 1866 | * For a 2-D matrix, when the matrix has only 1 column, then it should have |
| 1867 | * elemChannels channels; When the matrix has only 1 channel, |
| 1868 | * then it should have elemChannels columns. |
| 1869 | * For a 3-D matrix, it should have only one channel. Furthermore, |
| 1870 | * if the number of planes is not one, then the number of rows |
| 1871 | * within every plane has to be 1; if the number of rows within |
| 1872 | * every plane is not 1, then the number of planes has to be 1. |
| 1873 | * @param depth The depth the matrix should have. Set it to -1 when any depth is fine. |
| 1874 | * @param requireContinuous Set it to true to require the matrix to be continuous |
| 1875 | * @return -1 if the requirement is not satisfied. |
| 1876 | * Otherwise, it returns the number of elements in the matrix. Note |
| 1877 | * that an element may have multiple channels. |
| 1878 | * |
| 1879 | * The following code demonstrates its usage for a 2-d matrix: |
| 1880 | * @snippet snippets/core_mat_checkVector.cpp example-2d |
| 1881 | * |
| 1882 | * The following code demonstrates its usage for a 3-d matrix: |
| 1883 | * @snippet snippets/core_mat_checkVector.cpp example-3d |
| 1884 | */ |
| 1885 | int checkVector(int elemChannels, int depth=-1, bool requireContinuous=true) const; |
| 1886 | |
| 1887 | /** @brief Returns a pointer to the specified matrix row. |
| 1888 | |
| 1889 | The methods return `uchar*` or typed pointer to the specified matrix row. See the sample in |
| 1890 | Mat::isContinuous to know how to use these methods. |
| 1891 | @param i0 A 0-based row index. |
| 1892 | */ |
| 1893 | uchar* ptr(int i0=0); |
| 1894 | /** @overload */ |
| 1895 | const uchar* ptr(int i0=0) const; |
| 1896 | |
| 1897 | /** @overload |
| 1898 | @param row Index along the dimension 0 |
| 1899 | @param col Index along the dimension 1 |
| 1900 | */ |
| 1901 | uchar* ptr(int row, int col); |
| 1902 | /** @overload |
| 1903 | @param row Index along the dimension 0 |
| 1904 | @param col Index along the dimension 1 |
| 1905 | */ |
| 1906 | const uchar* ptr(int row, int col) const; |
| 1907 | |
| 1908 | /** @overload */ |
| 1909 | uchar* ptr(int i0, int i1, int i2); |
| 1910 | /** @overload */ |
| 1911 | const uchar* ptr(int i0, int i1, int i2) const; |
| 1912 | |
| 1913 | /** @overload */ |
| 1914 | uchar* ptr(const int* idx); |
| 1915 | /** @overload */ |
| 1916 | const uchar* ptr(const int* idx) const; |
| 1917 | /** @overload */ |
| 1918 | template<int n> uchar* ptr(const Vec<int, n>& idx); |
| 1919 | /** @overload */ |
| 1920 | template<int n> const uchar* ptr(const Vec<int, n>& idx) const; |
| 1921 | |
| 1922 | /** @overload */ |
| 1923 | template<typename _Tp> _Tp* ptr(int i0=0); |
| 1924 | /** @overload */ |
| 1925 | template<typename _Tp> const _Tp* ptr(int i0=0) const; |
| 1926 | /** @overload |
| 1927 | @param row Index along the dimension 0 |
| 1928 | @param col Index along the dimension 1 |
| 1929 | */ |
| 1930 | template<typename _Tp> _Tp* ptr(int row, int col); |
| 1931 | /** @overload |
| 1932 | @param row Index along the dimension 0 |
| 1933 | @param col Index along the dimension 1 |
| 1934 | */ |
| 1935 | template<typename _Tp> const _Tp* ptr(int row, int col) const; |
| 1936 | /** @overload */ |
| 1937 | template<typename _Tp> _Tp* ptr(int i0, int i1, int i2); |
| 1938 | /** @overload */ |
| 1939 | template<typename _Tp> const _Tp* ptr(int i0, int i1, int i2) const; |
| 1940 | /** @overload */ |
| 1941 | template<typename _Tp> _Tp* ptr(const int* idx); |
| 1942 | /** @overload */ |
| 1943 | template<typename _Tp> const _Tp* ptr(const int* idx) const; |
| 1944 | /** @overload */ |
| 1945 | template<typename _Tp, int n> _Tp* ptr(const Vec<int, n>& idx); |
| 1946 | /** @overload */ |
| 1947 | template<typename _Tp, int n> const _Tp* ptr(const Vec<int, n>& idx) const; |
| 1948 | |
| 1949 | /** @brief Returns a reference to the specified array element. |
| 1950 | |
| 1951 | The template methods return a reference to the specified array element. For the sake of higher |
| 1952 | performance, the index range checks are only performed in the Debug configuration. |
| 1953 | |
| 1954 | Note that the variants with a single index (i) can be used to access elements of single-row or |
| 1955 | single-column 2-dimensional arrays. That is, if, for example, A is a 1 x N floating-point matrix and |
| 1956 | B is an M x 1 integer matrix, you can simply write `A.at<float>(k+4)` and `B.at<int>(2*i+1)` |
| 1957 | instead of `A.at<float>(0,k+4)` and `B.at<int>(2*i+1,0)`, respectively. |
| 1958 | |
| 1959 | The example below initializes a Hilbert matrix: |
| 1960 | @code |
| 1961 | Mat H(100, 100, CV_64F); |
| 1962 | for(int i = 0; i < H.rows; i++) |
| 1963 | for(int j = 0; j < H.cols; j++) |
| 1964 | H.at<double>(i,j)=1./(i+j+1); |
| 1965 | @endcode |
| 1966 | |
| 1967 | Keep in mind that the size identifier used in the at operator cannot be chosen at random. It depends |
| 1968 | on the image from which you are trying to retrieve the data. The table below gives a better insight in this: |
| 1969 | - If matrix is of type `CV_8U` then use `Mat.at<uchar>(y,x)`. |
| 1970 | - If matrix is of type `CV_8S` then use `Mat.at<schar>(y,x)`. |
| 1971 | - If matrix is of type `CV_16U` then use `Mat.at<ushort>(y,x)`. |
| 1972 | - If matrix is of type `CV_16S` then use `Mat.at<short>(y,x)`. |
| 1973 | - If matrix is of type `CV_32S` then use `Mat.at<int>(y,x)`. |
| 1974 | - If matrix is of type `CV_32F` then use `Mat.at<float>(y,x)`. |
| 1975 | - If matrix is of type `CV_64F` then use `Mat.at<double>(y,x)`. |
| 1976 | |
| 1977 | @param i0 Index along the dimension 0 |
| 1978 | */ |
| 1979 | template<typename _Tp> _Tp& at(int i0=0); |
| 1980 | /** @overload |
| 1981 | @param i0 Index along the dimension 0 |
| 1982 | */ |
| 1983 | template<typename _Tp> const _Tp& at(int i0=0) const; |
| 1984 | /** @overload |
| 1985 | @param row Index along the dimension 0 |
| 1986 | @param col Index along the dimension 1 |
| 1987 | */ |
| 1988 | template<typename _Tp> _Tp& at(int row, int col); |
| 1989 | /** @overload |
| 1990 | @param row Index along the dimension 0 |
| 1991 | @param col Index along the dimension 1 |
| 1992 | */ |
| 1993 | template<typename _Tp> const _Tp& at(int row, int col) const; |
| 1994 | |
| 1995 | /** @overload |
| 1996 | @param i0 Index along the dimension 0 |
| 1997 | @param i1 Index along the dimension 1 |
| 1998 | @param i2 Index along the dimension 2 |
| 1999 | */ |
| 2000 | template<typename _Tp> _Tp& at(int i0, int i1, int i2); |
| 2001 | /** @overload |
| 2002 | @param i0 Index along the dimension 0 |
| 2003 | @param i1 Index along the dimension 1 |
| 2004 | @param i2 Index along the dimension 2 |
| 2005 | */ |
| 2006 | template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const; |
| 2007 | |
| 2008 | /** @overload |
| 2009 | @param idx Array of Mat::dims indices. |
| 2010 | */ |
| 2011 | template<typename _Tp> _Tp& at(const int* idx); |
| 2012 | /** @overload |
| 2013 | @param idx Array of Mat::dims indices. |
| 2014 | */ |
| 2015 | template<typename _Tp> const _Tp& at(const int* idx) const; |
| 2016 | |
| 2017 | /** @overload */ |
| 2018 | template<typename _Tp, int n> _Tp& at(const Vec<int, n>& idx); |
| 2019 | /** @overload */ |
| 2020 | template<typename _Tp, int n> const _Tp& at(const Vec<int, n>& idx) const; |
| 2021 | |
| 2022 | /** @overload |
| 2023 | special versions for 2D arrays (especially convenient for referencing image pixels) |
| 2024 | @param pt Element position specified as Point(j,i) . |
| 2025 | */ |
| 2026 | template<typename _Tp> _Tp& at(Point pt); |
| 2027 | /** @overload |
| 2028 | special versions for 2D arrays (especially convenient for referencing image pixels) |
| 2029 | @param pt Element position specified as Point(j,i) . |
| 2030 | */ |
| 2031 | template<typename _Tp> const _Tp& at(Point pt) const; |
| 2032 | |
| 2033 | /** @brief Returns the matrix iterator and sets it to the first matrix element. |
| 2034 | |
| 2035 | The methods return the matrix read-only or read-write iterators. The use of matrix iterators is very |
| 2036 | similar to the use of bi-directional STL iterators. In the example below, the alpha blending |
| 2037 | function is rewritten using the matrix iterators: |
| 2038 | @code |
| 2039 | template<typename T> |
| 2040 | void alphaBlendRGBA(const Mat& src1, const Mat& src2, Mat& dst) |
| 2041 | { |
| 2042 | typedef Vec<T, 4> VT; |
| 2043 | |
| 2044 | const float alpha_scale = (float)std::numeric_limits<T>::max(), |
| 2045 | inv_scale = 1.f/alpha_scale; |
| 2046 | |
| 2047 | CV_Assert( src1.type() == src2.type() && |
| 2048 | src1.type() == traits::Type<VT>::value && |
| 2049 | src1.size() == src2.size()); |
| 2050 | Size size = src1.size(); |
| 2051 | dst.create(size, src1.type()); |
| 2052 | |
| 2053 | MatConstIterator_<VT> it1 = src1.begin<VT>(), it1_end = src1.end<VT>(); |
| 2054 | MatConstIterator_<VT> it2 = src2.begin<VT>(); |
| 2055 | MatIterator_<VT> dst_it = dst.begin<VT>(); |
| 2056 | |
| 2057 | for( ; it1 != it1_end; ++it1, ++it2, ++dst_it ) |
| 2058 | { |
| 2059 | VT pix1 = *it1, pix2 = *it2; |
| 2060 | float alpha = pix1[3]*inv_scale, beta = pix2[3]*inv_scale; |
| 2061 | *dst_it = VT(saturate_cast<T>(pix1[0]*alpha + pix2[0]*beta), |
| 2062 | saturate_cast<T>(pix1[1]*alpha + pix2[1]*beta), |
| 2063 | saturate_cast<T>(pix1[2]*alpha + pix2[2]*beta), |
| 2064 | saturate_cast<T>((1 - (1-alpha)*(1-beta))*alpha_scale)); |
| 2065 | } |
| 2066 | } |
| 2067 | @endcode |
| 2068 | */ |
| 2069 | template<typename _Tp> MatIterator_<_Tp> begin(); |
| 2070 | template<typename _Tp> MatConstIterator_<_Tp> begin() const; |
| 2071 | |
| 2072 | /** @brief Same as begin() but for inverse traversal |
| 2073 | */ |
| 2074 | template<typename _Tp> std::reverse_iterator<MatIterator_<_Tp>> rbegin(); |
| 2075 | template<typename _Tp> std::reverse_iterator<MatConstIterator_<_Tp>> rbegin() const; |
| 2076 | |
| 2077 | /** @brief Returns the matrix iterator and sets it to the after-last matrix element. |
| 2078 | |
| 2079 | The methods return the matrix read-only or read-write iterators, set to the point following the last |
| 2080 | matrix element. |
| 2081 | */ |
| 2082 | template<typename _Tp> MatIterator_<_Tp> end(); |
| 2083 | template<typename _Tp> MatConstIterator_<_Tp> end() const; |
| 2084 | |
| 2085 | /** @brief Same as end() but for inverse traversal |
| 2086 | */ |
| 2087 | template<typename _Tp> std::reverse_iterator< MatIterator_<_Tp>> rend(); |
| 2088 | template<typename _Tp> std::reverse_iterator< MatConstIterator_<_Tp>> rend() const; |
| 2089 | |
| 2090 | |
| 2091 | /** @brief Runs the given functor over all matrix elements in parallel. |
| 2092 | |
| 2093 | The operation passed as argument has to be a function pointer, a function object or a lambda(C++11). |
| 2094 | |
| 2095 | Example 1. All of the operations below put 0xFF the first channel of all matrix elements: |
| 2096 | @code |
| 2097 | Mat image(1920, 1080, CV_8UC3); |
| 2098 | typedef cv::Point3_<uint8_t> Pixel; |
| 2099 | |
| 2100 | // first. raw pointer access. |
| 2101 | for (int r = 0; r < image.rows; ++r) { |
| 2102 | Pixel* ptr = image.ptr<Pixel>(r, 0); |
| 2103 | const Pixel* ptr_end = ptr + image.cols; |
| 2104 | for (; ptr != ptr_end; ++ptr) { |
| 2105 | ptr->x = 255; |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | // Using MatIterator. (Simple but there are a Iterator's overhead) |
| 2110 | for (Pixel &p : cv::Mat_<Pixel>(image)) { |
| 2111 | p.x = 255; |
| 2112 | } |
| 2113 | |
| 2114 | // Parallel execution with function object. |
| 2115 | struct Operator { |
| 2116 | void operator ()(Pixel &pixel, const int * position) { |
| 2117 | pixel.x = 255; |
| 2118 | } |
| 2119 | }; |
| 2120 | image.forEach<Pixel>(Operator()); |
| 2121 | |
| 2122 | // Parallel execution using C++11 lambda. |
| 2123 | image.forEach<Pixel>([](Pixel &p, const int * position) -> void { |
| 2124 | p.x = 255; |
| 2125 | }); |
| 2126 | @endcode |
| 2127 | Example 2. Using the pixel's position: |
| 2128 | @code |
| 2129 | // Creating 3D matrix (255 x 255 x 255) typed uint8_t |
| 2130 | // and initialize all elements by the value which equals elements position. |
| 2131 | // i.e. pixels (x,y,z) = (1,2,3) is (b,g,r) = (1,2,3). |
| 2132 | |
| 2133 | int sizes[] = { 255, 255, 255 }; |
| 2134 | typedef cv::Point3_<uint8_t> Pixel; |
| 2135 | |
| 2136 | Mat_<Pixel> image = Mat::zeros(3, sizes, CV_8UC3); |
| 2137 | |
| 2138 | image.forEach<Pixel>([](Pixel& pixel, const int position[]) -> void { |
| 2139 | pixel.x = position[0]; |
| 2140 | pixel.y = position[1]; |
| 2141 | pixel.z = position[2]; |
| 2142 | }); |
| 2143 | @endcode |
| 2144 | */ |
| 2145 | template<typename _Tp, typename Functor> void forEach(const Functor& operation); |
| 2146 | /** @overload */ |
| 2147 | template<typename _Tp, typename Functor> void forEach(const Functor& operation) const; |
| 2148 | |
| 2149 | Mat(Mat&& m) CV_NOEXCEPT; |
| 2150 | Mat& operator = (Mat&& m); |
| 2151 | |
| 2152 | enum { MAGIC_VAL = 0x42FF0000, AUTO_STEP = 0, CONTINUOUS_FLAG = CV_MAT_CONT_FLAG, SUBMATRIX_FLAG = CV_SUBMAT_FLAG }; |
| 2153 | enum { MAGIC_MASK = 0xFFFF0000, TYPE_MASK = 0x00000FFF, DEPTH_MASK = 7 }; |
| 2154 | |
| 2155 | /*! includes several bit-fields: |
| 2156 | - the magic signature |
| 2157 | - continuity flag |
| 2158 | - depth |
| 2159 | - number of channels |
| 2160 | */ |
| 2161 | int flags; |
| 2162 | //! the matrix dimensionality, >= 2 |
| 2163 | int dims; |
| 2164 | //! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions |
| 2165 | int rows, cols; |
| 2166 | //! pointer to the data |
| 2167 | uchar* data; |
| 2168 | |
| 2169 | //! helper fields used in locateROI and adjustROI |
| 2170 | const uchar* datastart; |
| 2171 | const uchar* dataend; |
| 2172 | const uchar* datalimit; |
| 2173 | |
| 2174 | //! custom allocator |
| 2175 | MatAllocator* allocator; |
| 2176 | //! and the standard allocator |
| 2177 | static MatAllocator* getStdAllocator(); |
| 2178 | static MatAllocator* getDefaultAllocator(); |
| 2179 | static void setDefaultAllocator(MatAllocator* allocator); |
| 2180 | |
| 2181 | //! internal use method: updates the continuity flag |
| 2182 | void updateContinuityFlag(); |
| 2183 | |
| 2184 | //! interaction with UMat |
| 2185 | UMatData* u; |
| 2186 | |
| 2187 | MatSize size; |
| 2188 | MatStep step; |
| 2189 | |
| 2190 | protected: |
| 2191 | template<typename _Tp, typename Functor> void forEach_impl(const Functor& operation); |
| 2192 | }; |
| 2193 | |
| 2194 | |
| 2195 | ///////////////////////////////// Mat_<_Tp> //////////////////////////////////// |
| 2196 | |
| 2197 | /** @brief Template matrix class derived from Mat |
| 2198 | |
| 2199 | @code{.cpp} |
| 2200 | template<typename _Tp> class Mat_ : public Mat |
| 2201 | { |
| 2202 | public: |
| 2203 | // ... some specific methods |
| 2204 | // and |
| 2205 | // no new extra fields |
| 2206 | }; |
| 2207 | @endcode |
| 2208 | The class `Mat_<_Tp>` is a *thin* template wrapper on top of the Mat class. It does not have any |
| 2209 | extra data fields. Nor this class nor Mat has any virtual methods. Thus, references or pointers to |
| 2210 | these two classes can be freely but carefully converted one to another. For example: |
| 2211 | @code{.cpp} |
| 2212 | // create a 100x100 8-bit matrix |
| 2213 | Mat M(100,100,CV_8U); |
| 2214 | // this will be compiled fine. no any data conversion will be done. |
| 2215 | Mat_<float>& M1 = (Mat_<float>&)M; |
| 2216 | // the program is likely to crash at the statement below |
| 2217 | M1(99,99) = 1.f; |
| 2218 | @endcode |
| 2219 | While Mat is sufficient in most cases, Mat_ can be more convenient if you use a lot of element |
| 2220 | access operations and if you know matrix type at the compilation time. Note that |
| 2221 | `Mat::at(int y,int x)` and `Mat_::operator()(int y,int x)` do absolutely the same |
| 2222 | and run at the same speed, but the latter is certainly shorter: |
| 2223 | @code{.cpp} |
| 2224 | Mat_<double> M(20,20); |
| 2225 | for(int i = 0; i < M.rows; i++) |
| 2226 | for(int j = 0; j < M.cols; j++) |
| 2227 | M(i,j) = 1./(i+j+1); |
| 2228 | Mat E, V; |
| 2229 | eigen(M,E,V); |
| 2230 | cout << E.at<double>(0,0)/E.at<double>(M.rows-1,0); |
| 2231 | @endcode |
| 2232 | To use Mat_ for multi-channel images/matrices, pass Vec as a Mat_ parameter: |
| 2233 | @code{.cpp} |
| 2234 | // allocate a 320x240 color image and fill it with green (in RGB space) |
| 2235 | Mat_<Vec3b> img(240, 320, Vec3b(0,255,0)); |
| 2236 | // now draw a diagonal white line |
| 2237 | for(int i = 0; i < 100; i++) |
| 2238 | img(i,i)=Vec3b(255,255,255); |
| 2239 | // and now scramble the 2nd (red) channel of each pixel |
| 2240 | for(int i = 0; i < img.rows; i++) |
| 2241 | for(int j = 0; j < img.cols; j++) |
| 2242 | img(i,j)[2] ^= (uchar)(i ^ j); |
| 2243 | @endcode |
| 2244 | Mat_ is fully compatible with C++11 range-based for loop. For example such loop |
| 2245 | can be used to safely apply look-up table: |
| 2246 | @code{.cpp} |
| 2247 | void applyTable(Mat_<uchar>& I, const uchar* const table) |
| 2248 | { |
| 2249 | for(auto& pixel : I) |
| 2250 | { |
| 2251 | pixel = table[pixel]; |
| 2252 | } |
| 2253 | } |
| 2254 | @endcode |
| 2255 | */ |
| 2256 | template<typename _Tp> class Mat_ : public Mat |
| 2257 | { |
| 2258 | public: |
| 2259 | typedef _Tp value_type; |
| 2260 | typedef typename DataType<_Tp>::channel_type channel_type; |
| 2261 | typedef MatIterator_<_Tp> iterator; |
| 2262 | typedef MatConstIterator_<_Tp> const_iterator; |
| 2263 | |
| 2264 | //! default constructor |
| 2265 | Mat_() CV_NOEXCEPT; |
| 2266 | //! equivalent to Mat(_rows, _cols, DataType<_Tp>::type) |
| 2267 | Mat_(int _rows, int _cols); |
| 2268 | //! constructor that sets each matrix element to specified value |
| 2269 | Mat_(int _rows, int _cols, const _Tp& value); |
| 2270 | //! equivalent to Mat(_size, DataType<_Tp>::type) |
| 2271 | explicit Mat_(Size _size); |
| 2272 | //! constructor that sets each matrix element to specified value |
| 2273 | Mat_(Size _size, const _Tp& value); |
| 2274 | //! n-dim array constructor |
| 2275 | Mat_(int _ndims, const int* _sizes); |
| 2276 | //! n-dim array constructor that sets each matrix element to specified value |
| 2277 | Mat_(int _ndims, const int* _sizes, const _Tp& value); |
| 2278 | //! copy/conversion constructor. If m is of different type, it's converted |
| 2279 | Mat_(const Mat& m); |
| 2280 | //! copy constructor |
| 2281 | Mat_(const Mat_& m); |
| 2282 | //! constructs a matrix on top of user-allocated data. step is in bytes(!!!), regardless of the type |
| 2283 | Mat_(int _rows, int _cols, _Tp* _data, size_t _step=AUTO_STEP); |
| 2284 | //! constructs n-dim matrix on top of user-allocated data. steps are in bytes(!!!), regardless of the type |
| 2285 | Mat_(int _ndims, const int* _sizes, _Tp* _data, const size_t* _steps=0); |
| 2286 | //! selects a submatrix |
| 2287 | Mat_(const Mat_& m, const Range& rowRange, const Range& colRange=Range::all()); |
| 2288 | //! selects a submatrix |
| 2289 | Mat_(const Mat_& m, const Rect& roi); |
| 2290 | //! selects a submatrix, n-dim version |
| 2291 | Mat_(const Mat_& m, const Range* ranges); |
| 2292 | //! selects a submatrix, n-dim version |
| 2293 | Mat_(const Mat_& m, const std::vector<Range>& ranges); |
| 2294 | //! from a matrix expression |
| 2295 | explicit Mat_(const MatExpr& e); |
| 2296 | //! makes a matrix out of Vec, std::vector, Point_ or Point3_. The matrix will have a single column |
| 2297 | explicit Mat_(const std::vector<_Tp>& vec, bool copyData=false); |
| 2298 | template<int n> explicit Mat_(const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData=true); |
| 2299 | template<int m, int n> explicit Mat_(const Matx<typename DataType<_Tp>::channel_type, m, n>& mtx, bool copyData=true); |
| 2300 | explicit Mat_(const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData=true); |
| 2301 | explicit Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData=true); |
| 2302 | explicit Mat_(const MatCommaInitializer_<_Tp>& commaInitializer); |
| 2303 | |
| 2304 | Mat_(std::initializer_list<_Tp> values); |
| 2305 | explicit Mat_(const std::initializer_list<int> sizes, const std::initializer_list<_Tp> values); |
| 2306 | |
| 2307 | template <std::size_t _Nm> explicit Mat_(const std::array<_Tp, _Nm>& arr, bool copyData=false); |
| 2308 | |
| 2309 | Mat_& operator = (const Mat& m); |
| 2310 | Mat_& operator = (const Mat_& m); |
| 2311 | //! set all the elements to s. |
| 2312 | Mat_& operator = (const _Tp& s); |
| 2313 | //! assign a matrix expression |
| 2314 | Mat_& operator = (const MatExpr& e); |
| 2315 | |
| 2316 | //! iterators; they are smart enough to skip gaps in the end of rows |
| 2317 | iterator begin(); |
| 2318 | iterator end(); |
| 2319 | const_iterator begin() const; |
| 2320 | const_iterator end() const; |
| 2321 | |
| 2322 | //reverse iterators |
| 2323 | std::reverse_iterator<iterator> rbegin(); |
| 2324 | std::reverse_iterator<iterator> rend(); |
| 2325 | std::reverse_iterator<const_iterator> rbegin() const; |
| 2326 | std::reverse_iterator<const_iterator> rend() const; |
| 2327 | |
| 2328 | //! template methods for operation over all matrix elements. |
| 2329 | // the operations take care of skipping gaps in the end of rows (if any) |
| 2330 | template<typename Functor> void forEach(const Functor& operation); |
| 2331 | template<typename Functor> void forEach(const Functor& operation) const; |
| 2332 | |
| 2333 | //! equivalent to Mat::create(_rows, _cols, DataType<_Tp>::type) |
| 2334 | void create(int _rows, int _cols); |
| 2335 | //! equivalent to Mat::create(_size, DataType<_Tp>::type) |
| 2336 | void create(Size _size); |
| 2337 | //! equivalent to Mat::create(_ndims, _sizes, DatType<_Tp>::type) |
| 2338 | void create(int _ndims, const int* _sizes); |
| 2339 | //! equivalent to Mat::release() |
| 2340 | void release(); |
| 2341 | //! cross-product |
| 2342 | Mat_ cross(const Mat_& m) const; |
| 2343 | //! data type conversion |
| 2344 | template<typename T2> operator Mat_<T2>() const; |
| 2345 | //! overridden forms of Mat::row() etc. |
| 2346 | Mat_ row(int y) const; |
| 2347 | Mat_ col(int x) const; |
| 2348 | Mat_ diag(int d=0) const; |
| 2349 | CV_NODISCARD_STD Mat_ clone() const; |
| 2350 | |
| 2351 | //! overridden forms of Mat::elemSize() etc. |
| 2352 | size_t elemSize() const; |
| 2353 | size_t elemSize1() const; |
| 2354 | int type() const; |
| 2355 | int depth() const; |
| 2356 | int channels() const; |
| 2357 | size_t step1(int i=0) const; |
| 2358 | //! returns step()/sizeof(_Tp) |
| 2359 | size_t stepT(int i=0) const; |
| 2360 | |
| 2361 | //! overridden forms of Mat::zeros() etc. Data type is omitted, of course |
| 2362 | CV_NODISCARD_STD static MatExpr zeros(int rows, int cols); |
| 2363 | CV_NODISCARD_STD static MatExpr zeros(Size size); |
| 2364 | CV_NODISCARD_STD static MatExpr zeros(int _ndims, const int* _sizes); |
| 2365 | CV_NODISCARD_STD static MatExpr ones(int rows, int cols); |
| 2366 | CV_NODISCARD_STD static MatExpr ones(Size size); |
| 2367 | CV_NODISCARD_STD static MatExpr ones(int _ndims, const int* _sizes); |
| 2368 | CV_NODISCARD_STD static MatExpr eye(int rows, int cols); |
| 2369 | CV_NODISCARD_STD static MatExpr eye(Size size); |
| 2370 | |
| 2371 | //! some more overridden methods |
| 2372 | Mat_& adjustROI( int dtop, int dbottom, int dleft, int dright ); |
| 2373 | Mat_ operator()( const Range& rowRange, const Range& colRange ) const; |
| 2374 | Mat_ operator()( const Rect& roi ) const; |
| 2375 | Mat_ operator()( const Range* ranges ) const; |
| 2376 | Mat_ operator()(const std::vector<Range>& ranges) const; |
| 2377 | |
| 2378 | //! more convenient forms of row and element access operators |
| 2379 | _Tp* operator [](int y); |
| 2380 | const _Tp* operator [](int y) const; |
| 2381 | |
| 2382 | //! returns reference to the specified element |
| 2383 | _Tp& operator ()(const int* idx); |
| 2384 | //! returns read-only reference to the specified element |
| 2385 | const _Tp& operator ()(const int* idx) const; |
| 2386 | |
| 2387 | //! returns reference to the specified element |
| 2388 | template<int n> _Tp& operator ()(const Vec<int, n>& idx); |
| 2389 | //! returns read-only reference to the specified element |
| 2390 | template<int n> const _Tp& operator ()(const Vec<int, n>& idx) const; |
| 2391 | |
| 2392 | //! returns reference to the specified element (1D case) |
| 2393 | _Tp& operator ()(int idx0); |
| 2394 | //! returns read-only reference to the specified element (1D case) |
| 2395 | const _Tp& operator ()(int idx0) const; |
| 2396 | //! returns reference to the specified element (2D case) |
| 2397 | _Tp& operator ()(int row, int col); |
| 2398 | //! returns read-only reference to the specified element (2D case) |
| 2399 | const _Tp& operator ()(int row, int col) const; |
| 2400 | //! returns reference to the specified element (3D case) |
| 2401 | _Tp& operator ()(int idx0, int idx1, int idx2); |
| 2402 | //! returns read-only reference to the specified element (3D case) |
| 2403 | const _Tp& operator ()(int idx0, int idx1, int idx2) const; |
| 2404 | |
| 2405 | _Tp& operator ()(Point pt); |
| 2406 | const _Tp& operator ()(Point pt) const; |
| 2407 | |
| 2408 | //! conversion to vector. |
| 2409 | operator std::vector<_Tp>() const; |
| 2410 | |
| 2411 | //! conversion to array. |
| 2412 | template<std::size_t _Nm> operator std::array<_Tp, _Nm>() const; |
| 2413 | |
| 2414 | //! conversion to Vec |
| 2415 | template<int n> operator Vec<typename DataType<_Tp>::channel_type, n>() const; |
| 2416 | //! conversion to Matx |
| 2417 | template<int m, int n> operator Matx<typename DataType<_Tp>::channel_type, m, n>() const; |
| 2418 | |
| 2419 | Mat_(Mat_&& m); |
| 2420 | Mat_& operator = (Mat_&& m); |
| 2421 | |
| 2422 | Mat_(Mat&& m); |
| 2423 | Mat_& operator = (Mat&& m); |
| 2424 | |
| 2425 | Mat_(MatExpr&& e); |
| 2426 | }; |
| 2427 | |
| 2428 | typedef Mat_<uchar> Mat1b; |
| 2429 | typedef Mat_<Vec2b> Mat2b; |
| 2430 | typedef Mat_<Vec3b> Mat3b; |
| 2431 | typedef Mat_<Vec4b> Mat4b; |
| 2432 | |
| 2433 | typedef Mat_<short> Mat1s; |
| 2434 | typedef Mat_<Vec2s> Mat2s; |
| 2435 | typedef Mat_<Vec3s> Mat3s; |
| 2436 | typedef Mat_<Vec4s> Mat4s; |
| 2437 | |
| 2438 | typedef Mat_<ushort> Mat1w; |
| 2439 | typedef Mat_<Vec2w> Mat2w; |
| 2440 | typedef Mat_<Vec3w> Mat3w; |
| 2441 | typedef Mat_<Vec4w> Mat4w; |
| 2442 | |
| 2443 | typedef Mat_<int> Mat1i; |
| 2444 | typedef Mat_<Vec2i> Mat2i; |
| 2445 | typedef Mat_<Vec3i> Mat3i; |
| 2446 | typedef Mat_<Vec4i> Mat4i; |
| 2447 | |
| 2448 | typedef Mat_<float> Mat1f; |
| 2449 | typedef Mat_<Vec2f> Mat2f; |
| 2450 | typedef Mat_<Vec3f> Mat3f; |
| 2451 | typedef Mat_<Vec4f> Mat4f; |
| 2452 | |
| 2453 | typedef Mat_<double> Mat1d; |
| 2454 | typedef Mat_<Vec2d> Mat2d; |
| 2455 | typedef Mat_<Vec3d> Mat3d; |
| 2456 | typedef Mat_<Vec4d> Mat4d; |
| 2457 | |
| 2458 | /** @todo document */ |
| 2459 | class CV_EXPORTS UMat |
| 2460 | { |
| 2461 | public: |
| 2462 | //! default constructor |
| 2463 | UMat(UMatUsageFlags usageFlags = USAGE_DEFAULT) CV_NOEXCEPT; |
| 2464 | //! constructs 2D matrix of the specified size and type |
| 2465 | // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.) |
| 2466 | UMat(int rows, int cols, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); |
| 2467 | UMat(Size size, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); |
| 2468 | //! constructs 2D matrix and fills it with the specified value _s. |
| 2469 | UMat(int rows, int cols, int type, const Scalar& s, UMatUsageFlags usageFlags = USAGE_DEFAULT); |
| 2470 | UMat(Size size, int type, const Scalar& s, UMatUsageFlags usageFlags = USAGE_DEFAULT); |
| 2471 | |
| 2472 | //! constructs n-dimensional matrix |
| 2473 | UMat(int ndims, const int* sizes, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); |
| 2474 | UMat(int ndims, const int* sizes, int type, const Scalar& s, UMatUsageFlags usageFlags = USAGE_DEFAULT); |
| 2475 | |
| 2476 | //! copy constructor |
| 2477 | UMat(const UMat& m); |
| 2478 | |
| 2479 | //! creates a matrix header for a part of the bigger matrix |
| 2480 | UMat(const UMat& m, const Range& rowRange, const Range& colRange=Range::all()); |
| 2481 | UMat(const UMat& m, const Rect& roi); |
| 2482 | UMat(const UMat& m, const Range* ranges); |
| 2483 | UMat(const UMat& m, const std::vector<Range>& ranges); |
| 2484 | |
| 2485 | //! builds matrix from std::vector. The data is always copied. The copyData |
| 2486 | //! parameter is deprecated and will be removed in OpenCV 5.0. |
| 2487 | template<typename _Tp> explicit UMat(const std::vector<_Tp>& vec, bool copyData=false); |
| 2488 | |
| 2489 | //! destructor - calls release() |
| 2490 | ~UMat(); |
| 2491 | //! assignment operators |
| 2492 | UMat& operator = (const UMat& m); |
| 2493 | |
| 2494 | Mat getMat(AccessFlag flags) const; |
| 2495 | |
| 2496 | //! returns a new matrix header for the specified row |
| 2497 | UMat row(int y) const; |
| 2498 | //! returns a new matrix header for the specified column |
| 2499 | UMat col(int x) const; |
| 2500 | //! ... for the specified row span |
| 2501 | UMat rowRange(int startrow, int endrow) const; |
| 2502 | UMat rowRange(const Range& r) const; |
| 2503 | //! ... for the specified column span |
| 2504 | UMat colRange(int startcol, int endcol) const; |
| 2505 | UMat colRange(const Range& r) const; |
| 2506 | //! ... for the specified diagonal |
| 2507 | //! (d=0 - the main diagonal, |
| 2508 | //! >0 - a diagonal from the upper half, |
| 2509 | //! <0 - a diagonal from the lower half) |
| 2510 | UMat diag(int d=0) const; |
| 2511 | //! constructs a square diagonal matrix which main diagonal is vector "d" |
| 2512 | CV_NODISCARD_STD static UMat diag(const UMat& d, UMatUsageFlags usageFlags /*= USAGE_DEFAULT*/); |
| 2513 | CV_NODISCARD_STD static UMat diag(const UMat& d) { return diag(d, usageFlags: USAGE_DEFAULT); } // OpenCV 5.0: remove abi compatibility overload |
| 2514 | |
| 2515 | //! returns deep copy of the matrix, i.e. the data is copied |
| 2516 | CV_NODISCARD_STD UMat clone() const; |
| 2517 | //! copies the matrix content to "m". |
| 2518 | // It calls m.create(this->size(), this->type()). |
| 2519 | void copyTo( OutputArray m ) const; |
| 2520 | //! copies those matrix elements to "m" that are marked with non-zero mask elements. |
| 2521 | void copyTo( OutputArray m, InputArray mask ) const; |
| 2522 | //! converts matrix to another datatype with optional scaling. See cvConvertScale. |
| 2523 | void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const; |
| 2524 | |
| 2525 | void assignTo( UMat& m, int type=-1 ) const; |
| 2526 | |
| 2527 | //! sets every matrix element to s |
| 2528 | UMat& operator = (const Scalar& s); |
| 2529 | //! sets some of the matrix elements to s, according to the mask |
| 2530 | UMat& setTo(InputArray value, InputArray mask=noArray()); |
| 2531 | //! creates alternative matrix header for the same data, with different |
| 2532 | // number of channels and/or different number of rows. see cvReshape. |
| 2533 | UMat reshape(int cn, int rows=0) const; |
| 2534 | UMat reshape(int cn, int newndims, const int* newsz) const; |
| 2535 | |
| 2536 | //! matrix transposition by means of matrix expressions |
| 2537 | UMat t() const; |
| 2538 | //! matrix inversion by means of matrix expressions |
| 2539 | UMat inv(int method=DECOMP_LU) const; |
| 2540 | //! per-element matrix multiplication by means of matrix expressions |
| 2541 | UMat mul(InputArray m, double scale=1) const; |
| 2542 | |
| 2543 | //! computes dot-product |
| 2544 | double dot(InputArray m) const; |
| 2545 | |
| 2546 | //! Matlab-style matrix initialization |
| 2547 | CV_NODISCARD_STD static UMat zeros(int rows, int cols, int type, UMatUsageFlags usageFlags /*= USAGE_DEFAULT*/); |
| 2548 | CV_NODISCARD_STD static UMat zeros(Size size, int type, UMatUsageFlags usageFlags /*= USAGE_DEFAULT*/); |
| 2549 | CV_NODISCARD_STD static UMat zeros(int ndims, const int* sz, int type, UMatUsageFlags usageFlags /*= USAGE_DEFAULT*/); |
| 2550 | CV_NODISCARD_STD static UMat zeros(int rows, int cols, int type) { return zeros(rows, cols, type, usageFlags: USAGE_DEFAULT); } // OpenCV 5.0: remove abi compatibility overload |
| 2551 | CV_NODISCARD_STD static UMat zeros(Size size, int type) { return zeros(size, type, usageFlags: USAGE_DEFAULT); } // OpenCV 5.0: remove abi compatibility overload |
| 2552 | CV_NODISCARD_STD static UMat zeros(int ndims, const int* sz, int type) { return zeros(ndims, sz, type, usageFlags: USAGE_DEFAULT); } // OpenCV 5.0: remove abi compatibility overload |
| 2553 | CV_NODISCARD_STD static UMat ones(int rows, int cols, int type, UMatUsageFlags usageFlags /*= USAGE_DEFAULT*/); |
| 2554 | CV_NODISCARD_STD static UMat ones(Size size, int type, UMatUsageFlags usageFlags /*= USAGE_DEFAULT*/); |
| 2555 | CV_NODISCARD_STD static UMat ones(int ndims, const int* sz, int type, UMatUsageFlags usageFlags /*= USAGE_DEFAULT*/); |
| 2556 | CV_NODISCARD_STD static UMat ones(int rows, int cols, int type) { return ones(rows, cols, type, usageFlags: USAGE_DEFAULT); } // OpenCV 5.0: remove abi compatibility overload |
| 2557 | CV_NODISCARD_STD static UMat ones(Size size, int type) { return ones(size, type, usageFlags: USAGE_DEFAULT); } // OpenCV 5.0: remove abi compatibility overload |
| 2558 | CV_NODISCARD_STD static UMat ones(int ndims, const int* sz, int type) { return ones(ndims, sz, type, usageFlags: USAGE_DEFAULT); } // OpenCV 5.0: remove abi compatibility overload |
| 2559 | CV_NODISCARD_STD static UMat eye(int rows, int cols, int type, UMatUsageFlags usageFlags /*= USAGE_DEFAULT*/); |
| 2560 | CV_NODISCARD_STD static UMat eye(Size size, int type, UMatUsageFlags usageFlags /*= USAGE_DEFAULT*/); |
| 2561 | CV_NODISCARD_STD static UMat eye(int rows, int cols, int type) { return eye(rows, cols, type, usageFlags: USAGE_DEFAULT); } // OpenCV 5.0: remove abi compatibility overload |
| 2562 | CV_NODISCARD_STD static UMat eye(Size size, int type) { return eye(size, type, usageFlags: USAGE_DEFAULT); } // OpenCV 5.0: remove abi compatibility overload |
| 2563 | |
| 2564 | //! allocates new matrix data unless the matrix already has specified size and type. |
| 2565 | // previous data is unreferenced if needed. |
| 2566 | void create(int rows, int cols, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); |
| 2567 | void create(Size size, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); |
| 2568 | void create(int ndims, const int* sizes, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); |
| 2569 | void create(const std::vector<int>& sizes, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); |
| 2570 | |
| 2571 | //! increases the reference counter; use with care to avoid memleaks |
| 2572 | void addref(); |
| 2573 | //! decreases reference counter; |
| 2574 | // deallocates the data when reference counter reaches 0. |
| 2575 | void release(); |
| 2576 | |
| 2577 | //! deallocates the matrix data |
| 2578 | void deallocate(); |
| 2579 | //! internal use function; properly re-allocates _size, _step arrays |
| 2580 | void copySize(const UMat& m); |
| 2581 | |
| 2582 | //! locates matrix header within a parent matrix. See below |
| 2583 | void locateROI( Size& wholeSize, Point& ofs ) const; |
| 2584 | //! moves/resizes the current matrix ROI inside the parent matrix. |
| 2585 | UMat& adjustROI( int dtop, int dbottom, int dleft, int dright ); |
| 2586 | //! extracts a rectangular sub-matrix |
| 2587 | // (this is a generalized form of row, rowRange etc.) |
| 2588 | UMat operator()( Range rowRange, Range colRange ) const; |
| 2589 | UMat operator()( const Rect& roi ) const; |
| 2590 | UMat operator()( const Range* ranges ) const; |
| 2591 | UMat operator()(const std::vector<Range>& ranges) const; |
| 2592 | |
| 2593 | //! returns true iff the matrix data is continuous |
| 2594 | // (i.e. when there are no gaps between successive rows). |
| 2595 | // similar to CV_IS_MAT_CONT(cvmat->type) |
| 2596 | bool isContinuous() const; |
| 2597 | |
| 2598 | //! returns true if the matrix is a submatrix of another matrix |
| 2599 | bool isSubmatrix() const; |
| 2600 | |
| 2601 | //! returns element size in bytes, |
| 2602 | // similar to CV_ELEM_SIZE(cvmat->type) |
| 2603 | size_t elemSize() const; |
| 2604 | //! returns the size of element channel in bytes. |
| 2605 | size_t elemSize1() const; |
| 2606 | //! returns element type, similar to CV_MAT_TYPE(cvmat->type) |
| 2607 | int type() const; |
| 2608 | //! returns element type, similar to CV_MAT_DEPTH(cvmat->type) |
| 2609 | int depth() const; |
| 2610 | //! returns element type, similar to CV_MAT_CN(cvmat->type) |
| 2611 | int channels() const; |
| 2612 | //! returns step/elemSize1() |
| 2613 | size_t step1(int i=0) const; |
| 2614 | //! returns true if matrix data is NULL |
| 2615 | bool empty() const; |
| 2616 | //! returns the total number of matrix elements |
| 2617 | size_t total() const; |
| 2618 | |
| 2619 | //! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise |
| 2620 | int checkVector(int elemChannels, int depth=-1, bool requireContinuous=true) const; |
| 2621 | |
| 2622 | UMat(UMat&& m); |
| 2623 | UMat& operator = (UMat&& m); |
| 2624 | |
| 2625 | /*! Returns the OpenCL buffer handle on which UMat operates on. |
| 2626 | The UMat instance should be kept alive during the use of the handle to prevent the buffer to be |
| 2627 | returned to the OpenCV buffer pool. |
| 2628 | */ |
| 2629 | void* handle(AccessFlag accessFlags) const; |
| 2630 | void ndoffset(size_t* ofs) const; |
| 2631 | |
| 2632 | enum { MAGIC_VAL = 0x42FF0000, AUTO_STEP = 0, CONTINUOUS_FLAG = CV_MAT_CONT_FLAG, SUBMATRIX_FLAG = CV_SUBMAT_FLAG }; |
| 2633 | enum { MAGIC_MASK = 0xFFFF0000, TYPE_MASK = 0x00000FFF, DEPTH_MASK = 7 }; |
| 2634 | |
| 2635 | /*! includes several bit-fields: |
| 2636 | - the magic signature |
| 2637 | - continuity flag |
| 2638 | - depth |
| 2639 | - number of channels |
| 2640 | */ |
| 2641 | int flags; |
| 2642 | |
| 2643 | //! the matrix dimensionality, >= 2 |
| 2644 | int dims; |
| 2645 | |
| 2646 | //! number of rows in the matrix; -1 when the matrix has more than 2 dimensions |
| 2647 | int rows; |
| 2648 | |
| 2649 | //! number of columns in the matrix; -1 when the matrix has more than 2 dimensions |
| 2650 | int cols; |
| 2651 | |
| 2652 | //! custom allocator |
| 2653 | MatAllocator* allocator; |
| 2654 | |
| 2655 | //! usage flags for allocator; recommend do not set directly, instead set during construct/create/getUMat |
| 2656 | UMatUsageFlags usageFlags; |
| 2657 | |
| 2658 | //! and the standard allocator |
| 2659 | static MatAllocator* getStdAllocator(); |
| 2660 | |
| 2661 | //! internal use method: updates the continuity flag |
| 2662 | void updateContinuityFlag(); |
| 2663 | |
| 2664 | //! black-box container of UMat data |
| 2665 | UMatData* u; |
| 2666 | |
| 2667 | //! offset of the submatrix (or 0) |
| 2668 | size_t offset; |
| 2669 | |
| 2670 | //! dimensional size of the matrix; accessible in various formats |
| 2671 | MatSize size; |
| 2672 | |
| 2673 | //! number of bytes each matrix element/row/plane/dimension occupies |
| 2674 | MatStep step; |
| 2675 | |
| 2676 | protected: |
| 2677 | }; |
| 2678 | |
| 2679 | |
| 2680 | /////////////////////////// multi-dimensional sparse matrix ////////////////////////// |
| 2681 | |
| 2682 | /** @brief The class SparseMat represents multi-dimensional sparse numerical arrays. |
| 2683 | |
| 2684 | Such a sparse array can store elements of any type that Mat can store. *Sparse* means that only |
| 2685 | non-zero elements are stored (though, as a result of operations on a sparse matrix, some of its |
| 2686 | stored elements can actually become 0. It is up to you to detect such elements and delete them |
| 2687 | using SparseMat::erase ). The non-zero elements are stored in a hash table that grows when it is |
| 2688 | filled so that the search time is O(1) in average (regardless of whether element is there or not). |
| 2689 | Elements can be accessed using the following methods: |
| 2690 | - Query operations (SparseMat::ptr and the higher-level SparseMat::ref, SparseMat::value and |
| 2691 | SparseMat::find), for example: |
| 2692 | @code |
| 2693 | const int dims = 5; |
| 2694 | int size[5] = {10, 10, 10, 10, 10}; |
| 2695 | SparseMat sparse_mat(dims, size, CV_32F); |
| 2696 | for(int i = 0; i < 1000; i++) |
| 2697 | { |
| 2698 | int idx[dims]; |
| 2699 | for(int k = 0; k < dims; k++) |
| 2700 | idx[k] = rand() % size[k]; |
| 2701 | sparse_mat.ref<float>(idx) += 1.f; |
| 2702 | } |
| 2703 | cout << "nnz = " << sparse_mat.nzcount() << endl; |
| 2704 | @endcode |
| 2705 | - Sparse matrix iterators. They are similar to MatIterator but different from NAryMatIterator. |
| 2706 | That is, the iteration loop is familiar to STL users: |
| 2707 | @code |
| 2708 | // prints elements of a sparse floating-point matrix |
| 2709 | // and the sum of elements. |
| 2710 | SparseMatConstIterator_<float> |
| 2711 | it = sparse_mat.begin<float>(), |
| 2712 | it_end = sparse_mat.end<float>(); |
| 2713 | double s = 0; |
| 2714 | int dims = sparse_mat.dims(); |
| 2715 | for(; it != it_end; ++it) |
| 2716 | { |
| 2717 | // print element indices and the element value |
| 2718 | const SparseMat::Node* n = it.node(); |
| 2719 | printf("("); |
| 2720 | for(int i = 0; i < dims; i++) |
| 2721 | printf("%d%s", n->idx[i], i < dims-1 ? ", " : ")"); |
| 2722 | printf(": %g\n", it.value<float>()); |
| 2723 | s += *it; |
| 2724 | } |
| 2725 | printf("Element sum is %g\n", s); |
| 2726 | @endcode |
| 2727 | If you run this loop, you will notice that elements are not enumerated in a logical order |
| 2728 | (lexicographical, and so on). They come in the same order as they are stored in the hash table |
| 2729 | (semi-randomly). You may collect pointers to the nodes and sort them to get the proper ordering. |
| 2730 | Note, however, that pointers to the nodes may become invalid when you add more elements to the |
| 2731 | matrix. This may happen due to possible buffer reallocation. |
| 2732 | - Combination of the above 2 methods when you need to process 2 or more sparse matrices |
| 2733 | simultaneously. For example, this is how you can compute unnormalized cross-correlation of the 2 |
| 2734 | floating-point sparse matrices: |
| 2735 | @code |
| 2736 | double cross_corr(const SparseMat& a, const SparseMat& b) |
| 2737 | { |
| 2738 | const SparseMat *_a = &a, *_b = &b; |
| 2739 | // if b contains less elements than a, |
| 2740 | // it is faster to iterate through b |
| 2741 | if(_a->nzcount() > _b->nzcount()) |
| 2742 | std::swap(_a, _b); |
| 2743 | SparseMatConstIterator_<float> it = _a->begin<float>(), |
| 2744 | it_end = _a->end<float>(); |
| 2745 | double ccorr = 0; |
| 2746 | for(; it != it_end; ++it) |
| 2747 | { |
| 2748 | // take the next element from the first matrix |
| 2749 | float avalue = *it; |
| 2750 | const Node* anode = it.node(); |
| 2751 | // and try to find an element with the same index in the second matrix. |
| 2752 | // since the hash value depends only on the element index, |
| 2753 | // reuse the hash value stored in the node |
| 2754 | float bvalue = _b->value<float>(anode->idx,&anode->hashval); |
| 2755 | ccorr += avalue*bvalue; |
| 2756 | } |
| 2757 | return ccorr; |
| 2758 | } |
| 2759 | @endcode |
| 2760 | */ |
| 2761 | class CV_EXPORTS SparseMat |
| 2762 | { |
| 2763 | public: |
| 2764 | typedef SparseMatIterator iterator; |
| 2765 | typedef SparseMatConstIterator const_iterator; |
| 2766 | |
| 2767 | enum { MAGIC_VAL=0x42FD0000, MAX_DIM=32, HASH_SCALE=0x5bd1e995, HASH_BIT=0x80000000 }; |
| 2768 | |
| 2769 | //! the sparse matrix header |
| 2770 | struct CV_EXPORTS Hdr |
| 2771 | { |
| 2772 | Hdr(int _dims, const int* _sizes, int _type); |
| 2773 | void clear(); |
| 2774 | int refcount; |
| 2775 | int dims; |
| 2776 | int valueOffset; |
| 2777 | size_t nodeSize; |
| 2778 | size_t nodeCount; |
| 2779 | size_t freeList; |
| 2780 | std::vector<uchar> pool; |
| 2781 | std::vector<size_t> hashtab; |
| 2782 | int size[MAX_DIM]; |
| 2783 | }; |
| 2784 | |
| 2785 | //! sparse matrix node - element of a hash table |
| 2786 | struct CV_EXPORTS Node |
| 2787 | { |
| 2788 | //! hash value |
| 2789 | size_t hashval; |
| 2790 | //! index of the next node in the same hash table entry |
| 2791 | size_t next; |
| 2792 | //! index of the matrix element |
| 2793 | int idx[MAX_DIM]; |
| 2794 | }; |
| 2795 | |
| 2796 | /** @brief Various SparseMat constructors. |
| 2797 | */ |
| 2798 | SparseMat(); |
| 2799 | |
| 2800 | /** @overload |
| 2801 | @param dims Array dimensionality. |
| 2802 | @param _sizes Sparce matrix size on all dementions. |
| 2803 | @param _type Sparse matrix data type. |
| 2804 | */ |
| 2805 | SparseMat(int dims, const int* _sizes, int _type); |
| 2806 | |
| 2807 | /** @overload |
| 2808 | @param m Source matrix for copy constructor. If m is dense matrix (ocvMat) then it will be converted |
| 2809 | to sparse representation. |
| 2810 | */ |
| 2811 | SparseMat(const SparseMat& m); |
| 2812 | |
| 2813 | /** @overload |
| 2814 | @param m Source matrix for copy constructor. If m is dense matrix (ocvMat) then it will be converted |
| 2815 | to sparse representation. |
| 2816 | */ |
| 2817 | explicit SparseMat(const Mat& m); |
| 2818 | |
| 2819 | //! the destructor |
| 2820 | ~SparseMat(); |
| 2821 | |
| 2822 | //! assignment operator. This is O(1) operation, i.e. no data is copied |
| 2823 | SparseMat& operator = (const SparseMat& m); |
| 2824 | //! equivalent to the corresponding constructor |
| 2825 | SparseMat& operator = (const Mat& m); |
| 2826 | |
| 2827 | //! creates full copy of the matrix |
| 2828 | CV_NODISCARD_STD SparseMat clone() const; |
| 2829 | |
| 2830 | //! copies all the data to the destination matrix. All the previous content of m is erased |
| 2831 | void copyTo( SparseMat& m ) const; |
| 2832 | //! converts sparse matrix to dense matrix. |
| 2833 | void copyTo( Mat& m ) const; |
| 2834 | //! multiplies all the matrix elements by the specified scale factor alpha and converts the results to the specified data type |
| 2835 | void convertTo( SparseMat& m, int rtype, double alpha=1 ) const; |
| 2836 | //! converts sparse matrix to dense n-dim matrix with optional type conversion and scaling. |
| 2837 | /*! |
| 2838 | @param [out] m - output matrix; if it does not have a proper size or type before the operation, |
| 2839 | it is reallocated |
| 2840 | @param [in] rtype - desired output matrix type or, rather, the depth since the number of channels |
| 2841 | are the same as the input has; if rtype is negative, the output matrix will have the |
| 2842 | same type as the input. |
| 2843 | @param [in] alpha - optional scale factor |
| 2844 | @param [in] beta - optional delta added to the scaled values |
| 2845 | */ |
| 2846 | void convertTo( Mat& m, int rtype, double alpha=1, double beta=0 ) const; |
| 2847 | |
| 2848 | // not used now |
| 2849 | void assignTo( SparseMat& m, int type=-1 ) const; |
| 2850 | |
| 2851 | //! reallocates sparse matrix. |
| 2852 | /*! |
| 2853 | If the matrix already had the proper size and type, |
| 2854 | it is simply cleared with clear(), otherwise, |
| 2855 | the old matrix is released (using release()) and the new one is allocated. |
| 2856 | */ |
| 2857 | void create(int dims, const int* _sizes, int _type); |
| 2858 | //! sets all the sparse matrix elements to 0, which means clearing the hash table. |
| 2859 | void clear(); |
| 2860 | //! manually increments the reference counter to the header. |
| 2861 | void addref(); |
| 2862 | // decrements the header reference counter. When the counter reaches 0, the header and all the underlying data are deallocated. |
| 2863 | void release(); |
| 2864 | |
| 2865 | //! converts sparse matrix to the old-style representation; all the elements are copied. |
| 2866 | //operator CvSparseMat*() const; |
| 2867 | //! returns the size of each element in bytes (not including the overhead - the space occupied by SparseMat::Node elements) |
| 2868 | size_t elemSize() const; |
| 2869 | //! returns elemSize()/channels() |
| 2870 | size_t elemSize1() const; |
| 2871 | |
| 2872 | //! returns type of sparse matrix elements |
| 2873 | int type() const; |
| 2874 | //! returns the depth of sparse matrix elements |
| 2875 | int depth() const; |
| 2876 | //! returns the number of channels |
| 2877 | int channels() const; |
| 2878 | |
| 2879 | //! returns the array of sizes, or NULL if the matrix is not allocated |
| 2880 | const int* size() const; |
| 2881 | //! returns the size of i-th matrix dimension (or 0) |
| 2882 | int size(int i) const; |
| 2883 | //! returns the matrix dimensionality |
| 2884 | int dims() const; |
| 2885 | //! returns the number of non-zero elements (=the number of hash table nodes) |
| 2886 | size_t nzcount() const; |
| 2887 | |
| 2888 | //! computes the element hash value (1D case) |
| 2889 | size_t hash(int i0) const; |
| 2890 | //! computes the element hash value (2D case) |
| 2891 | size_t hash(int i0, int i1) const; |
| 2892 | //! computes the element hash value (3D case) |
| 2893 | size_t hash(int i0, int i1, int i2) const; |
| 2894 | //! computes the element hash value (nD case) |
| 2895 | size_t hash(const int* idx) const; |
| 2896 | |
| 2897 | //!@{ |
| 2898 | /*! |
| 2899 | specialized variants for 1D, 2D, 3D cases and the generic_type one for n-D case. |
| 2900 | return pointer to the matrix element. |
| 2901 | - if the element is there (it's non-zero), the pointer to it is returned |
| 2902 | - if it's not there and createMissing=false, NULL pointer is returned |
| 2903 | - if it's not there and createMissing=true, then the new element |
| 2904 | is created and initialized with 0. Pointer to it is returned |
| 2905 | - if the optional hashval pointer is not NULL, the element hash value is |
| 2906 | not computed, but *hashval is taken instead. |
| 2907 | */ |
| 2908 | //! returns pointer to the specified element (1D case) |
| 2909 | uchar* ptr(int i0, bool createMissing, size_t* hashval=0); |
| 2910 | //! returns pointer to the specified element (2D case) |
| 2911 | uchar* ptr(int i0, int i1, bool createMissing, size_t* hashval=0); |
| 2912 | //! returns pointer to the specified element (3D case) |
| 2913 | uchar* ptr(int i0, int i1, int i2, bool createMissing, size_t* hashval=0); |
| 2914 | //! returns pointer to the specified element (nD case) |
| 2915 | uchar* ptr(const int* idx, bool createMissing, size_t* hashval=0); |
| 2916 | //!@} |
| 2917 | |
| 2918 | //!@{ |
| 2919 | /*! |
| 2920 | return read-write reference to the specified sparse matrix element. |
| 2921 | |
| 2922 | `ref<_Tp>(i0,...[,hashval])` is equivalent to `*(_Tp*)ptr(i0,...,true[,hashval])`. |
| 2923 | The methods always return a valid reference. |
| 2924 | If the element did not exist, it is created and initialized with 0. |
| 2925 | */ |
| 2926 | //! returns reference to the specified element (1D case) |
| 2927 | template<typename _Tp> _Tp& ref(int i0, size_t* hashval=0); |
| 2928 | //! returns reference to the specified element (2D case) |
| 2929 | template<typename _Tp> _Tp& ref(int i0, int i1, size_t* hashval=0); |
| 2930 | //! returns reference to the specified element (3D case) |
| 2931 | template<typename _Tp> _Tp& ref(int i0, int i1, int i2, size_t* hashval=0); |
| 2932 | //! returns reference to the specified element (nD case) |
| 2933 | template<typename _Tp> _Tp& ref(const int* idx, size_t* hashval=0); |
| 2934 | //!@} |
| 2935 | |
| 2936 | //!@{ |
| 2937 | /*! |
| 2938 | return value of the specified sparse matrix element. |
| 2939 | |
| 2940 | `value<_Tp>(i0,...[,hashval])` is equivalent to |
| 2941 | @code |
| 2942 | { const _Tp* p = find<_Tp>(i0,...[,hashval]); return p ? *p : _Tp(); } |
| 2943 | @endcode |
| 2944 | |
| 2945 | That is, if the element did not exist, the methods return 0. |
| 2946 | */ |
| 2947 | //! returns value of the specified element (1D case) |
| 2948 | template<typename _Tp> _Tp value(int i0, size_t* hashval=0) const; |
| 2949 | //! returns value of the specified element (2D case) |
| 2950 | template<typename _Tp> _Tp value(int i0, int i1, size_t* hashval=0) const; |
| 2951 | //! returns value of the specified element (3D case) |
| 2952 | template<typename _Tp> _Tp value(int i0, int i1, int i2, size_t* hashval=0) const; |
| 2953 | //! returns value of the specified element (nD case) |
| 2954 | template<typename _Tp> _Tp value(const int* idx, size_t* hashval=0) const; |
| 2955 | //!@} |
| 2956 | |
| 2957 | //!@{ |
| 2958 | /*! |
| 2959 | Return pointer to the specified sparse matrix element if it exists |
| 2960 | |
| 2961 | `find<_Tp>(i0,...[,hashval])` is equivalent to `(_const Tp*)ptr(i0,...false[,hashval])`. |
| 2962 | |
| 2963 | If the specified element does not exist, the methods return NULL. |
| 2964 | */ |
| 2965 | //! returns pointer to the specified element (1D case) |
| 2966 | template<typename _Tp> const _Tp* find(int i0, size_t* hashval=0) const; |
| 2967 | //! returns pointer to the specified element (2D case) |
| 2968 | template<typename _Tp> const _Tp* find(int i0, int i1, size_t* hashval=0) const; |
| 2969 | //! returns pointer to the specified element (3D case) |
| 2970 | template<typename _Tp> const _Tp* find(int i0, int i1, int i2, size_t* hashval=0) const; |
| 2971 | //! returns pointer to the specified element (nD case) |
| 2972 | template<typename _Tp> const _Tp* find(const int* idx, size_t* hashval=0) const; |
| 2973 | //!@} |
| 2974 | |
| 2975 | //! erases the specified element (2D case) |
| 2976 | void erase(int i0, int i1, size_t* hashval=0); |
| 2977 | //! erases the specified element (3D case) |
| 2978 | void erase(int i0, int i1, int i2, size_t* hashval=0); |
| 2979 | //! erases the specified element (nD case) |
| 2980 | void erase(const int* idx, size_t* hashval=0); |
| 2981 | |
| 2982 | //!@{ |
| 2983 | /*! |
| 2984 | return the sparse matrix iterator pointing to the first sparse matrix element |
| 2985 | */ |
| 2986 | //! returns the sparse matrix iterator at the matrix beginning |
| 2987 | SparseMatIterator begin(); |
| 2988 | //! returns the sparse matrix iterator at the matrix beginning |
| 2989 | template<typename _Tp> SparseMatIterator_<_Tp> begin(); |
| 2990 | //! returns the read-only sparse matrix iterator at the matrix beginning |
| 2991 | SparseMatConstIterator begin() const; |
| 2992 | //! returns the read-only sparse matrix iterator at the matrix beginning |
| 2993 | template<typename _Tp> SparseMatConstIterator_<_Tp> begin() const; |
| 2994 | //!@} |
| 2995 | /*! |
| 2996 | return the sparse matrix iterator pointing to the element following the last sparse matrix element |
| 2997 | */ |
| 2998 | //! returns the sparse matrix iterator at the matrix end |
| 2999 | SparseMatIterator end(); |
| 3000 | //! returns the read-only sparse matrix iterator at the matrix end |
| 3001 | SparseMatConstIterator end() const; |
| 3002 | //! returns the typed sparse matrix iterator at the matrix end |
| 3003 | template<typename _Tp> SparseMatIterator_<_Tp> end(); |
| 3004 | //! returns the typed read-only sparse matrix iterator at the matrix end |
| 3005 | template<typename _Tp> SparseMatConstIterator_<_Tp> end() const; |
| 3006 | |
| 3007 | //! returns the value stored in the sparse martix node |
| 3008 | template<typename _Tp> _Tp& value(Node* n); |
| 3009 | //! returns the value stored in the sparse martix node |
| 3010 | template<typename _Tp> const _Tp& value(const Node* n) const; |
| 3011 | |
| 3012 | ////////////// some internal-use methods /////////////// |
| 3013 | Node* node(size_t nidx); |
| 3014 | const Node* node(size_t nidx) const; |
| 3015 | |
| 3016 | uchar* newNode(const int* idx, size_t hashval); |
| 3017 | void removeNode(size_t hidx, size_t nidx, size_t previdx); |
| 3018 | void resizeHashTab(size_t newsize); |
| 3019 | |
| 3020 | int flags; |
| 3021 | Hdr* hdr; |
| 3022 | }; |
| 3023 | |
| 3024 | |
| 3025 | |
| 3026 | ///////////////////////////////// SparseMat_<_Tp> //////////////////////////////////// |
| 3027 | |
| 3028 | /** @brief Template sparse n-dimensional array class derived from SparseMat |
| 3029 | |
| 3030 | SparseMat_ is a thin wrapper on top of SparseMat created in the same way as Mat_ . It simplifies |
| 3031 | notation of some operations: |
| 3032 | @code |
| 3033 | int sz[] = {10, 20, 30}; |
| 3034 | SparseMat_<double> M(3, sz); |
| 3035 | ... |
| 3036 | M.ref(1, 2, 3) = M(4, 5, 6) + M(7, 8, 9); |
| 3037 | @endcode |
| 3038 | */ |
| 3039 | template<typename _Tp> class SparseMat_ : public SparseMat |
| 3040 | { |
| 3041 | public: |
| 3042 | typedef SparseMatIterator_<_Tp> iterator; |
| 3043 | typedef SparseMatConstIterator_<_Tp> const_iterator; |
| 3044 | |
| 3045 | //! the default constructor |
| 3046 | SparseMat_(); |
| 3047 | //! the full constructor equivalent to SparseMat(dims, _sizes, DataType<_Tp>::type) |
| 3048 | SparseMat_(int dims, const int* _sizes); |
| 3049 | //! the copy constructor. If DataType<_Tp>.type != m.type(), the m elements are converted |
| 3050 | SparseMat_(const SparseMat& m); |
| 3051 | //! the copy constructor. This is O(1) operation - no data is copied |
| 3052 | SparseMat_(const SparseMat_& m); |
| 3053 | //! converts dense matrix to the sparse form |
| 3054 | SparseMat_(const Mat& m); |
| 3055 | //! converts the old-style sparse matrix to the C++ class. All the elements are copied |
| 3056 | //SparseMat_(const CvSparseMat* m); |
| 3057 | //! the assignment operator. If DataType<_Tp>.type != m.type(), the m elements are converted |
| 3058 | SparseMat_& operator = (const SparseMat& m); |
| 3059 | //! the assignment operator. This is O(1) operation - no data is copied |
| 3060 | SparseMat_& operator = (const SparseMat_& m); |
| 3061 | //! converts dense matrix to the sparse form |
| 3062 | SparseMat_& operator = (const Mat& m); |
| 3063 | |
| 3064 | //! makes full copy of the matrix. All the elements are duplicated |
| 3065 | CV_NODISCARD_STD SparseMat_ clone() const; |
| 3066 | //! equivalent to cv::SparseMat::create(dims, _sizes, DataType<_Tp>::type) |
| 3067 | void create(int dims, const int* _sizes); |
| 3068 | //! converts sparse matrix to the old-style CvSparseMat. All the elements are copied |
| 3069 | //operator CvSparseMat*() const; |
| 3070 | |
| 3071 | //! returns type of the matrix elements |
| 3072 | int type() const; |
| 3073 | //! returns depth of the matrix elements |
| 3074 | int depth() const; |
| 3075 | //! returns the number of channels in each matrix element |
| 3076 | int channels() const; |
| 3077 | |
| 3078 | //! equivalent to SparseMat::ref<_Tp>(i0, hashval) |
| 3079 | _Tp& ref(int i0, size_t* hashval=0); |
| 3080 | //! equivalent to SparseMat::ref<_Tp>(i0, i1, hashval) |
| 3081 | _Tp& ref(int i0, int i1, size_t* hashval=0); |
| 3082 | //! equivalent to SparseMat::ref<_Tp>(i0, i1, i2, hashval) |
| 3083 | _Tp& ref(int i0, int i1, int i2, size_t* hashval=0); |
| 3084 | //! equivalent to SparseMat::ref<_Tp>(idx, hashval) |
| 3085 | _Tp& ref(const int* idx, size_t* hashval=0); |
| 3086 | |
| 3087 | //! equivalent to SparseMat::value<_Tp>(i0, hashval) |
| 3088 | _Tp operator()(int i0, size_t* hashval=0) const; |
| 3089 | //! equivalent to SparseMat::value<_Tp>(i0, i1, hashval) |
| 3090 | _Tp operator()(int i0, int i1, size_t* hashval=0) const; |
| 3091 | //! equivalent to SparseMat::value<_Tp>(i0, i1, i2, hashval) |
| 3092 | _Tp operator()(int i0, int i1, int i2, size_t* hashval=0) const; |
| 3093 | //! equivalent to SparseMat::value<_Tp>(idx, hashval) |
| 3094 | _Tp operator()(const int* idx, size_t* hashval=0) const; |
| 3095 | |
| 3096 | //! returns sparse matrix iterator pointing to the first sparse matrix element |
| 3097 | SparseMatIterator_<_Tp> begin(); |
| 3098 | //! returns read-only sparse matrix iterator pointing to the first sparse matrix element |
| 3099 | SparseMatConstIterator_<_Tp> begin() const; |
| 3100 | //! returns sparse matrix iterator pointing to the element following the last sparse matrix element |
| 3101 | SparseMatIterator_<_Tp> end(); |
| 3102 | //! returns read-only sparse matrix iterator pointing to the element following the last sparse matrix element |
| 3103 | SparseMatConstIterator_<_Tp> end() const; |
| 3104 | }; |
| 3105 | |
| 3106 | |
| 3107 | |
| 3108 | ////////////////////////////////// MatConstIterator ////////////////////////////////// |
| 3109 | |
| 3110 | class CV_EXPORTS MatConstIterator |
| 3111 | { |
| 3112 | public: |
| 3113 | typedef uchar* value_type; |
| 3114 | typedef ptrdiff_t difference_type; |
| 3115 | typedef const uchar** pointer; |
| 3116 | typedef uchar* reference; |
| 3117 | |
| 3118 | typedef std::random_access_iterator_tag iterator_category; |
| 3119 | |
| 3120 | //! default constructor |
| 3121 | MatConstIterator(); |
| 3122 | //! constructor that sets the iterator to the beginning of the matrix |
| 3123 | MatConstIterator(const Mat* _m); |
| 3124 | //! constructor that sets the iterator to the specified element of the matrix |
| 3125 | MatConstIterator(const Mat* _m, int _row, int _col=0); |
| 3126 | //! constructor that sets the iterator to the specified element of the matrix |
| 3127 | MatConstIterator(const Mat* _m, Point _pt); |
| 3128 | //! constructor that sets the iterator to the specified element of the matrix |
| 3129 | MatConstIterator(const Mat* _m, const int* _idx); |
| 3130 | //! copy constructor |
| 3131 | MatConstIterator(const MatConstIterator& it); |
| 3132 | |
| 3133 | //! copy operator |
| 3134 | MatConstIterator& operator = (const MatConstIterator& it); |
| 3135 | //! returns the current matrix element |
| 3136 | const uchar* operator *() const; |
| 3137 | //! returns the i-th matrix element, relative to the current |
| 3138 | const uchar* operator [](ptrdiff_t i) const; |
| 3139 | |
| 3140 | //! shifts the iterator forward by the specified number of elements |
| 3141 | MatConstIterator& operator += (ptrdiff_t ofs); |
| 3142 | //! shifts the iterator backward by the specified number of elements |
| 3143 | MatConstIterator& operator -= (ptrdiff_t ofs); |
| 3144 | //! decrements the iterator |
| 3145 | MatConstIterator& operator --(); |
| 3146 | //! decrements the iterator |
| 3147 | MatConstIterator operator --(int); |
| 3148 | //! increments the iterator |
| 3149 | MatConstIterator& operator ++(); |
| 3150 | //! increments the iterator |
| 3151 | MatConstIterator operator ++(int); |
| 3152 | //! returns the current iterator position |
| 3153 | Point pos() const; |
| 3154 | //! returns the current iterator position |
| 3155 | void pos(int* _idx) const; |
| 3156 | |
| 3157 | ptrdiff_t lpos() const; |
| 3158 | void seek(ptrdiff_t ofs, bool relative = false); |
| 3159 | void seek(const int* _idx, bool relative = false); |
| 3160 | |
| 3161 | const Mat* m; |
| 3162 | size_t elemSize; |
| 3163 | const uchar* ptr; |
| 3164 | const uchar* sliceStart; |
| 3165 | const uchar* sliceEnd; |
| 3166 | }; |
| 3167 | |
| 3168 | |
| 3169 | |
| 3170 | ////////////////////////////////// MatConstIterator_ ///////////////////////////////// |
| 3171 | |
| 3172 | /** @brief Matrix read-only iterator |
| 3173 | */ |
| 3174 | template<typename _Tp> |
| 3175 | class MatConstIterator_ : public MatConstIterator |
| 3176 | { |
| 3177 | public: |
| 3178 | typedef _Tp value_type; |
| 3179 | typedef ptrdiff_t difference_type; |
| 3180 | typedef const _Tp* pointer; |
| 3181 | typedef const _Tp& reference; |
| 3182 | |
| 3183 | typedef std::random_access_iterator_tag iterator_category; |
| 3184 | |
| 3185 | //! default constructor |
| 3186 | MatConstIterator_(); |
| 3187 | //! constructor that sets the iterator to the beginning of the matrix |
| 3188 | MatConstIterator_(const Mat_<_Tp>* _m); |
| 3189 | //! constructor that sets the iterator to the specified element of the matrix |
| 3190 | MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col=0); |
| 3191 | //! constructor that sets the iterator to the specified element of the matrix |
| 3192 | MatConstIterator_(const Mat_<_Tp>* _m, Point _pt); |
| 3193 | //! constructor that sets the iterator to the specified element of the matrix |
| 3194 | MatConstIterator_(const Mat_<_Tp>* _m, const int* _idx); |
| 3195 | //! copy constructor |
| 3196 | MatConstIterator_(const MatConstIterator_& it); |
| 3197 | |
| 3198 | //! copy operator |
| 3199 | MatConstIterator_& operator = (const MatConstIterator_& it); |
| 3200 | //! returns the current matrix element |
| 3201 | const _Tp& operator *() const; |
| 3202 | //! returns the i-th matrix element, relative to the current |
| 3203 | const _Tp& operator [](ptrdiff_t i) const; |
| 3204 | |
| 3205 | //! shifts the iterator forward by the specified number of elements |
| 3206 | MatConstIterator_& operator += (ptrdiff_t ofs); |
| 3207 | //! shifts the iterator backward by the specified number of elements |
| 3208 | MatConstIterator_& operator -= (ptrdiff_t ofs); |
| 3209 | //! decrements the iterator |
| 3210 | MatConstIterator_& operator --(); |
| 3211 | //! decrements the iterator |
| 3212 | MatConstIterator_ operator --(int); |
| 3213 | //! increments the iterator |
| 3214 | MatConstIterator_& operator ++(); |
| 3215 | //! increments the iterator |
| 3216 | MatConstIterator_ operator ++(int); |
| 3217 | //! returns the current iterator position |
| 3218 | Point pos() const; |
| 3219 | }; |
| 3220 | |
| 3221 | |
| 3222 | |
| 3223 | //////////////////////////////////// MatIterator_ //////////////////////////////////// |
| 3224 | |
| 3225 | /** @brief Matrix read-write iterator |
| 3226 | */ |
| 3227 | template<typename _Tp> |
| 3228 | class MatIterator_ : public MatConstIterator_<_Tp> |
| 3229 | { |
| 3230 | public: |
| 3231 | typedef _Tp* pointer; |
| 3232 | typedef _Tp& reference; |
| 3233 | |
| 3234 | typedef std::random_access_iterator_tag iterator_category; |
| 3235 | |
| 3236 | //! the default constructor |
| 3237 | MatIterator_(); |
| 3238 | //! constructor that sets the iterator to the beginning of the matrix |
| 3239 | MatIterator_(Mat_<_Tp>* _m); |
| 3240 | //! constructor that sets the iterator to the specified element of the matrix |
| 3241 | MatIterator_(Mat_<_Tp>* _m, int _row, int _col=0); |
| 3242 | //! constructor that sets the iterator to the specified element of the matrix |
| 3243 | MatIterator_(Mat_<_Tp>* _m, Point _pt); |
| 3244 | //! constructor that sets the iterator to the specified element of the matrix |
| 3245 | MatIterator_(Mat_<_Tp>* _m, const int* _idx); |
| 3246 | //! copy constructor |
| 3247 | MatIterator_(const MatIterator_& it); |
| 3248 | //! copy operator |
| 3249 | MatIterator_& operator = (const MatIterator_<_Tp>& it ); |
| 3250 | |
| 3251 | //! returns the current matrix element |
| 3252 | _Tp& operator *() const; |
| 3253 | //! returns the i-th matrix element, relative to the current |
| 3254 | _Tp& operator [](ptrdiff_t i) const; |
| 3255 | |
| 3256 | //! shifts the iterator forward by the specified number of elements |
| 3257 | MatIterator_& operator += (ptrdiff_t ofs); |
| 3258 | //! shifts the iterator backward by the specified number of elements |
| 3259 | MatIterator_& operator -= (ptrdiff_t ofs); |
| 3260 | //! decrements the iterator |
| 3261 | MatIterator_& operator --(); |
| 3262 | //! decrements the iterator |
| 3263 | MatIterator_ operator --(int); |
| 3264 | //! increments the iterator |
| 3265 | MatIterator_& operator ++(); |
| 3266 | //! increments the iterator |
| 3267 | MatIterator_ operator ++(int); |
| 3268 | }; |
| 3269 | |
| 3270 | |
| 3271 | |
| 3272 | /////////////////////////////// SparseMatConstIterator /////////////////////////////// |
| 3273 | |
| 3274 | /** @brief Read-Only Sparse Matrix Iterator. |
| 3275 | |
| 3276 | Here is how to use the iterator to compute the sum of floating-point sparse matrix elements: |
| 3277 | |
| 3278 | \code |
| 3279 | SparseMatConstIterator it = m.begin(), it_end = m.end(); |
| 3280 | double s = 0; |
| 3281 | CV_Assert( m.type() == CV_32F ); |
| 3282 | for( ; it != it_end; ++it ) |
| 3283 | s += it.value<float>(); |
| 3284 | \endcode |
| 3285 | */ |
| 3286 | class CV_EXPORTS SparseMatConstIterator |
| 3287 | { |
| 3288 | public: |
| 3289 | //! the default constructor |
| 3290 | SparseMatConstIterator(); |
| 3291 | //! the full constructor setting the iterator to the first sparse matrix element |
| 3292 | SparseMatConstIterator(const SparseMat* _m); |
| 3293 | //! the copy constructor |
| 3294 | SparseMatConstIterator(const SparseMatConstIterator& it); |
| 3295 | |
| 3296 | //! the assignment operator |
| 3297 | SparseMatConstIterator& operator = (const SparseMatConstIterator& it); |
| 3298 | |
| 3299 | //! template method returning the current matrix element |
| 3300 | template<typename _Tp> const _Tp& value() const; |
| 3301 | //! returns the current node of the sparse matrix. it.node->idx is the current element index |
| 3302 | const SparseMat::Node* node() const; |
| 3303 | |
| 3304 | //! moves iterator to the previous element |
| 3305 | SparseMatConstIterator& operator --(); |
| 3306 | //! moves iterator to the previous element |
| 3307 | SparseMatConstIterator operator --(int); |
| 3308 | //! moves iterator to the next element |
| 3309 | SparseMatConstIterator& operator ++(); |
| 3310 | //! moves iterator to the next element |
| 3311 | SparseMatConstIterator operator ++(int); |
| 3312 | |
| 3313 | //! moves iterator to the element after the last element |
| 3314 | void seekEnd(); |
| 3315 | |
| 3316 | const SparseMat* m; |
| 3317 | size_t hashidx; |
| 3318 | uchar* ptr; |
| 3319 | }; |
| 3320 | |
| 3321 | |
| 3322 | |
| 3323 | ////////////////////////////////// SparseMatIterator ///////////////////////////////// |
| 3324 | |
| 3325 | /** @brief Read-write Sparse Matrix Iterator |
| 3326 | |
| 3327 | The class is similar to cv::SparseMatConstIterator, |
| 3328 | but can be used for in-place modification of the matrix elements. |
| 3329 | */ |
| 3330 | class CV_EXPORTS SparseMatIterator : public SparseMatConstIterator |
| 3331 | { |
| 3332 | public: |
| 3333 | //! the default constructor |
| 3334 | SparseMatIterator(); |
| 3335 | //! the full constructor setting the iterator to the first sparse matrix element |
| 3336 | SparseMatIterator(SparseMat* _m); |
| 3337 | //! the full constructor setting the iterator to the specified sparse matrix element |
| 3338 | SparseMatIterator(SparseMat* _m, const int* idx); |
| 3339 | //! the copy constructor |
| 3340 | SparseMatIterator(const SparseMatIterator& it); |
| 3341 | |
| 3342 | //! the assignment operator |
| 3343 | SparseMatIterator& operator = (const SparseMatIterator& it); |
| 3344 | //! returns read-write reference to the current sparse matrix element |
| 3345 | template<typename _Tp> _Tp& value() const; |
| 3346 | //! returns pointer to the current sparse matrix node. it.node->idx is the index of the current element (do not modify it!) |
| 3347 | SparseMat::Node* node() const; |
| 3348 | |
| 3349 | //! moves iterator to the next element |
| 3350 | SparseMatIterator& operator ++(); |
| 3351 | //! moves iterator to the next element |
| 3352 | SparseMatIterator operator ++(int); |
| 3353 | }; |
| 3354 | |
| 3355 | |
| 3356 | |
| 3357 | /////////////////////////////// SparseMatConstIterator_ ////////////////////////////// |
| 3358 | |
| 3359 | /** @brief Template Read-Only Sparse Matrix Iterator Class. |
| 3360 | |
| 3361 | This is the derived from SparseMatConstIterator class that |
| 3362 | introduces more convenient operator *() for accessing the current element. |
| 3363 | */ |
| 3364 | template<typename _Tp> class SparseMatConstIterator_ : public SparseMatConstIterator |
| 3365 | { |
| 3366 | public: |
| 3367 | |
| 3368 | typedef std::forward_iterator_tag iterator_category; |
| 3369 | |
| 3370 | //! the default constructor |
| 3371 | SparseMatConstIterator_(); |
| 3372 | //! the full constructor setting the iterator to the first sparse matrix element |
| 3373 | SparseMatConstIterator_(const SparseMat_<_Tp>* _m); |
| 3374 | SparseMatConstIterator_(const SparseMat* _m); |
| 3375 | //! the copy constructor |
| 3376 | SparseMatConstIterator_(const SparseMatConstIterator_& it); |
| 3377 | |
| 3378 | //! the assignment operator |
| 3379 | SparseMatConstIterator_& operator = (const SparseMatConstIterator_& it); |
| 3380 | //! the element access operator |
| 3381 | const _Tp& operator *() const; |
| 3382 | |
| 3383 | //! moves iterator to the next element |
| 3384 | SparseMatConstIterator_& operator ++(); |
| 3385 | //! moves iterator to the next element |
| 3386 | SparseMatConstIterator_ operator ++(int); |
| 3387 | }; |
| 3388 | |
| 3389 | |
| 3390 | |
| 3391 | ///////////////////////////////// SparseMatIterator_ ///////////////////////////////// |
| 3392 | |
| 3393 | /** @brief Template Read-Write Sparse Matrix Iterator Class. |
| 3394 | |
| 3395 | This is the derived from cv::SparseMatConstIterator_ class that |
| 3396 | introduces more convenient operator *() for accessing the current element. |
| 3397 | */ |
| 3398 | template<typename _Tp> class SparseMatIterator_ : public SparseMatConstIterator_<_Tp> |
| 3399 | { |
| 3400 | public: |
| 3401 | |
| 3402 | typedef std::forward_iterator_tag iterator_category; |
| 3403 | |
| 3404 | //! the default constructor |
| 3405 | SparseMatIterator_(); |
| 3406 | //! the full constructor setting the iterator to the first sparse matrix element |
| 3407 | SparseMatIterator_(SparseMat_<_Tp>* _m); |
| 3408 | SparseMatIterator_(SparseMat* _m); |
| 3409 | //! the copy constructor |
| 3410 | SparseMatIterator_(const SparseMatIterator_& it); |
| 3411 | |
| 3412 | //! the assignment operator |
| 3413 | SparseMatIterator_& operator = (const SparseMatIterator_& it); |
| 3414 | //! returns the reference to the current element |
| 3415 | _Tp& operator *() const; |
| 3416 | |
| 3417 | //! moves the iterator to the next element |
| 3418 | SparseMatIterator_& operator ++(); |
| 3419 | //! moves the iterator to the next element |
| 3420 | SparseMatIterator_ operator ++(int); |
| 3421 | }; |
| 3422 | |
| 3423 | |
| 3424 | |
| 3425 | /////////////////////////////////// NAryMatIterator ////////////////////////////////// |
| 3426 | |
| 3427 | /** @brief n-ary multi-dimensional array iterator. |
| 3428 | |
| 3429 | Use the class to implement unary, binary, and, generally, n-ary element-wise operations on |
| 3430 | multi-dimensional arrays. Some of the arguments of an n-ary function may be continuous arrays, some |
| 3431 | may be not. It is possible to use conventional MatIterator 's for each array but incrementing all of |
| 3432 | the iterators after each small operations may be a big overhead. In this case consider using |
| 3433 | NAryMatIterator to iterate through several matrices simultaneously as long as they have the same |
| 3434 | geometry (dimensionality and all the dimension sizes are the same). On each iteration `it.planes[0]`, |
| 3435 | `it.planes[1]`,... will be the slices of the corresponding matrices. |
| 3436 | |
| 3437 | The example below illustrates how you can compute a normalized and threshold 3D color histogram: |
| 3438 | @code |
| 3439 | void computeNormalizedColorHist(const Mat& image, Mat& hist, int N, double minProb) |
| 3440 | { |
| 3441 | const int histSize[] = {N, N, N}; |
| 3442 | |
| 3443 | // make sure that the histogram has a proper size and type |
| 3444 | hist.create(3, histSize, CV_32F); |
| 3445 | |
| 3446 | // and clear it |
| 3447 | hist = Scalar(0); |
| 3448 | |
| 3449 | // the loop below assumes that the image |
| 3450 | // is a 8-bit 3-channel. check it. |
| 3451 | CV_Assert(image.type() == CV_8UC3); |
| 3452 | MatConstIterator_<Vec3b> it = image.begin<Vec3b>(), |
| 3453 | it_end = image.end<Vec3b>(); |
| 3454 | for( ; it != it_end; ++it ) |
| 3455 | { |
| 3456 | const Vec3b& pix = *it; |
| 3457 | hist.at<float>(pix[0]*N/256, pix[1]*N/256, pix[2]*N/256) += 1.f; |
| 3458 | } |
| 3459 | |
| 3460 | minProb *= image.rows*image.cols; |
| 3461 | |
| 3462 | // initialize iterator (the style is different from STL). |
| 3463 | // after initialization the iterator will contain |
| 3464 | // the number of slices or planes the iterator will go through. |
| 3465 | // it simultaneously increments iterators for several matrices |
| 3466 | // supplied as a null terminated list of pointers |
| 3467 | const Mat* arrays[] = {&hist, 0}; |
| 3468 | Mat planes[1]; |
| 3469 | NAryMatIterator itNAry(arrays, planes, 1); |
| 3470 | double s = 0; |
| 3471 | // iterate through the matrix. on each iteration |
| 3472 | // itNAry.planes[i] (of type Mat) will be set to the current plane |
| 3473 | // of the i-th n-dim matrix passed to the iterator constructor. |
| 3474 | for(int p = 0; p < itNAry.nplanes; p++, ++itNAry) |
| 3475 | { |
| 3476 | threshold(itNAry.planes[0], itNAry.planes[0], minProb, 0, THRESH_TOZERO); |
| 3477 | s += sum(itNAry.planes[0])[0]; |
| 3478 | } |
| 3479 | |
| 3480 | s = 1./s; |
| 3481 | itNAry = NAryMatIterator(arrays, planes, 1); |
| 3482 | for(int p = 0; p < itNAry.nplanes; p++, ++itNAry) |
| 3483 | itNAry.planes[0] *= s; |
| 3484 | } |
| 3485 | @endcode |
| 3486 | */ |
| 3487 | class CV_EXPORTS NAryMatIterator |
| 3488 | { |
| 3489 | public: |
| 3490 | //! the default constructor |
| 3491 | NAryMatIterator(); |
| 3492 | //! the full constructor taking arbitrary number of n-dim matrices |
| 3493 | NAryMatIterator(const Mat** arrays, uchar** ptrs, int narrays=-1); |
| 3494 | //! the full constructor taking arbitrary number of n-dim matrices |
| 3495 | NAryMatIterator(const Mat** arrays, Mat* planes, int narrays=-1); |
| 3496 | //! the separate iterator initialization method |
| 3497 | void init(const Mat** arrays, Mat* planes, uchar** ptrs, int narrays=-1); |
| 3498 | |
| 3499 | //! proceeds to the next plane of every iterated matrix |
| 3500 | NAryMatIterator& operator ++(); |
| 3501 | //! proceeds to the next plane of every iterated matrix (postfix increment operator) |
| 3502 | NAryMatIterator operator ++(int); |
| 3503 | |
| 3504 | //! the iterated arrays |
| 3505 | const Mat** arrays; |
| 3506 | //! the current planes |
| 3507 | Mat* planes; |
| 3508 | //! data pointers |
| 3509 | uchar** ptrs; |
| 3510 | //! the number of arrays |
| 3511 | int narrays; |
| 3512 | //! the number of hyper-planes that the iterator steps through |
| 3513 | size_t nplanes; |
| 3514 | //! the size of each segment (in elements) |
| 3515 | size_t size; |
| 3516 | protected: |
| 3517 | int iterdepth; |
| 3518 | size_t idx; |
| 3519 | }; |
| 3520 | |
| 3521 | |
| 3522 | |
| 3523 | ///////////////////////////////// Matrix Expressions ///////////////////////////////// |
| 3524 | |
| 3525 | class CV_EXPORTS MatOp |
| 3526 | { |
| 3527 | public: |
| 3528 | MatOp(); |
| 3529 | virtual ~MatOp(); |
| 3530 | |
| 3531 | virtual bool elementWise(const MatExpr& expr) const; |
| 3532 | virtual void assign(const MatExpr& expr, Mat& m, int type=-1) const = 0; |
| 3533 | virtual void roi(const MatExpr& expr, const Range& rowRange, |
| 3534 | const Range& colRange, MatExpr& res) const; |
| 3535 | virtual void diag(const MatExpr& expr, int d, MatExpr& res) const; |
| 3536 | virtual void augAssignAdd(const MatExpr& expr, Mat& m) const; |
| 3537 | virtual void augAssignSubtract(const MatExpr& expr, Mat& m) const; |
| 3538 | virtual void augAssignMultiply(const MatExpr& expr, Mat& m) const; |
| 3539 | virtual void augAssignDivide(const MatExpr& expr, Mat& m) const; |
| 3540 | virtual void augAssignAnd(const MatExpr& expr, Mat& m) const; |
| 3541 | virtual void augAssignOr(const MatExpr& expr, Mat& m) const; |
| 3542 | virtual void augAssignXor(const MatExpr& expr, Mat& m) const; |
| 3543 | |
| 3544 | virtual void add(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const; |
| 3545 | virtual void add(const MatExpr& expr1, const Scalar& s, MatExpr& res) const; |
| 3546 | |
| 3547 | virtual void subtract(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const; |
| 3548 | virtual void subtract(const Scalar& s, const MatExpr& expr, MatExpr& res) const; |
| 3549 | |
| 3550 | virtual void multiply(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=1) const; |
| 3551 | virtual void multiply(const MatExpr& expr1, double s, MatExpr& res) const; |
| 3552 | |
| 3553 | virtual void divide(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=1) const; |
| 3554 | virtual void divide(double s, const MatExpr& expr, MatExpr& res) const; |
| 3555 | |
| 3556 | virtual void abs(const MatExpr& expr, MatExpr& res) const; |
| 3557 | |
| 3558 | virtual void transpose(const MatExpr& expr, MatExpr& res) const; |
| 3559 | virtual void matmul(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const; |
| 3560 | virtual void invert(const MatExpr& expr, int method, MatExpr& res) const; |
| 3561 | |
| 3562 | virtual Size size(const MatExpr& expr) const; |
| 3563 | virtual int type(const MatExpr& expr) const; |
| 3564 | }; |
| 3565 | |
| 3566 | /** @brief Matrix expression representation |
| 3567 | @anchor MatrixExpressions |
| 3568 | This is a list of implemented matrix operations that can be combined in arbitrary complex |
| 3569 | expressions (here A, B stand for matrices ( cv::Mat ), s for a cv::Scalar, alpha for a |
| 3570 | real-valued scalar ( double )): |
| 3571 | - Addition, subtraction, negation: `A+B`, `A-B`, `A+s`, `A-s`, `s+A`, `s-A`, `-A` |
| 3572 | - Scaling: `A*alpha` |
| 3573 | - Per-element multiplication and division: `A.mul(B)`, `A/B`, `alpha/A` |
| 3574 | - Matrix multiplication: `A*B` |
| 3575 | - Transposition: `A.t()` (means A<sup>T</sup>) |
| 3576 | - Matrix inversion and pseudo-inversion, solving linear systems and least-squares problems: |
| 3577 | `A.inv([method]) (~ A<sup>-1</sup>)`, `A.inv([method])*B (~ X: AX=B)` |
| 3578 | - Comparison: `A cmpop B`, `A cmpop alpha`, `alpha cmpop A`, where *cmpop* is one of |
| 3579 | `>`, `>=`, `==`, `!=`, `<=`, `<`. The result of comparison is an 8-bit single channel mask whose |
| 3580 | elements are set to 255 (if the particular element or pair of elements satisfy the condition) or |
| 3581 | 0. |
| 3582 | - Bitwise logical operations: `A logicop B`, `A logicop s`, `s logicop A`, `~A`, where *logicop* is one of |
| 3583 | `&`, `|`, `^`. |
| 3584 | - Element-wise minimum and maximum: cv::min(A, B), cv::min(A, alpha), cv::max(A, B), cv::max(A, alpha) |
| 3585 | - Element-wise absolute value: cv::abs(A) |
| 3586 | - Cross-product, dot-product: `A.cross(B)`, `A.dot(B)` |
| 3587 | - Any function of matrix or matrices and scalars that returns a matrix or a scalar, such as cv::norm, |
| 3588 | cv::mean, cv::sum, cv::countNonZero, cv::trace, cv::determinant, cv::repeat, and others. |
| 3589 | - Matrix initializers ( Mat::eye(), Mat::zeros(), Mat::ones() ), matrix comma-separated |
| 3590 | initializers, matrix constructors and operators that extract sub-matrices (see cv::Mat description). |
| 3591 | - Mat_<destination_type>() constructors to cast the result to the proper type. |
| 3592 | @note Comma-separated initializers and probably some other operations may require additional |
| 3593 | explicit Mat() or Mat_<T>() constructor calls to resolve a possible ambiguity. |
| 3594 | |
| 3595 | Here are examples of matrix expressions: |
| 3596 | @code |
| 3597 | // compute pseudo-inverse of A, equivalent to A.inv(DECOMP_SVD) |
| 3598 | SVD svd(A); |
| 3599 | Mat pinvA = svd.vt.t()*Mat::diag(1./svd.w)*svd.u.t(); |
| 3600 | |
| 3601 | // compute the new vector of parameters in the Levenberg-Marquardt algorithm |
| 3602 | x -= (A.t()*A + lambda*Mat::eye(A.cols,A.cols,A.type())).inv(DECOMP_CHOLESKY)*(A.t()*err); |
| 3603 | |
| 3604 | // sharpen image using "unsharp mask" algorithm |
| 3605 | Mat blurred; double sigma = 1, threshold = 5, amount = 1; |
| 3606 | GaussianBlur(img, blurred, Size(), sigma, sigma); |
| 3607 | Mat lowContrastMask = abs(img - blurred) < threshold; |
| 3608 | Mat sharpened = img*(1+amount) + blurred*(-amount); |
| 3609 | img.copyTo(sharpened, lowContrastMask); |
| 3610 | @endcode |
| 3611 | */ |
| 3612 | class CV_EXPORTS MatExpr |
| 3613 | { |
| 3614 | public: |
| 3615 | MatExpr(); |
| 3616 | explicit MatExpr(const Mat& m); |
| 3617 | |
| 3618 | MatExpr(const MatOp* _op, int _flags, const Mat& _a = Mat(), const Mat& _b = Mat(), |
| 3619 | const Mat& _c = Mat(), double _alpha = 1, double _beta = 1, const Scalar& _s = Scalar()); |
| 3620 | |
| 3621 | operator Mat() const; |
| 3622 | template<typename _Tp> operator Mat_<_Tp>() const; |
| 3623 | |
| 3624 | Size size() const; |
| 3625 | int type() const; |
| 3626 | |
| 3627 | MatExpr row(int y) const; |
| 3628 | MatExpr col(int x) const; |
| 3629 | MatExpr diag(int d = 0) const; |
| 3630 | MatExpr operator()( const Range& rowRange, const Range& colRange ) const; |
| 3631 | MatExpr operator()( const Rect& roi ) const; |
| 3632 | |
| 3633 | MatExpr t() const; |
| 3634 | MatExpr inv(int method = DECOMP_LU) const; |
| 3635 | MatExpr mul(const MatExpr& e, double scale=1) const; |
| 3636 | MatExpr mul(const Mat& m, double scale=1) const; |
| 3637 | |
| 3638 | Mat cross(const Mat& m) const; |
| 3639 | double dot(const Mat& m) const; |
| 3640 | |
| 3641 | void swap(MatExpr& b); |
| 3642 | |
| 3643 | const MatOp* op; |
| 3644 | int flags; |
| 3645 | |
| 3646 | Mat a, b, c; |
| 3647 | double alpha, beta; |
| 3648 | Scalar s; |
| 3649 | }; |
| 3650 | |
| 3651 | //! @} core_basic |
| 3652 | |
| 3653 | //! @relates cv::MatExpr |
| 3654 | //! @{ |
| 3655 | CV_EXPORTS MatExpr operator + (const Mat& a, const Mat& b); |
| 3656 | CV_EXPORTS MatExpr operator + (const Mat& a, const Scalar& s); |
| 3657 | CV_EXPORTS MatExpr operator + (const Scalar& s, const Mat& a); |
| 3658 | CV_EXPORTS MatExpr operator + (const MatExpr& e, const Mat& m); |
| 3659 | CV_EXPORTS MatExpr operator + (const Mat& m, const MatExpr& e); |
| 3660 | CV_EXPORTS MatExpr operator + (const MatExpr& e, const Scalar& s); |
| 3661 | CV_EXPORTS MatExpr operator + (const Scalar& s, const MatExpr& e); |
| 3662 | CV_EXPORTS MatExpr operator + (const MatExpr& e1, const MatExpr& e2); |
| 3663 | template<typename _Tp, int m, int n> static inline |
| 3664 | MatExpr operator + (const Mat& a, const Matx<_Tp, m, n>& b) { return a + Mat(b); } |
| 3665 | template<typename _Tp, int m, int n> static inline |
| 3666 | MatExpr operator + (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) + b; } |
| 3667 | |
| 3668 | CV_EXPORTS MatExpr operator - (const Mat& a, const Mat& b); |
| 3669 | CV_EXPORTS MatExpr operator - (const Mat& a, const Scalar& s); |
| 3670 | CV_EXPORTS MatExpr operator - (const Scalar& s, const Mat& a); |
| 3671 | CV_EXPORTS MatExpr operator - (const MatExpr& e, const Mat& m); |
| 3672 | CV_EXPORTS MatExpr operator - (const Mat& m, const MatExpr& e); |
| 3673 | CV_EXPORTS MatExpr operator - (const MatExpr& e, const Scalar& s); |
| 3674 | CV_EXPORTS MatExpr operator - (const Scalar& s, const MatExpr& e); |
| 3675 | CV_EXPORTS MatExpr operator - (const MatExpr& e1, const MatExpr& e2); |
| 3676 | template<typename _Tp, int m, int n> static inline |
| 3677 | MatExpr operator - (const Mat& a, const Matx<_Tp, m, n>& b) { return a - Mat(b); } |
| 3678 | template<typename _Tp, int m, int n> static inline |
| 3679 | MatExpr operator - (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) - b; } |
| 3680 | |
| 3681 | CV_EXPORTS MatExpr operator - (const Mat& m); |
| 3682 | CV_EXPORTS MatExpr operator - (const MatExpr& e); |
| 3683 | |
| 3684 | CV_EXPORTS MatExpr operator * (const Mat& a, const Mat& b); |
| 3685 | CV_EXPORTS MatExpr operator * (const Mat& a, double s); |
| 3686 | CV_EXPORTS MatExpr operator * (double s, const Mat& a); |
| 3687 | CV_EXPORTS MatExpr operator * (const MatExpr& e, const Mat& m); |
| 3688 | CV_EXPORTS MatExpr operator * (const Mat& m, const MatExpr& e); |
| 3689 | CV_EXPORTS MatExpr operator * (const MatExpr& e, double s); |
| 3690 | CV_EXPORTS MatExpr operator * (double s, const MatExpr& e); |
| 3691 | CV_EXPORTS MatExpr operator * (const MatExpr& e1, const MatExpr& e2); |
| 3692 | template<typename _Tp, int m, int n> static inline |
| 3693 | MatExpr operator * (const Mat& a, const Matx<_Tp, m, n>& b) { return a * Mat(b); } |
| 3694 | template<typename _Tp, int m, int n> static inline |
| 3695 | MatExpr operator * (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) * b; } |
| 3696 | |
| 3697 | CV_EXPORTS MatExpr operator / (const Mat& a, const Mat& b); |
| 3698 | CV_EXPORTS MatExpr operator / (const Mat& a, double s); |
| 3699 | CV_EXPORTS MatExpr operator / (double s, const Mat& a); |
| 3700 | CV_EXPORTS MatExpr operator / (const MatExpr& e, const Mat& m); |
| 3701 | CV_EXPORTS MatExpr operator / (const Mat& m, const MatExpr& e); |
| 3702 | CV_EXPORTS MatExpr operator / (const MatExpr& e, double s); |
| 3703 | CV_EXPORTS MatExpr operator / (double s, const MatExpr& e); |
| 3704 | CV_EXPORTS MatExpr operator / (const MatExpr& e1, const MatExpr& e2); |
| 3705 | template<typename _Tp, int m, int n> static inline |
| 3706 | MatExpr operator / (const Mat& a, const Matx<_Tp, m, n>& b) { return a / Mat(b); } |
| 3707 | template<typename _Tp, int m, int n> static inline |
| 3708 | MatExpr operator / (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) / b; } |
| 3709 | |
| 3710 | CV_EXPORTS MatExpr operator < (const Mat& a, const Mat& b); |
| 3711 | CV_EXPORTS MatExpr operator < (const Mat& a, double s); |
| 3712 | CV_EXPORTS MatExpr operator < (double s, const Mat& a); |
| 3713 | template<typename _Tp, int m, int n> static inline |
| 3714 | MatExpr operator < (const Mat& a, const Matx<_Tp, m, n>& b) { return a < Mat(b); } |
| 3715 | template<typename _Tp, int m, int n> static inline |
| 3716 | MatExpr operator < (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) < b; } |
| 3717 | |
| 3718 | CV_EXPORTS MatExpr operator <= (const Mat& a, const Mat& b); |
| 3719 | CV_EXPORTS MatExpr operator <= (const Mat& a, double s); |
| 3720 | CV_EXPORTS MatExpr operator <= (double s, const Mat& a); |
| 3721 | template<typename _Tp, int m, int n> static inline |
| 3722 | MatExpr operator <= (const Mat& a, const Matx<_Tp, m, n>& b) { return a <= Mat(b); } |
| 3723 | template<typename _Tp, int m, int n> static inline |
| 3724 | MatExpr operator <= (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) <= b; } |
| 3725 | |
| 3726 | CV_EXPORTS MatExpr operator == (const Mat& a, const Mat& b); |
| 3727 | CV_EXPORTS MatExpr operator == (const Mat& a, double s); |
| 3728 | CV_EXPORTS MatExpr operator == (double s, const Mat& a); |
| 3729 | template<typename _Tp, int m, int n> static inline |
| 3730 | MatExpr operator == (const Mat& a, const Matx<_Tp, m, n>& b) { return a == Mat(b); } |
| 3731 | template<typename _Tp, int m, int n> static inline |
| 3732 | MatExpr operator == (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) == b; } |
| 3733 | |
| 3734 | CV_EXPORTS MatExpr operator != (const Mat& a, const Mat& b); |
| 3735 | CV_EXPORTS MatExpr operator != (const Mat& a, double s); |
| 3736 | CV_EXPORTS MatExpr operator != (double s, const Mat& a); |
| 3737 | template<typename _Tp, int m, int n> static inline |
| 3738 | MatExpr operator != (const Mat& a, const Matx<_Tp, m, n>& b) { return a != Mat(b); } |
| 3739 | template<typename _Tp, int m, int n> static inline |
| 3740 | MatExpr operator != (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) != b; } |
| 3741 | |
| 3742 | CV_EXPORTS MatExpr operator >= (const Mat& a, const Mat& b); |
| 3743 | CV_EXPORTS MatExpr operator >= (const Mat& a, double s); |
| 3744 | CV_EXPORTS MatExpr operator >= (double s, const Mat& a); |
| 3745 | template<typename _Tp, int m, int n> static inline |
| 3746 | MatExpr operator >= (const Mat& a, const Matx<_Tp, m, n>& b) { return a >= Mat(b); } |
| 3747 | template<typename _Tp, int m, int n> static inline |
| 3748 | MatExpr operator >= (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) >= b; } |
| 3749 | |
| 3750 | CV_EXPORTS MatExpr operator > (const Mat& a, const Mat& b); |
| 3751 | CV_EXPORTS MatExpr operator > (const Mat& a, double s); |
| 3752 | CV_EXPORTS MatExpr operator > (double s, const Mat& a); |
| 3753 | template<typename _Tp, int m, int n> static inline |
| 3754 | MatExpr operator > (const Mat& a, const Matx<_Tp, m, n>& b) { return a > Mat(b); } |
| 3755 | template<typename _Tp, int m, int n> static inline |
| 3756 | MatExpr operator > (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) > b; } |
| 3757 | |
| 3758 | CV_EXPORTS MatExpr operator & (const Mat& a, const Mat& b); |
| 3759 | CV_EXPORTS MatExpr operator & (const Mat& a, const Scalar& s); |
| 3760 | CV_EXPORTS MatExpr operator & (const Scalar& s, const Mat& a); |
| 3761 | template<typename _Tp, int m, int n> static inline |
| 3762 | MatExpr operator & (const Mat& a, const Matx<_Tp, m, n>& b) { return a & Mat(b); } |
| 3763 | template<typename _Tp, int m, int n> static inline |
| 3764 | MatExpr operator & (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) & b; } |
| 3765 | |
| 3766 | CV_EXPORTS MatExpr operator | (const Mat& a, const Mat& b); |
| 3767 | CV_EXPORTS MatExpr operator | (const Mat& a, const Scalar& s); |
| 3768 | CV_EXPORTS MatExpr operator | (const Scalar& s, const Mat& a); |
| 3769 | template<typename _Tp, int m, int n> static inline |
| 3770 | MatExpr operator | (const Mat& a, const Matx<_Tp, m, n>& b) { return a | Mat(b); } |
| 3771 | template<typename _Tp, int m, int n> static inline |
| 3772 | MatExpr operator | (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) | b; } |
| 3773 | |
| 3774 | CV_EXPORTS MatExpr operator ^ (const Mat& a, const Mat& b); |
| 3775 | CV_EXPORTS MatExpr operator ^ (const Mat& a, const Scalar& s); |
| 3776 | CV_EXPORTS MatExpr operator ^ (const Scalar& s, const Mat& a); |
| 3777 | template<typename _Tp, int m, int n> static inline |
| 3778 | MatExpr operator ^ (const Mat& a, const Matx<_Tp, m, n>& b) { return a ^ Mat(b); } |
| 3779 | template<typename _Tp, int m, int n> static inline |
| 3780 | MatExpr operator ^ (const Matx<_Tp, m, n>& a, const Mat& b) { return Mat(a) ^ b; } |
| 3781 | |
| 3782 | CV_EXPORTS MatExpr operator ~(const Mat& m); |
| 3783 | |
| 3784 | CV_EXPORTS MatExpr min(const Mat& a, const Mat& b); |
| 3785 | CV_EXPORTS MatExpr min(const Mat& a, double s); |
| 3786 | CV_EXPORTS MatExpr min(double s, const Mat& a); |
| 3787 | template<typename _Tp, int m, int n> static inline |
| 3788 | MatExpr min (const Mat& a, const Matx<_Tp, m, n>& b) { return min(a, b: Mat(b)); } |
| 3789 | template<typename _Tp, int m, int n> static inline |
| 3790 | MatExpr min (const Matx<_Tp, m, n>& a, const Mat& b) { return min(a: Mat(a), b); } |
| 3791 | |
| 3792 | CV_EXPORTS MatExpr max(const Mat& a, const Mat& b); |
| 3793 | CV_EXPORTS MatExpr max(const Mat& a, double s); |
| 3794 | CV_EXPORTS MatExpr max(double s, const Mat& a); |
| 3795 | template<typename _Tp, int m, int n> static inline |
| 3796 | MatExpr max (const Mat& a, const Matx<_Tp, m, n>& b) { return max(a, b: Mat(b)); } |
| 3797 | template<typename _Tp, int m, int n> static inline |
| 3798 | MatExpr max (const Matx<_Tp, m, n>& a, const Mat& b) { return max(a: Mat(a), b); } |
| 3799 | |
| 3800 | /** @brief Calculates an absolute value of each matrix element. |
| 3801 | |
| 3802 | abs is a meta-function that is expanded to one of absdiff or convertScaleAbs forms: |
| 3803 | - C = abs(A-B) is equivalent to `absdiff(A, B, C)` |
| 3804 | - C = abs(A) is equivalent to `absdiff(A, Scalar::all(0), C)` |
| 3805 | - C = `Mat_<Vec<uchar,n> >(abs(A*alpha + beta))` is equivalent to `convertScaleAbs(A, C, alpha, |
| 3806 | beta)` |
| 3807 | |
| 3808 | The output matrix has the same size and the same type as the input one except for the last case, |
| 3809 | where C is depth=CV_8U . |
| 3810 | @param m matrix. |
| 3811 | @sa @ref MatrixExpressions, absdiff, convertScaleAbs |
| 3812 | */ |
| 3813 | CV_EXPORTS MatExpr abs(const Mat& m); |
| 3814 | /** @overload |
| 3815 | @param e matrix expression. |
| 3816 | */ |
| 3817 | CV_EXPORTS MatExpr abs(const MatExpr& e); |
| 3818 | //! @} relates cv::MatExpr |
| 3819 | |
| 3820 | } // cv |
| 3821 | |
| 3822 | #include "opencv2/core/mat.inl.hpp" |
| 3823 | |
| 3824 | #endif // OPENCV_CORE_MAT_HPP |
| 3825 | |