cvsumpixels.cpp.svn-base

来自「非结构化路识别」· SVN-BASE 代码 · 共 803 行 · 第 1/3 页

SVN-BASE
803
字号
    static CvBigFuncTable sum_tab;
    static CvFuncTable sumcoi_tab;
    static int inittab = 0;

    CvScalar sum = {{ 0, 0, 0, 0 }};

    CV_FUNCNAME("cvSum");

    __BEGIN__;

    int type, coi = 0;
    int mat_step;
    CvSize size;
    CvMat stub, *mat = (CvMat*)arr;

    if( !inittab )
    {
        icvInitSumRTable( &sum_tab );
        icvInitSumCnCRTable( &sumcoi_tab );
        inittab = 1;
    }

    if( !CV_IS_MAT(mat) )
    {
        if( CV_IS_MATND(mat) )
        {
            CvMatND stub;
            CvMatNDIterator iterator;
            CvFunc2D_1A1P func;

            CV_CALL( icvPrepareArrayOp( 1, (void**)&mat, 0, &stub, &iterator ));

            type = CV_MAT_TYPE(iterator.hdr[0]->type);
            func = (CvFunc2D_1A1P)(sum_tab.fn_2d[type]);
            if( !func )
                CV_ERROR( CV_StsUnsupportedFormat, "" );
       
            do
            {
                CvScalar temp = {{ 0, 0, 0, 0 }};
                IPPI_CALL( func( iterator.ptr[0], CV_STUB_STEP,
                                 iterator.size, temp.val ));
                sum.val[0] += temp.val[0];
                sum.val[1] += temp.val[1];
                sum.val[2] += temp.val[2];
                sum.val[3] += temp.val[3];
            }
            while( icvNextMatNDSlice( &iterator ));
            EXIT;
        }
        else
            CV_CALL( mat = cvGetMat( mat, &stub, &coi ));
    }

    type = CV_MAT_TYPE(mat->type);
    size = icvGetMatSize( mat );

    mat_step = mat->step;

    if( CV_IS_MAT_CONT( mat->type ))
    {
        size.width *= size.height;
        
        if( size.width <= CV_MAX_INLINE_MAT_OP_SIZE )
        {
            if( type == CV_32FC1 )
            {
                float* data = mat->data.fl;

                do
                {
                    sum.val[0] += data[size.width - 1];
                }
                while( --size.width );

                EXIT;
            }

            if( type == CV_64FC1 )
            {
                double* data = mat->data.db;

                do
                {
                    sum.val[0] += data[size.width - 1];
                }
                while( --size.width );

                EXIT;
            }
        }
        size.height = 1;
        mat_step = CV_STUB_STEP;
    }

    if( CV_MAT_CN(type) == 1 || coi == 0 )
    {
        CvFunc2D_1A1P func = (CvFunc2D_1A1P)(sum_tab.fn_2d[type]);

        if( !func )
            CV_ERROR( CV_StsBadArg, icvUnsupportedFormat );

        IPPI_CALL( func( mat->data.ptr, mat_step, size, sum.val ));
    }
    else
    {
        CvFunc2DnC_1A1P func = (CvFunc2DnC_1A1P)(sumcoi_tab.fn_2d[CV_MAT_DEPTH(type)]);

        if( !func )
            CV_ERROR( CV_StsBadArg, icvUnsupportedFormat );

        IPPI_CALL( func( mat->data.ptr, mat_step, size,
                         CV_MAT_CN(type), coi, sum.val ));
    }

    __END__;

    return  sum;
}


#define ICV_DEF_SUM_C1( __op__, name, flavor, srctype, sumtype )        \
    ICV_DEF_SUM_FUNC_2D( __op__, CV_SUM_ENTRY_C1, CV_SUM_EXIT_C1,       \
                         name, flavor, 1, srctype, sumtype, int )       \
    ICV_DEF_SUM_FUNC_2D_COI( __op__, name, flavor, srctype, sumtype, int )

ICV_DEF_SUM_C1( CV_NONZERO, CountNonZero, 8u, uchar, int )
ICV_DEF_SUM_C1( CV_NONZERO, CountNonZero, 16s, ushort, int )
ICV_DEF_SUM_C1( CV_NONZERO, CountNonZero, 32s, int, int )
ICV_DEF_SUM_C1( CV_NONZERO_FLT, CountNonZero, 32f, int, int )
ICV_DEF_SUM_C1( CV_NONZERO_FLT, CountNonZero, 64f, int64, int )

#define icvCountNonZero_8s_C1R icvCountNonZero_8u_C1R
#define icvCountNonZero_8s_CnCR icvCountNonZero_8u_CnCR

CV_DEF_INIT_FUNC_TAB_2D( CountNonZero, C1R )
CV_DEF_INIT_FUNC_TAB_2D( CountNonZero, CnCR )


CV_IMPL int
cvCountNonZero( const CvArr* img )
{
    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*)img;

    if( !inittab )
    {
        icvInitCountNonZeroC1RTable( &nz_tab );
        icvInitCountNonZeroCnCRTable( &nzcoi_tab );
        inittab = 1;
    }

    if( !CV_IS_MAT(mat) )
    {
        if( CV_IS_MATND(mat) )
        {
            CvMatND stub;
            CvMatNDIterator iterator;
            CvFunc2D_1A1P func;

            CV_CALL( icvPrepareArrayOp( 1, (void**)&mat, 0, &stub, &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( icvNextMatNDSlice( &iterator ));
            EXIT;
        }
        else
            CV_CALL( mat = cvGetMat( mat, &stub, &coi ));
    }

    type = CV_MAT_TYPE(mat->type);
    size = icvGetMatSize( 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, icvUnsupportedFormat );

        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, icvUnsupportedFormat );

        IPPI_CALL( func( mat->data.ptr, mat_step, size, CV_MAT_CN(type), coi, &count ));
    }

    __END__;

    return  count;
}


#define ICV_DEF_INTEGRAL_OP( flavor, arrtype, sumtype, sqsumtype, worktype,     \
                             cast_macro, cast_sqr_macro )                       \
IPCVAPI( CvStatus,                                                              \
    icvIntegralImage_##flavor##_C1R,( const arrtype* src, int srcstep,          \
                                    sumtype* sum, int sumstep,                  \
                                    sqsumtype* sqsum, int sqsumstep,            \
                                    sumtype* tilted, int tiltedstep,            \
                                    CvSize size ))                              \
                                                                                \
IPCVAPI_IMPL( CvStatus,                                                         \
    icvIntegralImage_##flavor##_C1R,( const arrtype* src, int srcstep,          \
                                    sumtype* sum, int sumstep,                  \
                                    sqsumtype* sqsum, int sqsumstep,            \
                                    sumtype* tilted, int tiltedstep,            \
                                    CvSize size ))                              \
{                                                                               \
    int x, y;                                                                   \
    sumtype s;                                                                  \
    sqsumtype sq;                                                               \
    sumtype* buf = 0;                                                           \
                                                                                \
    srcstep /= sizeof(src[0]);                                                  \
                                                                                \
    memset( sum, 0, (size.width+1)*sizeof(sum[0]));                             \
    sumstep /= sizeof(sum[0]);                                                  \
    sum += sumstep + 1;                                                         \
                                                                                \
    if( sqsum )                                                                 \
    {                                                                           \

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?