opencv/modules/core/src/matrix.cpp

946 lines
25 KiB
C++
Raw Normal View History

2018-02-06 00:16:33 +08:00
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html
#include "precomp.hpp"
2014-01-16 22:30:39 +08:00
#include "bufferpool.impl.hpp"
namespace cv {
void MatAllocator::map(UMatData*, int) const
{
}
void MatAllocator::unmap(UMatData* u) const
{
if(u->urefcount == 0 && u->refcount == 0)
2013-12-16 20:46:36 +08:00
{
deallocate(u);
2013-12-16 20:46:36 +08:00
}
}
void MatAllocator::download(UMatData* u, void* dstptr,
int dims, const size_t sz[],
const size_t srcofs[], const size_t srcstep[],
const size_t dststep[]) const
{
if(!u)
return;
int isz[CV_MAX_DIM];
uchar* srcptr = u->data;
for( int i = 0; i < dims; i++ )
{
CV_Assert( sz[i] <= (size_t)INT_MAX );
if( sz[i] == 0 )
return;
if( srcofs )
srcptr += srcofs[i]*(i <= dims-2 ? srcstep[i] : 1);
isz[i] = (int)sz[i];
}
Mat src(dims, isz, CV_8U, srcptr, srcstep);
Mat dst(dims, isz, CV_8U, dstptr, dststep);
const Mat* arrays[] = { &src, &dst };
uchar* ptrs[2];
NAryMatIterator it(arrays, ptrs, 2);
size_t planesz = it.size;
for( size_t j = 0; j < it.nplanes; j++, ++it )
memcpy(ptrs[1], ptrs[0], planesz);
}
void MatAllocator::upload(UMatData* u, const void* srcptr, int dims, const size_t sz[],
const size_t dstofs[], const size_t dststep[],
const size_t srcstep[]) const
{
if(!u)
return;
int isz[CV_MAX_DIM];
uchar* dstptr = u->data;
for( int i = 0; i < dims; i++ )
{
CV_Assert( sz[i] <= (size_t)INT_MAX );
if( sz[i] == 0 )
return;
if( dstofs )
dstptr += dstofs[i]*(i <= dims-2 ? dststep[i] : 1);
isz[i] = (int)sz[i];
}
Mat src(dims, isz, CV_8U, (void*)srcptr, srcstep);
Mat dst(dims, isz, CV_8U, dstptr, dststep);
const Mat* arrays[] = { &src, &dst };
uchar* ptrs[2];
NAryMatIterator it(arrays, ptrs, 2);
size_t planesz = it.size;
for( size_t j = 0; j < it.nplanes; j++, ++it )
memcpy(ptrs[1], ptrs[0], planesz);
}
void MatAllocator::copy(UMatData* usrc, UMatData* udst, int dims, const size_t sz[],
const size_t srcofs[], const size_t srcstep[],
const size_t dstofs[], const size_t dststep[], bool /*sync*/) const
{
CV_INSTRUMENT_REGION()
if(!usrc || !udst)
return;
int isz[CV_MAX_DIM];
uchar* srcptr = usrc->data;
uchar* dstptr = udst->data;
for( int i = 0; i < dims; i++ )
{
CV_Assert( sz[i] <= (size_t)INT_MAX );
if( sz[i] == 0 )
2013-12-17 18:14:04 +08:00
return;
if( srcofs )
2013-12-17 18:14:04 +08:00
srcptr += srcofs[i]*(i <= dims-2 ? srcstep[i] : 1);
if( dstofs )
2013-12-17 18:14:04 +08:00
dstptr += dstofs[i]*(i <= dims-2 ? dststep[i] : 1);
isz[i] = (int)sz[i];
}
Mat src(dims, isz, CV_8U, srcptr, srcstep);
Mat dst(dims, isz, CV_8U, dstptr, dststep);
const Mat* arrays[] = { &src, &dst };
uchar* ptrs[2];
NAryMatIterator it(arrays, ptrs, 2);
size_t planesz = it.size;
for( size_t j = 0; j < it.nplanes; j++, ++it )
memcpy(ptrs[1], ptrs[0], planesz);
}
2015-01-02 08:33:40 +08:00
BufferPoolController* MatAllocator::getBufferPoolController(const char* id) const
2014-01-16 22:30:39 +08:00
{
2015-01-02 08:33:40 +08:00
(void)id;
2014-01-16 22:30:39 +08:00
static DummyBufferPoolController dummy;
return &dummy;
}
2018-03-15 21:16:50 +08:00
class StdMatAllocator CV_FINAL : public MatAllocator
{
public:
UMatData* allocate(int dims, const int* sizes, int type,
2018-03-15 21:16:50 +08:00
void* data0, size_t* step, int /*flags*/, UMatUsageFlags /*usageFlags*/) const CV_OVERRIDE
{
size_t total = CV_ELEM_SIZE(type);
for( int i = dims-1; i >= 0; i-- )
{
if( step )
{
if( data0 && step[i] != CV_AUTOSTEP )
{
CV_Assert(total <= step[i]);
total = step[i];
}
else
step[i] = total;
}
total *= sizes[i];
}
uchar* data = data0 ? (uchar*)data0 : (uchar*)fastMalloc(total);
UMatData* u = new UMatData(this);
u->data = u->origdata = data;
u->size = total;
if(data0)
u->flags |= UMatData::USER_ALLOCATED;
return u;
}
2018-03-15 21:16:50 +08:00
bool allocate(UMatData* u, int /*accessFlags*/, UMatUsageFlags /*usageFlags*/) const CV_OVERRIDE
{
if(!u) return false;
return true;
}
2018-03-15 21:16:50 +08:00
void deallocate(UMatData* u) const CV_OVERRIDE
{
2015-03-05 15:31:51 +08:00
if(!u)
return;
2015-09-08 09:06:04 +08:00
CV_Assert(u->urefcount == 0);
CV_Assert(u->refcount == 0);
if( !(u->flags & UMatData::USER_ALLOCATED) )
{
2015-09-08 09:06:04 +08:00
fastFree(u->origdata);
u->origdata = 0;
}
2015-09-08 09:06:04 +08:00
delete u;
}
};
2018-02-06 00:16:33 +08:00
namespace
{
MatAllocator* volatile g_matAllocator = NULL;
}
MatAllocator* Mat::getDefaultAllocator()
{
2015-12-08 23:37:54 +08:00
if (g_matAllocator == NULL)
{
cv::AutoLock lock(cv::getInitializationMutex());
if (g_matAllocator == NULL)
{
g_matAllocator = getStdAllocator();
}
2015-12-08 23:37:54 +08:00
}
return g_matAllocator;
}
void Mat::setDefaultAllocator(MatAllocator* allocator)
{
2015-12-08 23:37:54 +08:00
g_matAllocator = allocator;
}
MatAllocator* Mat::getStdAllocator()
{
2015-12-08 23:37:54 +08:00
CV_SINGLETON_LAZY_INIT(MatAllocator, new StdMatAllocator())
}
2018-02-06 00:16:33 +08:00
//==================================================================================================
2010-10-12 20:31:40 +08:00
2018-02-06 00:16:33 +08:00
void setSize( Mat& m, int _dims, const int* _sz, const size_t* _steps, bool autoSteps)
2010-10-12 20:31:40 +08:00
{
CV_Assert( 0 <= _dims && _dims <= CV_MAX_DIM );
if( m.dims != _dims )
{
if( m.step.p != m.step.buf )
{
fastFree(m.step.p);
m.step.p = m.step.buf;
m.size.p = &m.rows;
}
if( _dims > 2 )
{
m.step.p = (size_t*)fastMalloc(_dims*sizeof(m.step.p[0]) + (_dims+1)*sizeof(m.size.p[0]));
m.size.p = (int*)(m.step.p + _dims) + 1;
m.size.p[-1] = _dims;
m.rows = m.cols = -1;
2010-10-12 20:31:40 +08:00
}
}
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
m.dims = _dims;
if( !_sz )
return;
2012-06-08 01:21:29 +08:00
2014-02-05 20:01:15 +08:00
size_t esz = CV_ELEM_SIZE(m.flags), esz1 = CV_ELEM_SIZE1(m.flags), total = esz;
for( int i = _dims-1; i >= 0; i-- )
2010-10-12 20:31:40 +08:00
{
int s = _sz[i];
CV_Assert( s >= 0 );
2010-10-12 20:31:40 +08:00
m.size.p[i] = s;
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
if( _steps )
2014-02-05 20:01:15 +08:00
{
if (_steps[i] % esz1 != 0)
{
CV_Error(Error::BadStep, "Step must be a multiple of esz1");
}
2010-10-12 20:31:40 +08:00
m.step.p[i] = i < _dims-1 ? _steps[i] : esz;
2014-02-05 20:01:15 +08:00
}
2010-10-12 20:31:40 +08:00
else if( autoSteps )
{
m.step.p[i] = total;
int64 total1 = (int64)total*s;
if( (uint64)total1 != (size_t)total1 )
CV_Error( CV_StsOutOfRange, "The total matrix size does not fit to \"size_t\" type" );
total = (size_t)total1;
}
}
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
if( _dims == 1 )
{
m.dims = 2;
m.cols = 1;
m.step[1] = esz;
}
}
2012-06-08 01:21:29 +08:00
int updateContinuityFlag(int flags, int dims, const int* size, const size_t* step)
2010-10-12 20:31:40 +08:00
{
int i, j;
for( i = 0; i < dims; i++ )
2010-10-12 20:31:40 +08:00
{
if( size[i] > 1 )
2010-10-12 20:31:40 +08:00
break;
}
2012-06-08 01:21:29 +08:00
uint64 t = (uint64)size[std::min(i, dims-1)]*CV_MAT_CN(flags);
for( j = dims-1; j > i; j-- )
2010-10-12 20:31:40 +08:00
{
t *= size[j];
if( step[j]*size[j] < step[j-1] )
2010-10-12 20:31:40 +08:00
break;
}
2012-06-08 01:21:29 +08:00
if( j <= i && t == (uint64)(int)t )
return flags | Mat::CONTINUOUS_FLAG;
return flags & ~Mat::CONTINUOUS_FLAG;
}
void Mat::updateContinuityFlag()
{
flags = cv::updateContinuityFlag(flags, dims, size.p, step.p);
}
2012-06-08 01:21:29 +08:00
2018-02-06 00:16:33 +08:00
void finalizeHdr(Mat& m)
{
m.updateContinuityFlag();
int d = m.dims;
if( d > 2 )
2010-10-12 20:31:40 +08:00
m.rows = m.cols = -1;
if(m.u)
m.datastart = m.data = m.u->data;
2010-10-12 20:31:40 +08:00
if( m.data )
{
m.datalimit = m.datastart + m.size[0]*m.step[0];
if( m.size[0] > 0 )
{
m.dataend = m.ptr() + m.size[d-1]*m.step[d-1];
for( int i = 0; i < d-1; i++ )
m.dataend += (m.size[i] - 1)*m.step[i];
}
else
m.dataend = m.datalimit;
2010-10-12 20:31:40 +08:00
}
else
m.dataend = m.datalimit = 0;
2010-10-12 20:31:40 +08:00
}
2012-06-08 01:21:29 +08:00
2018-02-06 00:16:33 +08:00
//==================================================================================================
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
void Mat::create(int d, const int* _sizes, int _type)
{
int i;
2013-04-01 15:29:13 +08:00
CV_Assert(0 <= d && d <= CV_MAX_DIM && _sizes);
2010-10-12 20:31:40 +08:00
_type = CV_MAT_TYPE(_type);
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
if( data && (d == dims || (d == 1 && dims <= 2)) && _type == type() )
{
if( d == 2 && rows == _sizes[0] && cols == _sizes[1] )
return;
for( i = 0; i < d; i++ )
if( size[i] != _sizes[i] )
break;
if( i == d && (d > 1 || size[1] == 1))
return;
}
2012-06-08 01:21:29 +08:00
int _sizes_backup[CV_MAX_DIM]; // #5991
if (_sizes == (this->size.p))
{
for(i = 0; i < d; i++ )
_sizes_backup[i] = _sizes[i];
_sizes = _sizes_backup;
}
2010-10-12 20:31:40 +08:00
release();
if( d == 0 )
return;
flags = (_type & CV_MAT_TYPE_MASK) | MAGIC_VAL;
setSize(*this, d, _sizes, 0, true);
2012-06-08 01:21:29 +08:00
if( total() > 0 )
2010-10-12 20:31:40 +08:00
{
MatAllocator *a = allocator, *a0 = getDefaultAllocator();
2011-11-25 23:39:24 +08:00
#ifdef HAVE_TGPU
if( !a || a == tegra::getAllocator() )
a = tegra::getAllocator(d, _sizes, _type);
2011-11-25 23:39:24 +08:00
#endif
if(!a)
a = a0;
2017-11-28 17:53:40 +08:00
CV_TRY
{
u = a->allocate(dims, size, _type, 0, step.p, 0, USAGE_DEFAULT);
CV_Assert(u != 0);
}
2017-11-28 17:53:40 +08:00
CV_CATCH_ALL
{
if(a != a0)
u = a0->allocate(dims, size, _type, 0, step.p, 0, USAGE_DEFAULT);
CV_Assert(u != 0);
}
CV_Assert( step[dims-1] == (size_t)CV_ELEM_SIZE(flags) );
2010-10-12 20:31:40 +08:00
}
2012-06-08 01:21:29 +08:00
2013-12-16 20:46:36 +08:00
addref();
2010-10-12 20:31:40 +08:00
finalizeHdr(*this);
}
void Mat::create(const std::vector<int>& _sizes, int _type)
{
create((int)_sizes.size(), _sizes.data(), _type);
}
2010-10-12 20:31:40 +08:00
void Mat::copySize(const Mat& m)
{
setSize(*this, m.dims, 0, 0);
for( int i = 0; i < dims; i++ )
{
size[i] = m.size[i];
step[i] = m.step[i];
}
}
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
void Mat::deallocate()
{
if(u)
{
UMatData* u_ = u;
u = NULL;
(u_->currAllocator ? u_->currAllocator : allocator ? allocator : getDefaultAllocator())->unmap(u_);
}
2010-10-12 20:31:40 +08:00
}
2013-03-30 23:03:55 +08:00
Mat::Mat(const Mat& m, const Range& _rowRange, const Range& _colRange)
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
datalimit(0), allocator(0), u(0), size(&rows)
2010-10-12 20:31:40 +08:00
{
CV_Assert( m.dims >= 2 );
if( m.dims > 2 )
{
AutoBuffer<Range> rs(m.dims);
2012-06-09 23:00:04 +08:00
rs[0] = _rowRange;
rs[1] = _colRange;
2010-10-12 20:31:40 +08:00
for( int i = 2; i < m.dims; i++ )
rs[i] = Range::all();
2018-06-11 06:42:00 +08:00
*this = m(rs.data());
2010-10-12 20:31:40 +08:00
return;
}
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
*this = m;
2017-11-28 17:53:40 +08:00
CV_TRY
2010-10-12 20:31:40 +08:00
{
if( _rowRange != Range::all() && _rowRange != Range(0,rows) )
{
CV_Assert( 0 <= _rowRange.start && _rowRange.start <= _rowRange.end
&& _rowRange.end <= m.rows );
rows = _rowRange.size();
data += step*_rowRange.start;
flags |= SUBMATRIX_FLAG;
}
2012-06-08 01:21:29 +08:00
if( _colRange != Range::all() && _colRange != Range(0,cols) )
{
CV_Assert( 0 <= _colRange.start && _colRange.start <= _colRange.end
&& _colRange.end <= m.cols );
cols = _colRange.size();
data += _colRange.start*elemSize();
flags |= SUBMATRIX_FLAG;
}
}
2017-11-28 17:53:40 +08:00
CV_CATCH_ALL
2010-10-12 20:31:40 +08:00
{
release();
2017-11-28 17:53:40 +08:00
CV_RETHROW();
2010-10-12 20:31:40 +08:00
}
2012-06-08 01:21:29 +08:00
updateContinuityFlag();
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
if( rows <= 0 || cols <= 0 )
{
release();
rows = cols = 0;
}
}
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
Mat::Mat(const Mat& m, const Rect& roi)
: flags(m.flags), dims(2), rows(roi.height), cols(roi.width),
data(m.data + roi.y*m.step[0]),
datastart(m.datastart), dataend(m.dataend), datalimit(m.datalimit),
allocator(m.allocator), u(m.u), size(&rows)
2010-10-12 20:31:40 +08:00
{
CV_Assert( m.dims <= 2 );
2012-06-08 01:21:29 +08:00
size_t esz = CV_ELEM_SIZE(flags);
2010-10-12 20:31:40 +08:00
data += roi.x*esz;
CV_Assert( 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols &&
0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows );
if( u )
CV_XADD(&u->refcount, 1);
if( roi.width < m.cols || roi.height < m.rows )
flags |= SUBMATRIX_FLAG;
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
step[0] = m.step[0]; step[1] = esz;
updateContinuityFlag();
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
if( rows <= 0 || cols <= 0 )
{
release();
rows = cols = 0;
}
}
2012-06-08 01:21:29 +08:00
2013-03-30 23:03:55 +08:00
Mat::Mat(int _dims, const int* _sizes, int _type, void* _data, const size_t* _steps)
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
datalimit(0), allocator(0), u(0), size(&rows)
2010-10-12 20:31:40 +08:00
{
flags |= CV_MAT_TYPE(_type);
datastart = data = (uchar*)_data;
2010-10-12 20:31:40 +08:00
setSize(*this, _dims, _sizes, _steps, true);
finalizeHdr(*this);
}
2012-06-08 01:21:29 +08:00
Mat::Mat(const std::vector<int>& _sizes, int _type, void* _data, const size_t* _steps)
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
datalimit(0), allocator(0), u(0), size(&rows)
{
flags |= CV_MAT_TYPE(_type);
datastart = data = (uchar*)_data;
setSize(*this, (int)_sizes.size(), _sizes.data(), _steps, true);
finalizeHdr(*this);
}
2013-03-30 23:03:55 +08:00
Mat::Mat(const Mat& m, const Range* ranges)
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
datalimit(0), allocator(0), u(0), size(&rows)
2010-10-12 20:31:40 +08:00
{
int d = m.dims;
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
CV_Assert(ranges);
for( int i = 0; i < d; i++ )
2010-10-12 20:31:40 +08:00
{
Range r = ranges[i];
CV_Assert( r == Range::all() || (0 <= r.start && r.start < r.end && r.end <= m.size[i]) );
}
*this = m;
for( int i = 0; i < d; i++ )
2010-10-12 20:31:40 +08:00
{
Range r = ranges[i];
if( r != Range::all() && r != Range(0, size.p[i]))
2010-10-12 20:31:40 +08:00
{
size.p[i] = r.end - r.start;
data += r.start*step.p[i];
flags |= SUBMATRIX_FLAG;
2010-10-12 20:31:40 +08:00
}
}
updateContinuityFlag();
2010-10-12 20:31:40 +08:00
}
2012-06-08 01:21:29 +08:00
Mat::Mat(const Mat& m, const std::vector<Range>& ranges)
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
datalimit(0), allocator(0), u(0), size(&rows)
{
int d = m.dims;
CV_Assert((int)ranges.size() == d);
for (int i = 0; i < d; i++)
{
Range r = ranges[i];
CV_Assert(r == Range::all() || (0 <= r.start && r.start < r.end && r.end <= m.size[i]));
}
*this = m;
for (int i = 0; i < d; i++)
{
Range r = ranges[i];
if (r != Range::all() && r != Range(0, size.p[i]))
{
size.p[i] = r.end - r.start;
data += r.start*step.p[i];
flags |= SUBMATRIX_FLAG;
}
}
updateContinuityFlag();
}
2012-06-08 01:21:29 +08:00
2013-03-29 01:01:12 +08:00
Mat Mat::diag(int d) const
{
2010-10-12 20:31:40 +08:00
CV_Assert( dims <= 2 );
2013-03-29 01:01:12 +08:00
Mat m = *this;
size_t esz = elemSize();
int len;
2013-03-29 01:01:12 +08:00
if( d >= 0 )
{
len = std::min(cols - d, rows);
m.data += esz*d;
}
else
{
len = std::min(rows + d, cols);
m.data -= step[0]*d;
}
CV_DbgAssert( len > 0 );
m.size[0] = m.rows = len;
m.size[1] = m.cols = 1;
m.step[0] += (len > 1 ? esz : 0);
m.updateContinuityFlag();
2013-03-29 01:01:12 +08:00
if( size() != Size(1,1) )
m.flags |= SUBMATRIX_FLAG;
return m;
}
2012-06-08 01:21:29 +08:00
2018-02-06 00:16:33 +08:00
void Mat::pop_back(size_t nelems)
{
CV_Assert( nelems <= (size_t)size.p[0] );
2012-06-08 01:21:29 +08:00
if( isSubmatrix() )
*this = rowRange(0, size.p[0] - (int)nelems);
else
{
size.p[0] -= (int)nelems;
dataend -= nelems*step.p[0];
}
}
2012-06-08 01:21:29 +08:00
void Mat::push_back_(const void* elem)
{
size_t r = size.p[0];
if( isSubmatrix() || dataend + step.p[0] > datalimit )
reserve( std::max(r + 1, (r*3+1)/2) );
2012-06-08 01:21:29 +08:00
size_t esz = elemSize();
memcpy(data + r*step.p[0], elem, esz);
size.p[0] = int(r + 1);
dataend += step.p[0];
uint64 tsz = size.p[0];
for( int i = 1; i < dims; i++ )
tsz *= size.p[i];
if( esz < step.p[0] || tsz != (uint64)(int)tsz )
flags &= ~CONTINUOUS_FLAG;
}
2018-02-06 00:16:33 +08:00
void Mat::reserve(size_t nelems)
{
const size_t MIN_SIZE = 64;
2012-06-08 01:21:29 +08:00
CV_Assert( (int)nelems >= 0 );
if( !isSubmatrix() && data + step.p[0]*nelems <= datalimit )
return;
2012-06-08 01:21:29 +08:00
int r = size.p[0];
2012-06-08 01:21:29 +08:00
if( (size_t)r >= nelems )
return;
2012-06-08 01:21:29 +08:00
size.p[0] = std::max((int)nelems, 1);
size_t newsize = total()*elemSize();
2012-06-08 01:21:29 +08:00
if( newsize < MIN_SIZE )
size.p[0] = (int)((MIN_SIZE + newsize - 1)*nelems/newsize);
2012-06-08 01:21:29 +08:00
Mat m(dims, size.p, type());
size.p[0] = r;
if( r > 0 )
{
Mat mpart = m.rowRange(0, r);
copyTo(mpart);
}
2012-06-08 01:21:29 +08:00
*this = m;
size.p[0] = r;
dataend = data + step.p[0]*r;
}
2018-02-06 00:16:33 +08:00
void Mat::reserveBuffer(size_t nbytes)
{
size_t esz = 1;
int mtype = CV_8UC1;
if (!empty())
{
if (!isSubmatrix() && data + nbytes <= dataend)//Should it be datalimit?
return;
esz = elemSize();
mtype = type();
}
size_t nelems = (nbytes - 1) / esz + 1;
#if SIZE_MAX > UINT_MAX
CV_Assert(nelems <= size_t(INT_MAX)*size_t(INT_MAX));
int newrows = nelems > size_t(INT_MAX) ? nelems > 0x400*size_t(INT_MAX) ? nelems > 0x100000 * size_t(INT_MAX) ? nelems > 0x40000000 * size_t(INT_MAX) ?
size_t(INT_MAX) : 0x40000000 : 0x100000 : 0x400 : 1;
#else
int newrows = nelems > size_t(INT_MAX) ? 2 : 1;
#endif
int newcols = (int)((nelems - 1) / newrows + 1);
create(newrows, newcols, mtype);
}
2012-06-08 01:21:29 +08:00
void Mat::resize(size_t nelems)
{
int saveRows = size.p[0];
if( saveRows == (int)nelems )
return;
CV_Assert( (int)nelems >= 0 );
2012-06-08 01:21:29 +08:00
if( isSubmatrix() || data + step.p[0]*nelems > datalimit )
reserve(nelems);
2012-06-08 01:21:29 +08:00
size.p[0] = (int)nelems;
dataend += (size.p[0] - saveRows)*step.p[0];
2012-06-08 01:21:29 +08:00
//updateContinuityFlag(*this);
2012-06-08 01:21:29 +08:00
}
void Mat::resize(size_t nelems, const Scalar& s)
{
int saveRows = size.p[0];
resize(nelems);
2012-06-08 01:21:29 +08:00
if( size.p[0] > saveRows )
{
Mat part = rowRange(saveRows, size.p[0]);
part = s;
}
2012-06-08 01:21:29 +08:00
}
void Mat::push_back(const Mat& elems)
{
size_t r = size.p[0];
size_t delta = elems.size.p[0];
if( delta == 0 )
return;
if( this == &elems )
{
Mat tmp = elems;
push_back(tmp);
return;
}
2012-06-08 01:21:29 +08:00
if( !data )
{
*this = elems.clone();
return;
}
size.p[0] = elems.size.p[0];
bool eq = size == elems.size;
size.p[0] = int(r);
if( !eq )
2016-07-14 22:13:09 +08:00
CV_Error(CV_StsUnmatchedSizes, "Pushed vector length is not equal to matrix row length");
if( type() != elems.type() )
2016-07-14 22:13:09 +08:00
CV_Error(CV_StsUnmatchedFormats, "Pushed vector type is not the same as matrix type");
2012-06-08 01:21:29 +08:00
if( isSubmatrix() || dataend + step.p[0]*delta > datalimit )
reserve( std::max(r + delta, (r*3+1)/2) );
2012-06-08 01:21:29 +08:00
size.p[0] += int(delta);
dataend += step.p[0]*delta;
2012-06-08 01:21:29 +08:00
//updateContinuityFlag(*this);
2012-06-08 01:21:29 +08:00
if( isContinuous() && elems.isContinuous() )
memcpy(data + r*step.p[0], elems.data, elems.total()*elems.elemSize());
else
{
Mat part = rowRange(int(r), int(r + delta));
elems.copyTo(part);
}
}
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
void Mat::locateROI( Size& wholeSize, Point& ofs ) const
{
CV_Assert( dims <= 2 && step[0] > 0 );
size_t esz = elemSize(), minstep;
ptrdiff_t delta1 = data - datastart, delta2 = dataend - datastart;
2012-06-08 01:21:29 +08:00
2010-10-12 20:31:40 +08:00
if( delta1 == 0 )
ofs.x = ofs.y = 0;
else
{
2010-10-12 20:31:40 +08:00
ofs.y = (int)(delta1/step[0]);
ofs.x = (int)((delta1 - step[0]*ofs.y)/esz);
CV_DbgAssert( data == datastart + ofs.y*step[0] + ofs.x*esz );
}
2010-10-12 20:31:40 +08:00
minstep = (ofs.x + cols)*esz;
wholeSize.height = (int)((delta2 - minstep)/step[0] + 1);
wholeSize.height = std::max(wholeSize.height, ofs.y + rows);
wholeSize.width = (int)((delta2 - step*(wholeSize.height-1))/esz);
wholeSize.width = std::max(wholeSize.width, ofs.x + cols);
}
2010-10-12 20:31:40 +08:00
Mat& Mat::adjustROI( int dtop, int dbottom, int dleft, int dright )
{
CV_Assert( dims <= 2 && step[0] > 0 );
Size wholeSize; Point ofs;
size_t esz = elemSize();
locateROI( wholeSize, ofs );
int row1 = std::min(std::max(ofs.y - dtop, 0), wholeSize.height), row2 = std::max(0, std::min(ofs.y + rows + dbottom, wholeSize.height));
int col1 = std::min(std::max(ofs.x - dleft, 0), wholeSize.width), col2 = std::max(0, std::min(ofs.x + cols + dright, wholeSize.width));
if(row1 > row2)
std::swap(row1, row2);
if(col1 > col2)
std::swap(col1, col2);
2010-10-12 20:31:40 +08:00
data += (row1 - ofs.y)*step + (col1 - ofs.x)*esz;
rows = row2 - row1; cols = col2 - col1;
size.p[0] = rows; size.p[1] = cols;
updateContinuityFlag();
2010-10-12 20:31:40 +08:00
return *this;
2012-06-08 01:21:29 +08:00
}
Mat Mat::reshape(int new_cn, int new_rows) const
{
int cn = channels();
Mat hdr = *this;
2012-06-08 01:21:29 +08:00
if( dims > 2 )
{
if( new_rows == 0 && new_cn != 0 && size[dims-1]*cn % new_cn == 0 )
{
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn-1) << CV_CN_SHIFT);
hdr.step[dims-1] = CV_ELEM_SIZE(hdr.flags);
hdr.size[dims-1] = hdr.size[dims-1]*cn / new_cn;
return hdr;
}
if( new_rows > 0 )
{
int sz[] = { new_rows, (int)(total()/new_rows) };
return reshape(new_cn, 2, sz);
}
}
2012-06-08 01:21:29 +08:00
CV_Assert( dims <= 2 );
2012-06-08 01:21:29 +08:00
if( new_cn == 0 )
new_cn = cn;
int total_width = cols * cn;
if( (new_cn > total_width || total_width % new_cn != 0) && new_rows == 0 )
new_rows = rows * total_width / new_cn;
if( new_rows != 0 && new_rows != rows )
{
int total_size = total_width * rows;
if( !isContinuous() )
CV_Error( CV_BadStep,
"The matrix is not continuous, thus its number of rows can not be changed" );
if( (unsigned)new_rows > (unsigned)total_size )
CV_Error( CV_StsOutOfRange, "Bad new number of rows" );
total_width = total_size / new_rows;
if( total_width * new_rows != total_size )
CV_Error( CV_StsBadArg, "The total number of matrix elements "
"is not divisible by the new number of rows" );
hdr.rows = new_rows;
2010-10-12 20:31:40 +08:00
hdr.step[0] = total_width * elemSize1();
}
int new_width = total_width / new_cn;
if( new_width * new_cn != total_width )
CV_Error( CV_BadNumChannels,
"The total width is not divisible by the new number of channels" );
hdr.cols = new_width;
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn-1) << CV_CN_SHIFT);
hdr.step[1] = CV_ELEM_SIZE(hdr.flags);
return hdr;
}
2018-02-06 00:16:33 +08:00
Mat Mat::reshape(int _cn, int _newndims, const int* _newsz) const
{
2018-02-06 00:16:33 +08:00
if(_newndims == dims)
{
2018-02-06 00:16:33 +08:00
if(_newsz == 0)
return reshape(_cn);
if(_newndims == 2)
return reshape(_cn, _newsz[0]);
}
2012-06-08 01:21:29 +08:00
2018-02-06 00:16:33 +08:00
if (isContinuous())
{
2018-02-06 00:16:33 +08:00
CV_Assert(_cn >= 0 && _newndims > 0 && _newndims <= CV_MAX_DIM && _newsz);
2018-02-06 00:16:33 +08:00
if (_cn == 0)
_cn = this->channels();
else
CV_Assert(_cn <= CV_CN_MAX);
2018-02-06 00:16:33 +08:00
size_t total_elem1_ref = this->total() * this->channels();
size_t total_elem1 = _cn;
2018-02-06 00:16:33 +08:00
AutoBuffer<int, 4> newsz_buf( (size_t)_newndims );
2018-02-06 00:16:33 +08:00
for (int i = 0; i < _newndims; i++)
{
CV_Assert(_newsz[i] >= 0);
2018-02-06 00:16:33 +08:00
if (_newsz[i] > 0)
newsz_buf[i] = _newsz[i];
else if (i < dims)
newsz_buf[i] = this->size[i];
else
CV_Error(CV_StsOutOfRange, "Copy dimension (which has zero size) is not present in source matrix");
2018-02-06 00:16:33 +08:00
total_elem1 *= (size_t)newsz_buf[i];
}
2018-02-06 00:16:33 +08:00
if (total_elem1 != total_elem1_ref)
CV_Error(CV_StsUnmatchedSizes, "Requested and source matrices have different count of elements");
2018-02-06 00:16:33 +08:00
Mat hdr = *this;
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((_cn-1) << CV_CN_SHIFT);
2018-06-11 06:42:00 +08:00
setSize(hdr, _newndims, newsz_buf.data(), NULL, true);
2018-02-06 00:16:33 +08:00
return hdr;
}
2018-02-06 00:16:33 +08:00
CV_Error(CV_StsNotImplemented, "Reshaping of n-dimensional non-continuous matrices is not supported yet");
// TBD
}
2018-02-06 00:16:33 +08:00
Mat Mat::reshape(int _cn, const std::vector<int>& _newshape) const
{
2018-02-06 00:16:33 +08:00
if(_newshape.empty())
{
2018-02-06 00:16:33 +08:00
CV_Assert(empty());
return *this;
}
2018-02-06 00:16:33 +08:00
return reshape(_cn, (int)_newshape.size(), &_newshape[0]);
}
2012-06-08 01:21:29 +08:00
2018-02-06 00:16:33 +08:00
Mat Mat::diag(const Mat& d)
{
2018-02-06 00:16:33 +08:00
CV_Assert( d.cols == 1 || d.rows == 1 );
int len = d.rows + d.cols - 1;
Mat m(len, len, d.type(), Scalar(0));
Mat md = m.diag();
if( d.cols == 1 )
d.copyTo(md);
else
transpose(d, md);
return m;
}
2018-02-06 00:16:33 +08:00
int Mat::checkVector(int _elemChannels, int _depth, bool _requireContinuous) const
2013-03-29 01:01:12 +08:00
{
2018-02-06 00:16:33 +08:00
return data && (depth() == _depth || _depth <= 0) &&
(isContinuous() || !_requireContinuous) &&
((dims == 2 && (((rows == 1 || cols == 1) && channels() == _elemChannels) ||
(cols == _elemChannels && channels() == 1))) ||
(dims == 3 && channels() == 1 && size.p[2] == _elemChannels && (size.p[0] == 1 || size.p[1] == 1) &&
(isContinuous() || step.p[1] == step.p[2]*size.p[2])))
? (int)(total()*channels()/_elemChannels) : -1;
2013-03-29 01:01:12 +08:00
}
2018-02-06 00:16:33 +08:00
} // cv::