a big patch; use special proxy types (Input/OutputArray, Input/OutputArrayOfArrays) for passing in vectors, matrices etc.

This commit is contained in:
Vadim Pisarevsky
2011-04-17 13:14:45 +00:00
parent 335370a7c0
commit abeeb40d46
94 changed files with 10831 additions and 9631 deletions

View File

@@ -11,7 +11,7 @@
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
@@ -52,18 +52,12 @@ namespace cv
{
template<typename T> static void
copyMask_(const Mat& srcmat, Mat& dstmat, const Mat& maskmat)
copyMask_(const uchar* _src, size_t sstep, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size)
{
const uchar* mask = maskmat.data;
size_t sstep = srcmat.step;
size_t dstep = dstmat.step;
size_t mstep = maskmat.step;
Size size = getContinuousSize(srcmat, dstmat, maskmat);
for( int y = 0; y < size.height; y++, mask += mstep )
for( ; size.height--; mask += mstep, _src += sstep, _dst += dstep )
{
const T* src = (const T*)(srcmat.data + sstep*y);
T* dst = (T*)(dstmat.data + dstep*y);
const T* src = (const T*)_src;
T* dst = (T*)_dst;
int x = 0;
for( ; x <= size.width - 4; x += 4 )
{
@@ -82,389 +76,397 @@ copyMask_(const Mat& srcmat, Mat& dstmat, const Mat& maskmat)
}
}
template<typename T> static void
setMask_(const void* _scalar, Mat& dstmat, const Mat& maskmat)
static void
copyMaskGeneric(const uchar* _src, size_t sstep, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size, void* _esz)
{
T scalar = *(T*)_scalar;
const uchar* mask = maskmat.data;
size_t dstep = dstmat.step;
size_t mstep = maskmat.step;
Size size = dstmat.size();
if( dstmat.isContinuous() && maskmat.isContinuous() )
size_t k, esz = *(size_t*)_esz;
for( ; size.height--; mask += mstep, _src += sstep, _dst += dstep )
{
size.width *= size.height;
size.height = 1;
const uchar* src = _src;
uchar* dst = _dst;
int x = 0;
for( ; x < size.width; x++, src += esz, dst += esz )
{
if( !mask[x] )
continue;
for( k = 0; k < esz; k++ )
dst[k] = src[k];
}
}
for( int y = 0; y < size.height; y++, mask += mstep )
}
template<typename T> static void
setMask_(T value, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size)
{
for( ; size.height--; mask += mstep, _dst += dstep )
{
T* dst = (T*)(dstmat.data + dstep*y);
T* dst = (T*)_dst;
int x = 0;
for( ; x <= size.width - 4; x += 4 )
{
if( mask[x] )
dst[x] = scalar;
dst[x] = value;
if( mask[x+1] )
dst[x+1] = scalar;
dst[x+1] = value;
if( mask[x+2] )
dst[x+2] = scalar;
dst[x+2] = value;
if( mask[x+3] )
dst[x+3] = scalar;
dst[x+3] = value;
}
for( ; x < size.width; x++ )
if( mask[x] )
dst[x] = scalar;
dst[x] = value;
}
}
typedef void (*SetMaskFunc)(const void* scalar, Mat& dst, const Mat& mask);
CopyMaskFunc g_copyMaskFuncTab[] =
static void
setMaskGeneric(const uchar* value, size_t, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size, void* _esz)
{
size_t k, esz = *(size_t*)_esz;
for( ; size.height--; mask += mstep, _dst += dstep )
{
uchar* dst = _dst;
int x = 0;
for( ; x < size.width; x++, dst += esz )
{
if( !mask[x] )
continue;
for( k = 0; k < esz; k++ )
dst[k] = value[k];
}
}
}
#define DEF_COPY_SET_MASK(suffix, type) \
static void copyMask##suffix(const uchar* src, size_t sstep, const uchar* mask, size_t mstep, \
uchar* dst, size_t dstep, Size size, void*) \
{ \
copyMask_<type>(src, sstep, mask, mstep, dst, dstep, size); \
} \
static void setMask##suffix( const uchar* src, size_t, const uchar* mask, size_t mstep, \
uchar* dst, size_t dstep, Size size, void*) \
{ \
setMask_<type>(*(const type*)src, mask, mstep, dst, dstep, size); \
}
DEF_COPY_SET_MASK(8u, uchar);
DEF_COPY_SET_MASK(16u, ushort);
DEF_COPY_SET_MASK(8uC3, Vec3b);
DEF_COPY_SET_MASK(32s, int);
DEF_COPY_SET_MASK(16uC3, Vec3s);
DEF_COPY_SET_MASK(32sC2, Vec2i);
DEF_COPY_SET_MASK(32sC3, Vec3i);
DEF_COPY_SET_MASK(32sC4, Vec4i);
DEF_COPY_SET_MASK(32sC6, Vec6i);
DEF_COPY_SET_MASK(32sC8, Vec8i);
BinaryFunc copyMaskTab[] =
{
0,
copyMask_<uchar>, // 1
copyMask_<ushort>, // 2
copyMask_<Vec<uchar,3> >, // 3
copyMask_<int>, // 4
copyMask8u,
copyMask16u,
copyMask8uC3,
copyMask32s,
0,
copyMask_<Vec<ushort,3> >, // 6
copyMask16uC3,
0,
copyMask_<Vec<int,2> >, // 8
copyMask32sC2,
0, 0, 0,
copyMask_<Vec<int,3> >, // 12
copyMask32sC3,
0, 0, 0,
copyMask_<Vec<int,4> >, // 16
copyMask32sC4,
0, 0, 0, 0, 0, 0, 0,
copyMask_<Vec<int,6> >, // 24
copyMask32sC6,
0, 0, 0, 0, 0, 0, 0,
copyMask_<Vec<int,8> > // 32
copyMask32sC8
};
static SetMaskFunc setMaskFuncTab[] =
BinaryFunc setMaskTab[] =
{
0,
setMask_<uchar>, // 1
setMask_<ushort>, // 2
setMask_<Vec<uchar,3> >, // 3
setMask_<int>, // 4
setMask8u,
setMask16u,
setMask8uC3,
setMask32s,
0,
setMask_<Vec<ushort,3> >, // 6
setMask16uC3,
0,
setMask_<Vec<int,2> >, // 8
setMask32sC2,
0, 0, 0,
setMask_<Vec<int,3> >, // 12
setMask32sC3,
0, 0, 0,
setMask_<Vec<int,4> >, // 16
setMask32sC4,
0, 0, 0, 0, 0, 0, 0,
setMask_<Vec<int,6> >, // 24
setMask32sC6,
0, 0, 0, 0, 0, 0, 0,
setMask_<Vec<int,8> > // 32
};
setMask32sC8
};
BinaryFunc getCopyMaskFunc(size_t esz)
{
return esz <= 32 && copyMaskTab[esz] ? copyMaskTab[esz] : copyMaskGeneric;
}
/* dst = src */
void Mat::copyTo( Mat& dst ) const
void Mat::copyTo( OutputArray _dst ) const
{
if( data == dst.data && data != 0 )
return;
if( dims > 2 )
int dtype = _dst.type();
if( _dst.fixedType() && dtype != type() )
{
dst.create( dims, size, type() );
if( total() != 0 )
{
const Mat* arrays[] = { this, &dst, 0 };
Mat planes[2];
NAryMatIterator it(arrays, planes);
CV_DbgAssert(it.planes[0].isContinuous() &&
it.planes[1].isContinuous());
size_t planeSize = it.planes[0].elemSize()*it.planes[0].rows*it.planes[0].cols;
convertTo( _dst, dtype );
return;
}
if( empty() )
{
_dst.release();
return;
}
if( dims <= 2 )
{
_dst.create( rows, cols, type() );
Mat dst = _dst.getMat();
if( data == dst.data )
return;
for( int i = 0; i < it.nplanes; i++, ++it )
memcpy(it.planes[1].data, it.planes[0].data, planeSize);
if( rows > 0 && cols > 0 )
{
const uchar* sptr = data;
uchar* dptr = dst.data;
Size sz = getContinuousSize(*this, dst, (int)elemSize());
for( ; sz.height--; sptr += step, dptr += dst.step )
memcpy( dptr, sptr, sz.width );
}
return;
}
dst.create( rows, cols, type() );
Size sz = size();
_dst.create( dims, size, type() );
Mat dst = _dst.getMat();
if( data == dst.data )
return;
if( rows > 0 && cols > 0 )
if( total() != 0 )
{
const uchar* sptr = data;
uchar* dptr = dst.data;
size_t width = sz.width*elemSize();
if( isContinuous() && dst.isContinuous() )
{
width *= sz.height;
sz.height = 1;
}
for( ; sz.height--; sptr += step, dptr += dst.step )
memcpy( dptr, sptr, width );
const Mat* arrays[] = { this, &dst };
uchar* ptrs[2];
NAryMatIterator it(arrays, ptrs, 2);
size_t size = it.size*elemSize();
for( size_t i = 0; i < it.nplanes; i++, ++it )
memcpy(ptrs[1], ptrs[0], size);
}
}
void Mat::copyTo( Mat& dst, const Mat& mask ) const
void Mat::copyTo( OutputArray _dst, const InputArray& _mask ) const
{
Mat mask = _mask.getMat();
if( !mask.data )
{
copyTo(dst);
copyTo(_dst);
return;
}
if( dims > 2 )
{
dst.create( dims, size, type() );
const Mat* arrays[] = { this, &dst, &mask, 0 };
Mat planes[3];
NAryMatIterator it(arrays, planes);
for( int i = 0; i < it.nplanes; i++, ++it )
it.planes[0].copyTo(it.planes[1], it.planes[2]);
return;
}
uchar* data0 = dst.data;
dst.create( size(), type() );
CV_Assert( mask.type() == CV_8U );
size_t esz = elemSize();
BinaryFunc copymask = getCopyMaskFunc(esz);
uchar* data0 = _dst.getMat().data;
_dst.create( dims, size, type() );
Mat dst = _dst.getMat();
if( dst.data != data0 ) // do not leave dst uninitialized
dst = Scalar(0);
getCopyMaskFunc((int)elemSize())(*this, dst, mask);
if( dims <= 2 )
{
Size sz = getContinuousSize(*this, dst, mask);
copymask(data, step, mask.data, mask.step, dst.data, dst.step, sz, &esz);
return;
}
const Mat* arrays[] = { this, &dst, &mask, 0 };
uchar* ptrs[3];
NAryMatIterator it(arrays, ptrs);
Size sz((int)it.size, 1);
for( size_t i = 0; i < it.nplanes; i++, ++it )
copymask(ptrs[0], 0, ptrs[2], 0, ptrs[1], 0, sz, &esz);
}
Mat& Mat::operator = (const Scalar& s)
{
if( dims > 2 )
{
const Mat* arrays[] = { this, 0 };
Mat planes[1];
NAryMatIterator it(arrays, planes);
for( int i = 0; i < it.nplanes; i++, ++it )
it.planes[0] = s;
return *this;
}
Size sz = size();
uchar* dst = data;
sz.width *= (int)elemSize();
if( isContinuous() )
{
sz.width *= sz.height;
sz.height = 1;
}
const Mat* arrays[] = { this };
uchar* ptr;
NAryMatIterator it(arrays, &ptr, 1);
size_t size = it.size*elemSize();
if( s[0] == 0 && s[1] == 0 && s[2] == 0 && s[3] == 0 )
{
for( ; sz.height--; dst += step )
memset( dst, 0, sz.width );
for( size_t i = 0; i < it.nplanes; i++, ++it )
memset( ptr, 0, size );
}
else
{
int t = type(), esz1 = (int)elemSize1();
double scalar[12];
scalarToRawData(s, scalar, t, 12);
int copy_len = 12*esz1;
uchar* dst_limit = dst + sz.width;
if( sz.height-- )
if( it.nplanes > 0 )
{
while( dst + copy_len <= dst_limit )
double scalar[12];
scalarToRawData(s, scalar, type(), 12);
size_t blockSize = 12*elemSize1();
for( size_t j = 0; j < size; j += blockSize )
{
memcpy( dst, scalar, copy_len );
dst += copy_len;
size_t sz = std::min(blockSize, size - j);
memcpy( ptr + j, scalar, sz );
}
memcpy( dst, scalar, dst_limit - dst );
}
if( sz.height > 0 )
for( size_t i = 1; i < it.nplanes; i++ )
{
dst = dst_limit - sz.width + step;
for( ; sz.height--; dst += step )
memcpy( dst, data, sz.width );
++it;
memcpy( ptr, data, size );
}
}
return *this;
}
Mat& Mat::setTo(const Scalar& s, const Mat& mask)
Mat& Mat::setTo(const Scalar& s, const InputArray& _mask)
{
Mat mask = _mask.getMat();
if( !mask.data )
*this = s;
else
{
CV_Assert( channels() <= 4 );
SetMaskFunc func = setMaskFuncTab[elemSize()];
CV_Assert( func != 0 );
CV_Assert( channels() <= 4 && mask.type() == CV_8U );
size_t esz = elemSize();
BinaryFunc func = esz <= 32 ? setMaskTab[esz] : setMaskGeneric;
double buf[4];
scalarToRawData(s, buf, type(), 0);
if( dims > 2 )
{
const Mat* arrays[] = { this, &mask, 0 };
Mat planes[2];
NAryMatIterator it(arrays, planes);
for( int i = 0; i < it.nplanes; i++, ++it )
func(buf, it.planes[0], it.planes[1]);
}
else
func(buf, *this, mask);
const Mat* arrays[] = { this, &mask, 0 };
uchar* ptrs[2];
NAryMatIterator it(arrays, ptrs);
Size sz((int)it.size, 1);
for( size_t i = 0; i < it.nplanes; i++, ++it )
func((const uchar*)buf, 0, ptrs[1], 0, ptrs[0], 0, sz, &esz);
}
return *this;
}
template<typename T> static void
flipHoriz_( const Mat& srcmat, Mat& dstmat, bool flipv )
static void
flipHoriz( const uchar* src, size_t sstep, uchar* dst, size_t dstep, Size size, size_t esz )
{
uchar* dst0 = dstmat.data;
size_t srcstep = srcmat.step;
int dststep = (int)dstmat.step;
Size size = srcmat.size();
int i, j, limit = ((size.width + 1)/2)*esz;
AutoBuffer<int> _tab(size.width*esz);
int* tab = _tab;
for( i = 0; i < size.width; i++ )
for( size_t k = 0; k < esz; k++ )
tab[i*esz + k] = (size.width - i - 1)*esz + k;
if( flipv )
for( ; size.height--; src += sstep, dst += dstep )
{
dst0 += (size.height - 1)*dststep;
dststep = -dststep;
}
for( int y = 0; y < size.height; y++ )
{
const T* src = (const T*)(srcmat.data + srcstep*y);
T* dst = (T*)(dst0 + dststep*y);
for( int i = 0; i < (size.width + 1)/2; i++ )
for( i = 0; i < limit; i++ )
{
T t0 = src[i], t1 = src[size.width - i - 1];
dst[i] = t1; dst[size.width - i - 1] = t0;
j = tab[i];
uchar t0 = src[i], t1 = src[j];
dst[i] = t1; dst[j] = t0;
}
}
}
typedef void (*FlipHorizFunc)( const Mat& src, Mat& dst, bool flipv );
static void
flipVert( const Mat& srcmat, Mat& dstmat )
flipVert( const uchar* src0, size_t sstep, uchar* dst0, size_t dstep, Size size, size_t esz )
{
const uchar* src = srcmat.data;
uchar* dst = dstmat.data;
size_t srcstep = srcmat.step, dststep = dstmat.step;
Size size = srcmat.size();
const uchar* src1 = src + (size.height - 1)*srcstep;
uchar* dst1 = dst + (size.height - 1)*dststep;
size.width *= (int)srcmat.elemSize();
const uchar* src1 = src0 + (size.height - 1)*sstep;
uchar* dst1 = dst0 + (size.height - 1)*dstep;
size.width *= (int)esz;
for( int y = 0; y < (size.height + 1)/2; y++, src += srcstep, src1 -= srcstep,
dst += dststep, dst1 -= dststep )
for( int y = 0; y < (size.height + 1)/2; y++, src0 += sstep, src1 -= sstep,
dst0 += dstep, dst1 -= dstep )
{
int i = 0;
if( ((size_t)(src)|(size_t)(dst)|(size_t)src1|(size_t)dst1) % sizeof(int) == 0 )
if( ((size_t)src0|(size_t)dst0|(size_t)src1|(size_t)dst1) % sizeof(int) == 0 )
{
for( ; i <= size.width - 16; i += 16 )
{
int t0 = ((int*)(src + i))[0];
int t0 = ((int*)(src0 + i))[0];
int t1 = ((int*)(src1 + i))[0];
((int*)(dst + i))[0] = t1;
((int*)(dst0 + i))[0] = t1;
((int*)(dst1 + i))[0] = t0;
t0 = ((int*)(src + i))[1];
t0 = ((int*)(src0 + i))[1];
t1 = ((int*)(src1 + i))[1];
((int*)(dst + i))[1] = t1;
((int*)(dst0 + i))[1] = t1;
((int*)(dst1 + i))[1] = t0;
t0 = ((int*)(src + i))[2];
t0 = ((int*)(src0 + i))[2];
t1 = ((int*)(src1 + i))[2];
((int*)(dst + i))[2] = t1;
((int*)(dst0 + i))[2] = t1;
((int*)(dst1 + i))[2] = t0;
t0 = ((int*)(src + i))[3];
t0 = ((int*)(src0 + i))[3];
t1 = ((int*)(src1 + i))[3];
((int*)(dst + i))[3] = t1;
((int*)(dst0 + i))[3] = t1;
((int*)(dst1 + i))[3] = t0;
}
for( ; i <= size.width - 4; i += 4 )
{
int t0 = ((int*)(src + i))[0];
int t0 = ((int*)(src0 + i))[0];
int t1 = ((int*)(src1 + i))[0];
((int*)(dst + i))[0] = t1;
((int*)(dst0 + i))[0] = t1;
((int*)(dst1 + i))[0] = t0;
}
}
for( ; i < size.width; i++ )
{
uchar t0 = src[i];
uchar t0 = src0[i];
uchar t1 = src1[i];
dst[i] = t1;
dst0[i] = t1;
dst1[i] = t0;
}
}
}
void flip( const Mat& src, Mat& dst, int flip_mode )
void flip( const InputArray& _src, OutputArray _dst, int flip_mode )
{
static FlipHorizFunc tab[] =
{
0,
flipHoriz_<uchar>, // 1
flipHoriz_<ushort>, // 2
flipHoriz_<Vec<uchar,3> >, // 3
flipHoriz_<int>, // 4
0,
flipHoriz_<Vec<ushort,3> >, // 6
0,
flipHoriz_<Vec<int,2> >, // 8
0, 0, 0,
flipHoriz_<Vec<int,3> >, // 12
0, 0, 0,
flipHoriz_<Vec<int,4> >, // 16
0, 0, 0, 0, 0, 0, 0,
flipHoriz_<Vec<int,6> >, // 24
0, 0, 0, 0, 0, 0, 0,
flipHoriz_<Vec<int,8> > // 32
};
Mat src = _src.getMat();
CV_Assert( src.dims <= 2 );
dst.create( src.size(), src.type() );
_dst.create( src.size(), src.type() );
Mat dst = _dst.getMat();
size_t esz = src.elemSize();
if( flip_mode == 0 )
flipVert( src, dst );
if( flip_mode <= 0 )
flipVert( src.data, src.step, dst.data, dst.step, src.size(), esz );
else
{
int esz = (int)src.elemSize();
CV_Assert( esz <= 32 );
FlipHorizFunc func = tab[esz];
CV_Assert( func != 0 );
if( flip_mode > 0 )
func( src, dst, false );
else if( src.data != dst.data )
func( src, dst, true );
else
{
func( dst, dst, false );
flipVert( dst, dst );
}
}
flipHoriz( src.data, src.step, dst.data, dst.step, src.size(), esz );
if( flip_mode < 0 )
flipHoriz( dst.data, dst.step, dst.data, dst.step, dst.size(), esz );
}
void repeat(const Mat& src, int ny, int nx, Mat& dst)
void repeat(const InputArray& _src, int ny, int nx, OutputArray _dst)
{
Mat src = _src.getMat();
CV_Assert( src.dims <= 2 );
dst.create(src.rows*ny, src.cols*nx, src.type());
_dst.create(src.rows*ny, src.cols*nx, src.type());
Mat dst = _dst.getMat();
Size ssize = src.size(), dsize = dst.size();
int esz = (int)src.elemSize();
int x, y;
@@ -524,7 +526,7 @@ cvCopy( const void* srcarr, void* dstarr, const void* maskarr )
{
CvSparseNode* node_copy = (CvSparseNode*)cvSetNew( dst1->heap );
int tabidx = node->hashval & (dst1->hashsize - 1);
CV_MEMCPY_AUTO( node_copy, node, dst1->heap->elem_size );
memcpy( node_copy, node, dst1->heap->elem_size );
node_copy->next = (CvSparseNode*)dst1->hashtable[tabidx];
dst1->hashtable[tabidx] = node_copy;
}