matrix_operations.hpp
Go to the documentation of this file.
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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., 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_OCL_MATRIX_OPERATIONS_HPP__
45 #define __OPENCV_OCL_MATRIX_OPERATIONS_HPP__
46 
47 #include "opencv2/ocl/ocl.hpp"
48 
49 namespace cv
50 {
51 
52  namespace ocl
53  {
54 
55  enum
56  {
57  MAT_ADD = 1,
65  };
66 
67  class CV_EXPORTS oclMatExpr
68  {
69  public:
70  oclMatExpr() : a(oclMat()), b(oclMat()), op(0) {}
71  oclMatExpr(const oclMat& _a, const oclMat& _b, int _op)
72  : a(_a), b(_b), op(_op) {}
73  operator oclMat() const;
74  void assign(oclMat& m) const;
75 
76  protected:
78  int op;
79  };
83 
84  inline oclMat::oclMat() : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0) {}
85 
86  inline oclMat::oclMat(int _rows, int _cols, int _type) : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
87  {
88  if( _rows > 0 && _cols > 0 )
89  create( _rows, _cols, _type );
90  }
91 
92  inline oclMat::oclMat(Size _size, int _type) : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
93  {
94  if( _size.height > 0 && _size.width > 0 )
95  create( _size.height, _size.width, _type );
96  }
97 
98  inline oclMat::oclMat(int _rows, int _cols, int _type, const Scalar &_s)
99  : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
100  {
101  if(_rows > 0 && _cols > 0)
102  {
103  create(_rows, _cols, _type);
104  *this = _s;
105  }
106  }
107 
108  inline oclMat::oclMat(Size _size, int _type, const Scalar &_s)
109  : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
110  {
111  if( _size.height > 0 && _size.width > 0 )
112  {
113  create( _size.height, _size.width, _type );
114  *this = _s;
115  }
116  }
117 
118  inline oclMat::oclMat(const oclMat &m)
119  : flags(m.flags), rows(m.rows), cols(m.cols), step(m.step), data(m.data),
120  refcount(m.refcount), datastart(m.datastart), dataend(m.dataend), clCxt(m.clCxt), offset(m.offset), wholerows(m.wholerows), wholecols(m.wholecols)
121  {
122  if( refcount )
123  CV_XADD(refcount, 1);
124  }
125 
126  inline oclMat::oclMat(int _rows, int _cols, int _type, void *_data, size_t _step)
127  : flags(0), rows(0), cols(0), step(0), data(0), refcount(0),
128  datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
129  {
130  cv::Mat m(_rows, _cols, _type, _data, _step);
131  upload(m);
132  //size_t minstep = cols * elemSize();
133  //if( step == Mat::AUTO_STEP )
134  //{
135  // step = minstep;
136  // flags |= Mat::CONTINUOUS_FLAG;
137  //}
138  //else
139  //{
140  // if( rows == 1 ) step = minstep;
141  // CV_DbgAssert( step >= minstep );
142  // flags |= step == minstep ? Mat::CONTINUOUS_FLAG : 0;
143  //}
144  //dataend += step * (rows - 1) + minstep;
145  }
146 
147  inline oclMat::oclMat(Size _size, int _type, void *_data, size_t _step)
148  : flags(0), rows(0), cols(0),
149  step(0), data(0), refcount(0),
150  datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
151  {
152  cv::Mat m(_size, _type, _data, _step);
153  upload(m);
154  //size_t minstep = cols * elemSize();
155  //if( step == Mat::AUTO_STEP )
156  //{
157  // step = minstep;
158  // flags |= Mat::CONTINUOUS_FLAG;
159  //}
160  //else
161  //{
162  // if( rows == 1 ) step = minstep;
163  // CV_DbgAssert( step >= minstep );
164  // flags |= step == minstep ? Mat::CONTINUOUS_FLAG : 0;
165  //}
166  //dataend += step * (rows - 1) + minstep;
167  }
168 
169 
170  inline oclMat::oclMat(const oclMat &m, const Range &rRange, const Range &cRange)
171  {
172  flags = m.flags;
173  step = m.step;
174  refcount = m.refcount;
175  data = m.data;
176  datastart = m.datastart;
177  dataend = m.dataend;
178  clCxt = m.clCxt;
179  wholerows = m.wholerows;
180  wholecols = m.wholecols;
181  offset = m.offset;
182  if( rRange == Range::all() )
183  rows = m.rows;
184  else
185  {
186  CV_Assert( 0 <= rRange.start && rRange.start <= rRange.end && rRange.end <= m.rows );
187  rows = rRange.size();
188  offset += step * rRange.start;
189  }
190 
191  if( cRange == Range::all() )
192  cols = m.cols;
193  else
194  {
195  CV_Assert( 0 <= cRange.start && cRange.start <= cRange.end && cRange.end <= m.cols );
196  cols = cRange.size();
197  offset += cRange.start * elemSize();
198  flags &= cols < m.cols ? ~Mat::CONTINUOUS_FLAG : -1;
199  }
200 
201  if( rows == 1 )
203 
204  if( refcount )
205  CV_XADD(refcount, 1);
206  if( rows <= 0 || cols <= 0 )
207  rows = cols = 0;
208  }
209 
210  inline oclMat::oclMat(const oclMat &m, const Rect &roi)
211  : flags(m.flags), rows(roi.height), cols(roi.width),
212  step(m.step), data(m.data), refcount(m.refcount),
213  datastart(m.datastart), dataend(m.dataend), clCxt(m.clCxt), offset(m.offset), wholerows(m.wholerows), wholecols(m.wholecols)
214  {
215  flags &= roi.width < m.cols ? ~Mat::CONTINUOUS_FLAG : -1;
216  offset += roi.y * step + roi.x * elemSize();
217  CV_Assert( 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.wholecols &&
218  0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.wholerows );
219  if( refcount )
220  CV_XADD(refcount, 1);
221  if( rows <= 0 || cols <= 0 )
222  rows = cols = 0;
223  }
224 
225  inline oclMat::oclMat(const Mat &m)
226  : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0) , offset(0), wholerows(0), wholecols(0)
227  {
228  //clCxt = Context::getContext();
229  upload(m);
230  }
231 
233  {
234  release();
235  }
236 
238  {
239  if( this != &m )
240  {
241  if( m.refcount )
242  CV_XADD(m.refcount, 1);
243  release();
244  clCxt = m.clCxt;
245  flags = m.flags;
246  rows = m.rows;
247  cols = m.cols;
248  step = m.step;
249  data = m.data;
250  datastart = m.datastart;
251  dataend = m.dataend;
252  offset = m.offset;
253  wholerows = m.wholerows;
254  wholecols = m.wholecols;
255  refcount = m.refcount;
256  }
257  return *this;
258  }
259 
260  inline oclMat &oclMat::operator = (const Mat &m)
261  {
262  //clCxt = Context::getContext();
263  upload(m);
264  return *this;
265  }
266 
267  inline oclMat& oclMat::operator = (const oclMatExpr& expr)
268  {
269  expr.assign(*this);
270  return *this;
271  }
272 
273  /* Fixme! To be supported in OpenCL later. */
274 #if 0
275  template <class T> inline oclMat::operator DevMem2D_<T>() const
276  {
277  return DevMem2D_<T>(rows, cols, (T *)data, step);
278  }
279  template <class T> inline oclMat::operator PtrStep_<T>() const
280  {
281  return PtrStep_<T>(static_cast< DevMem2D_<T> >(*this));
282  }
283 #endif
284 
285  //CPP: void oclMat::upload(const Mat& m);
286 
287  inline oclMat::operator Mat() const
288  {
289  Mat m;
290  download(m);
291  return m;
292  }
293 
294  //CPP void oclMat::download(cv::Mat& m) const;
295 
296  inline oclMat oclMat::row(int y) const
297  {
298  return oclMat(*this, Range(y, y + 1), Range::all());
299  }
300  inline oclMat oclMat::col(int x) const
301  {
302  return oclMat(*this, Range::all(), Range(x, x + 1));
303  }
304  inline oclMat oclMat::rowRange(int startrow, int endrow) const
305  {
306  return oclMat(*this, Range(startrow, endrow), Range::all());
307  }
308  inline oclMat oclMat::rowRange(const Range &r) const
309  {
310  return oclMat(*this, r, Range::all());
311  }
312  inline oclMat oclMat::colRange(int startcol, int endcol) const
313  {
314  return oclMat(*this, Range::all(), Range(startcol, endcol));
315  }
316  inline oclMat oclMat::colRange(const Range &r) const
317  {
318  return oclMat(*this, Range::all(), r);
319  }
320 
321  inline oclMat oclMat::clone() const
322  {
323  oclMat m;
324  copyTo(m);
325  return m;
326  }
327 
328  //CPP void oclMat::copyTo( oclMat& m ) const;
329  //CPP void oclMat::copyTo( oclMat& m, const oclMat& mask ) const;
330  //CPP void oclMat::convertTo( oclMat& m, int rtype, double alpha=1, double beta=0 ) const;
331 
332  inline void oclMat::assignTo( oclMat &m, int mtype ) const
333  {
334  if( mtype < 0 )
335  m = *this;
336  else
337  convertTo(m, mtype);
338  }
339 
340  //CPP oclMat& oclMat::operator = (const Scalar& s);
341  //CPP oclMat& oclMat::setTo(const Scalar& s, const oclMat& mask=oclMat());
342  //CPP oclMat oclMat::reshape(int _cn, int _rows=0) const;
343  inline void oclMat::create(Size _size, int _type)
344  {
345  create(_size.height, _size.width, _type);
346  }
347  //CPP void oclMat::create(int _rows, int _cols, int _type);
348  //CPP void oclMat::release();
349 
350  inline void oclMat::swap(oclMat &b)
351  {
352  std::swap( flags, b.flags );
353  std::swap( rows, b.rows );
354  std::swap( cols, b.cols );
355  std::swap( step, b.step );
356  std::swap( data, b.data );
357  std::swap( datastart, b.datastart );
358  std::swap( dataend, b.dataend );
359  std::swap( refcount, b.refcount );
360  std::swap( offset, b.offset );
361  std::swap( clCxt, b.clCxt );
362  std::swap( wholerows, b.wholerows );
363  std::swap( wholecols, b.wholecols );
364  }
365 
366  inline void oclMat::locateROI( Size &wholeSize, Point &ofs ) const
367  {
368  size_t esz = elemSize();//, minstep;
369  //ptrdiff_t delta1 = offset;//, delta2 = dataend - datastart;
370  CV_DbgAssert( step > 0 );
371  if( offset == 0 )
372  ofs.x = ofs.y = 0;
373  else
374  {
375  ofs.y = (int)(offset / step);
376  ofs.x = (int)((offset - step * ofs.y) / esz);
377  //CV_DbgAssert( data == datastart + ofs.y*step + ofs.x*esz );
378  }
379  //minstep = (ofs.x + cols)*esz;
380  //wholeSize.height = (int)((delta2 - minstep)/step + 1);
381  //wholeSize.height = std::max(wholeSize.height, ofs.y + rows);
382  //wholeSize.width = (int)((delta2 - step*(wholeSize.height-1))/esz);
383  //wholeSize.width = std::max(wholeSize.width, ofs.x + cols);
384  wholeSize.height = wholerows;
385  wholeSize.width = wholecols;
386  }
387 
388  inline oclMat &oclMat::adjustROI( int dtop, int dbottom, int dleft, int dright )
389  {
390  Size wholeSize;
391  Point ofs;
392  size_t esz = elemSize();
393  locateROI( wholeSize, ofs );
394  int row1 = std::max(ofs.y - dtop, 0), row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
395  int col1 = std::max(ofs.x - dleft, 0), col2 = std::min(ofs.x + cols + dright, wholeSize.width);
396  offset += (row1 - ofs.y) * step + (col1 - ofs.x) * esz;
397  rows = row2 - row1;
398  cols = col2 - col1;
399  if( esz * cols == step || rows == 1 )
401  else
402  flags &= ~Mat::CONTINUOUS_FLAG;
403  return *this;
404  }
405 
406  inline oclMat oclMat::operator()( Range rRange, Range cRange ) const
407  {
408  return oclMat(*this, rRange, cRange);
409  }
410  inline oclMat oclMat::operator()( const Rect &roi ) const
411  {
412  return oclMat(*this, roi);
413  }
414 
415  inline bool oclMat::isContinuous() const
416  {
417  return (flags & Mat::CONTINUOUS_FLAG) != 0;
418  }
419  inline size_t oclMat::elemSize() const
420  {
421  return CV_ELEM_SIZE((CV_MAKE_TYPE(type(), oclchannels())));
422  }
423  inline size_t oclMat::elemSize1() const
424  {
425  return CV_ELEM_SIZE1(flags);
426  }
427  inline int oclMat::type() const
428  {
429  return CV_MAT_TYPE(flags);
430  }
431  inline int oclMat::ocltype() const
432  {
433  return CV_MAKE_TYPE(depth(), oclchannels());
434  }
435  inline int oclMat::depth() const
436  {
437  return CV_MAT_DEPTH(flags);
438  }
439  inline int oclMat::channels() const
440  {
441  return CV_MAT_CN(flags);
442  }
443  inline int oclMat::oclchannels() const
444  {
445  return (CV_MAT_CN(flags)) == 3 ? 4 : (CV_MAT_CN(flags));
446  }
447  inline size_t oclMat::step1() const
448  {
449  return step / elemSize1();
450  }
451  inline Size oclMat::size() const
452  {
453  return Size(cols, rows);
454  }
455  inline bool oclMat::empty() const
456  {
457  return data == 0;
458  }
459 
460  inline oclMat oclMat::t() const
461  {
462  oclMat tmp;
463  transpose(*this, tmp);
464  return tmp;
465  }
466 
467  static inline void swap( oclMat &a, oclMat &b )
468  {
469  a.swap(b);
470  }
471 
472  inline void ensureSizeIsEnough(int rows, int cols, int type, oclMat &m)
473  {
474  if (m.type() == type && m.rows >= rows && m.cols >= cols)
475  m = m(Rect(0, 0, cols, rows));
476  else
477  m.create(rows, cols, type);
478  }
479 
480  inline void ensureSizeIsEnough(Size size, int type, oclMat &m)
481  {
482  ensureSizeIsEnough(size.height, size.width, type, m);
483  }
484 
485 
486  } /* end of namespace ocl */
487 
488 } /* end of namespace cv */
489 
490 #endif /* __OPENCV_OCL_MATRIX_OPERATIONS_HPP__ */
GLdouble GLdouble GLdouble r
Definition: core.hpp:1950
GLenum GLint GLint y
Definition: core_c.h:613
IplImage CvRect * roi
Definition: legacy.hpp:234
CV_EXPORTS void transpose(const oclMat &src, oclMat &dst)
transposes the matrix
void assignTo(oclMat &m, int type=-1) const
Definition: matrix_operations.hpp:332
Definition: matrix_operations.hpp:60
int type() const
returns element type, similar to CV_MAT_TYPE(cvMat->type)
Definition: matrix_operations.hpp:427
int start
Definition: core.hpp:990
int rows
the number of rows and columns
Definition: ocl.hpp:399
Definition: matrix_operations.hpp:62
Size2i Size
Definition: core.hpp:896
Definition: matrix_operations.hpp:58
int end
Definition: core.hpp:990
oclMat b
Definition: matrix_operations.hpp:77
void create(int rows, int cols, int type)
allocates new oclMatrix data unless the oclMatrix already has specified size and type.
static Range all()
Definition: operations.hpp:2219
The 2D range class.
Definition: core.hpp:979
GLenum GLsizei width
void convertTo(oclMat &m, int rtype, double alpha=1, double beta=0) const
converts oclMatrix to another datatype with optional scalng. See cvConvertScale.
bool isContinuous() const
returns true if the oclMatrix data is continuous
Definition: matrix_operations.hpp:415
int int int flags
Definition: highgui_c.h:186
int wholerows
Definition: ocl.hpp:419
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: core_c.h:403
_Tp x
Definition: core.hpp:766
const CvMat const CvMat const CvMat CvMat CvMat CvMat CvMat CvSize CvMat CvMat * T
Definition: calib3d.hpp:270
void release()
decreases reference counter;
oclMatExpr()
Definition: matrix_operations.hpp:70
internal::ParamGenerator< T > Range(T start, T end, IncrementT step)
Definition: ts_gtest.h:15869
_Tp height
Definition: core.hpp:883
void int step
Definition: core_c.h:403
void upload(const cv::Mat &m)
pefroms blocking upload data to oclMat.
The 2D size class.
Definition: core.hpp:81
int depth() const
returns element type, similar to CV_MAT_DEPTH(cvMat->type)
Definition: matrix_operations.hpp:435
size_t step
a distance between successive rows in bytes; includes the gap if any
Definition: ocl.hpp:401
oclMat clone() const
returns deep copy of the oclMatrix, i.e. the data is copied
Definition: matrix_operations.hpp:321
void locateROI(Size &wholeSize, Point &ofs) const
locates oclMatrix header within a parent oclMatrix. See below
Definition: matrix_operations.hpp:366
int oclchannels() const
Definition: matrix_operations.hpp:443
int ocltype() const
Definition: matrix_operations.hpp:431
int wholecols
Definition: ocl.hpp:420
oclMat col(int x) const
returns a new oclMatrix header for the specified column
Definition: matrix_operations.hpp:300
uchar * datastart
helper fields used in locateROI and adjustROI
Definition: ocl.hpp:411
Definition: matrix_operations.hpp:59
bool empty() const
returns true if oclMatrix data is NULL
Definition: matrix_operations.hpp:455
int cols
Definition: ocl.hpp:399
Definition: ocl.hpp:249
_Tp y
Definition: core.hpp:883
GLenum GLsizei GLsizei height
int flags
includes several bit-fields:
Definition: ocl.hpp:397
CV_EXPORTS_W void min(InputArray src1, InputArray src2, OutputArray dst)
computes per-element minimum of two arrays (dst = min(src1, src2))
Size size() const
returns oclMatrix size:
Definition: matrix_operations.hpp:451
Definition: matrix_operations.hpp:64
GLenum GLint x
Definition: core_c.h:632
oclMat rowRange(int startrow, int endrow) const
... for the specified row span
Definition: matrix_operations.hpp:304
GLintptr offset
_Tp x
Definition: core.hpp:883
Definition: matrix_operations.hpp:61
~oclMat()
destructor - calls release()
Definition: matrix_operations.hpp:232
size_t elemSize() const
returns element size in bytes,
Definition: matrix_operations.hpp:419
uchar * dataend
Definition: ocl.hpp:412
int op
Definition: matrix_operations.hpp:78
oclMat & adjustROI(int dtop, int dbottom, int dleft, int dright)
moves/resizes the current oclMatrix ROI inside the parent oclMatrix.
Definition: matrix_operations.hpp:388
Definition: matrix_operations.hpp:63
GLboolean GLboolean GLboolean b
Definition: legacy.hpp:633
The n-dimensional matrix class.
Definition: core.hpp:1688
int rows
Definition: core_c.h:114
int channels() const
returns element type, similar to CV_MAT_CN(cvMat->type)
Definition: matrix_operations.hpp:439
int cols
Definition: core_c.h:109
GLuint GLuint GLsizei GLenum type
Definition: core_c.h:114
void swap(oclMat &mat)
swaps with other smart pointer
Definition: matrix_operations.hpp:350
Definition: matrix_operations.hpp:67
oclMat colRange(int startcol, int endcol) const
... for the specified column span
Definition: matrix_operations.hpp:312
oclMat row(int y) const
returns a new oclMatrix header for the specified row
Definition: matrix_operations.hpp:296
uchar * data
pointer to the data(OCL memory object)
Definition: ocl.hpp:403
template 2D point class.
Definition: core.hpp:82
oclMat & operator=(const oclMat &m)
assignment operators
Definition: matrix_operations.hpp:237
_Tp width
Definition: core.hpp:840
void assign(oclMat &m) const
oclMatExpr(const oclMat &_a, const oclMat &_b, int _op)
Definition: matrix_operations.hpp:71
oclMat operator()(Range rowRange, Range colRange) const
extracts a rectangular sub-oclMatrix
Definition: matrix_operations.hpp:406
GLboolean GLboolean GLboolean GLboolean a
Definition: legacy.hpp:633
oclMat()
default constructor
Definition: matrix_operations.hpp:84
size_t step1() const
returns step/elemSize1()
Definition: matrix_operations.hpp:447
size_t elemSize1() const
returns the size of element channel in bytes.
Definition: matrix_operations.hpp:423
_Tp y
Definition: core.hpp:766
void ensureSizeIsEnough(int rows, int cols, int type, oclMat &m)
Definition: matrix_operations.hpp:472
::max::max int
Definition: functional.hpp:324
CvPoint2D32f float a
Definition: legacy.hpp:578
CV_EXPORTS void swap(Mat &a, Mat &b)
swaps two matrices
oclMat t() const
matrix transposition
Definition: matrix_operations.hpp:460
const GLfloat * m
_Tp height
Definition: core.hpp:840
Context * clCxt
OpenCL context associated with the oclMat object.
Definition: ocl.hpp:415
int size() const
Definition: operations.hpp:2217
GLsizeiptr size
Definition: core_c.h:939
Rect_< int > Rect
Definition: core.hpp:897
void copyTo(oclMat &m, const oclMat &mask=oclMat()) const
copies those oclMatrix elements to "m" that are marked with non-zero mask elements.
_Tp width
Definition: core.hpp:883
Definition: matrix_operations.hpp:57
int * refcount
pointer to the reference counter;
Definition: ocl.hpp:407
int offset
Definition: ocl.hpp:417