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