📄 cxsumpixels.cpp
字号:
}
if( CV_MAT_CN(type) == 1 || coi == 0 )
{
int pass_hint = CV_MAT_DEPTH(type) == CV_32F;
if( CV_MAT_CN(type) > 4 )
CV_ERROR( CV_StsOutOfRange, "The input array must have at most 4 channels" );
if( !pass_hint )
{
CvFunc2D_1A1P func = (CvFunc2D_1A1P)(sum_tab.fn_2d[type]);
if( !func )
CV_ERROR( CV_StsBadArg, cvUnsupportedFormat );
IPPI_CALL( func( mat->data.ptr, mat_step, size, sum.val ));
}
else
{
CvFunc2D_1A1P1I func = (CvFunc2D_1A1P1I)(sum_tab.fn_2d[type]);
if( !func )
CV_ERROR( CV_StsBadArg, cvUnsupportedFormat );
IPPI_CALL( func( mat->data.ptr, mat_step, size, sum.val, cvAlgHintAccurate ));
}
}
else
{
CvFunc2DnC_1A1P func = (CvFunc2DnC_1A1P)(sumcoi_tab.fn_2d[CV_MAT_DEPTH(type)]);
if( !func )
CV_ERROR( CV_StsBadArg, cvUnsupportedFormat );
IPPI_CALL( func( mat->data.ptr, mat_step, size,
CV_MAT_CN(type), coi, sum.val ));
}
__END__;
return sum;
}
#define ICV_DEF_NONZERO_ALL( flavor, __op__, arrtype ) \
ICV_DEF_SUM_NOHINT_FUNC_2D( CountNonZero, flavor, 1, __op__, \
arrtype, int, int, int, 0 ) \
ICV_DEF_SUM_NOHINT_FUNC_2D_COI( CountNonZero, flavor, __op__, \
arrtype, int, int, int, 0 )
#undef CV_NONZERO_DBL
#define CV_NONZERO_DBL(x) (((x) & CV_BIG_INT(0x7fffffffffffffff)) != 0)
ICV_DEF_NONZERO_ALL( 8u, CV_NONZERO, uchar )
ICV_DEF_NONZERO_ALL( 16s, CV_NONZERO, ushort )
ICV_DEF_NONZERO_ALL( 32s, CV_NONZERO, int )
ICV_DEF_NONZERO_ALL( 32f, CV_NONZERO_FLT, int )
ICV_DEF_NONZERO_ALL( 64f, CV_NONZERO_DBL, int64 )
#define icvCountNonZero_8s_C1R icvCountNonZero_8u_C1R
#define icvCountNonZero_8s_CnCR icvCountNonZero_8u_CnCR
#define icvCountNonZero_16u_C1R icvCountNonZero_16s_C1R
#define icvCountNonZero_16u_CnCR icvCountNonZero_16s_CnCR
CV_DEF_INIT_FUNC_TAB_2D( CountNonZero, C1R )
CV_DEF_INIT_FUNC_TAB_2D( CountNonZero, CnCR )
CV_IMPL int
cvCountNonZero( const CvArr* arr )
{
static CvFuncTable nz_tab;
static CvFuncTable nzcoi_tab;
static int inittab = 0;
int count = 0;
CV_FUNCNAME("cvCountNonZero");
__BEGIN__;
int type, coi = 0;
int mat_step;
CvSize size;
CvMat stub, *mat = (CvMat*)arr;
if( !inittab )
{
icvInitCountNonZeroC1RTable( &nz_tab );
icvInitCountNonZeroCnCRTable( &nzcoi_tab );
inittab = 1;
}
if( !CV_IS_MAT(mat) )
{
if( CV_IS_MATND(mat) )
{
void* matnd = (void*)arr;
CvMatND nstub;
CvNArrayIterator iterator;
CvFunc2D_1A1P func;
CV_CALL( cvInitNArrayIterator( 1, &matnd, 0, &nstub, &iterator ));
type = CV_MAT_TYPE(iterator.hdr[0]->type);
if( CV_MAT_CN(type) != 1 )
CV_ERROR( CV_BadNumChannels,
"Only single-channel array are supported here" );
func = (CvFunc2D_1A1P)(nz_tab.fn_2d[CV_MAT_DEPTH(type)]);
if( !func )
CV_ERROR( CV_StsUnsupportedFormat, "" );
do
{
int temp;
IPPI_CALL( func( iterator.ptr[0], CV_STUB_STEP,
iterator.size, &temp ));
count += temp;
}
while( cvNextNArraySlice( &iterator ));
EXIT;
}
else
CV_CALL( mat = cvGetMat( mat, &stub, &coi ));
}
type = CV_MAT_TYPE(mat->type);
size = cvGetMatSize( mat );
mat_step = mat->step;
if( CV_IS_MAT_CONT( mat->type ))
{
size.width *= size.height;
size.height = 1;
mat_step = CV_STUB_STEP;
}
if( CV_MAT_CN(type) == 1 || coi == 0 )
{
CvFunc2D_1A1P func = (CvFunc2D_1A1P)(nz_tab.fn_2d[CV_MAT_DEPTH(type)]);
if( CV_MAT_CN(type) != 1 )
CV_ERROR( CV_BadNumChannels,
"The function can handle only a single channel at a time (use COI)");
if( !func )
CV_ERROR( CV_StsBadArg, cvUnsupportedFormat );
IPPI_CALL( func( mat->data.ptr, mat_step, size, &count ));
}
else
{
CvFunc2DnC_1A1P func = (CvFunc2DnC_1A1P)(nzcoi_tab.fn_2d[CV_MAT_DEPTH(type)]);
if( !func )
CV_ERROR( CV_StsBadArg, cvUnsupportedFormat );
IPPI_CALL( func( mat->data.ptr, mat_step, size, CV_MAT_CN(type), coi, &count ));
}
__END__;
return count;
}
/****************************************************************************************\
* Reduce Matrix to Vector *
\****************************************************************************************/
#define ICV_ACC_ROWS_FUNC( name, flavor, arrtype, acctype, \
__op__, load_macro ) \
static CvStatus CV_STDCALL \
icv##name##Rows_##flavor##_C1R( const arrtype* src, int srcstep,\
acctype* dst, CvSize size ) \
{ \
int i, width = size.width; \
srcstep /= sizeof(src[0]); \
\
for( i = 0; i < width; i++ ) \
dst[i] = load_macro(src[i]); \
\
for( ; --size.height; ) \
{ \
src += srcstep; \
for( i = 0; i <= width - 4; i += 4 ) \
{ \
acctype s0 = load_macro(src[i]); \
acctype s1 = load_macro(src[i+1]); \
acctype a0 = dst[i], a1 = dst[i+1]; \
a0 = (acctype)__op__(a0,s0); a1 = (acctype)__op__(a1,s1); \
dst[i] = a0; dst[i+1] = a1; \
\
s0 = load_macro(src[i+2]); \
s1 = load_macro(src[i+3]); \
a0 = dst[i+2]; a1 = dst[i+3]; \
a0 = (acctype)__op__(a0,s0); a1 = (acctype)__op__(a1,s1); \
dst[i+2] = a0; dst[i+3] = a1; \
} \
\
for( ; i < width; i++ ) \
{ \
acctype s0 = load_macro(src[i]), a0 = dst[i]; \
a0 = (acctype)__op__(a0,s0); \
dst[i] = a0; \
} \
} \
\
return CV_OK; \
}
#define ICV_ACC_COLS_FUNC_C1( name, flavor, arrtype, worktype, acctype, __op__ )\
static CvStatus CV_STDCALL \
icv##name##Cols_##flavor##_C1R( const arrtype* src, int srcstep, \
acctype* dst, int dststep, CvSize size )\
{ \
int i, width = size.width; \
srcstep /= sizeof(src[0]); \
dststep /= sizeof(dst[0]); \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
if( width == 1 ) \
dst[0] = (acctype)src[0]; \
else \
{ \
worktype a0 = src[0], a1 = src[1]; \
for( i = 2; i <= width - 4; i += 4 ) \
{ \
worktype s0 = src[i], s1 = src[i+1]; \
a0 = __op__(a0, s0); \
a1 = __op__(a1, s1); \
s0 = src[i+2]; s1 = src[i+3]; \
a0 = __op__(a0, s0); \
a1 = __op__(a1, s1); \
} \
\
for( ; i < width; i++ ) \
{ \
worktype s0 = src[i]; \
a0 = __op__(a0, s0); \
} \
a0 = __op__(a0, a1); \
dst[0] = (acctype)a0; \
} \
} \
\
return CV_OK; \
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -