cvtemplmatch.cpp.svn-base

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

SVN-BASE
1,915
字号
            {
                dst[0] = src[0];
            }
        }

        for( y = 0; y < resultSize.height; y++, imgPtr += templSize.width )
        {
            int64 res = icvCmpBlocksL2_8s_C1( imgPtr, templBuf, winLen );

            resNum[y] = res;
        }



        for( y = 0; y < resultSize.height; y++ )
        {
            pResult[x + y * resultStep] = (float) resNum[y];
        }
    }

    return CV_OK;
}


/* ----------------------------------- SqDiffNormed ----------------------------------- */

IPCVAPI_IMPL( CvStatus, icvMatchTemplate_SqDiffNormed_8s32f_C1R,
              (const char *pImage, int imageStep, CvSize roiSize,
               const char *pTemplate, int templStep, CvSize templSize,
               float *pResult, int resultStep, void *pBuffer) )
{
    char *imgBuf = 0;
    char *templBuf = 0;
    int64 *sqsumBuf = 0;
    int64 *resNum = 0;
    int64 *resDenom = 0;
    double templCoeff = 0;

    int winLen = templSize.width * templSize.height;
    CvSize resultSize = cvSize( roiSize.width - templSize.width + 1,
                                roiSize.height - templSize.height + 1 );
    int x, y;

    CvStatus result = icvMatchTemplateEntry( pImage, imageStep, roiSize,
                                             pTemplate, templStep, templSize,
                                             pResult, resultStep, pBuffer,
                                             cv8s, 0, 1,
                                             (void **) &imgBuf, (void **) &templBuf,
                                             0, (void **) &sqsumBuf,
                                             (void **) &resNum, (void **) &resDenom );

    if( result != CV_OK )
        return result;

    resultStep /= sizeof_float;

    /* calc common statistics for template and image */
    {
        const char *rowPtr = (const char *) imgBuf;
        int64 templSqsum = icvCrossCorr_8s_C1( templBuf, templBuf, winLen );


        templCoeff = (double) templSqsum;
        templCoeff = icvInvSqrt64d( fabs( templCoeff ) + FLT_EPSILON );

        for( y = 0; y < roiSize.height; y++, rowPtr += templSize.width )
        {
            sqsumBuf[y] = icvCrossCorr_8s_C1( rowPtr, rowPtr, templSize.width );
        }

    }

    /* main loop - through x coordinate of the result */
    for( x = 0; x < resultSize.width; x++ )
    {
        int64 sqsum = 0;
        char *img_ptr = imgBuf + x;

        /* update sums and image band buffer */
        if( x > 0 )
        {
            const char *src = pImage + x + templSize.width - 1;
            char *dst = img_ptr - 1;
            int out_val = dst[0];

            dst += templSize.width;

            for( y = 0; y < roiSize.height; y++, src += imageStep, dst += templSize.width )
            {
                int in_val = src[0];

                sqsumBuf[y] += (in_val - out_val) * (in_val + out_val);
                out_val = dst[0];
                dst[0] = (char) in_val;
            }
        }

        for( y = 0; y < templSize.height; y++ )
        {
            sqsum += sqsumBuf[y];
        }

        for( y = 0; y < resultSize.height; y++, img_ptr += templSize.width )
        {
            int64 res = icvCmpBlocksL2_8s_C1( img_ptr, templBuf, winLen );

            if( y > 0 )
            {
                sqsum -= sqsumBuf[y - 1];
                sqsum += sqsumBuf[y + templSize.height - 1];
            }
            resNum[y] = res;
            resDenom[y] = sqsum;
        }



        for( y = 0; y < resultSize.height; y++ )
        {
            double res = ((double) resNum[y]) * templCoeff *
                icvInvSqrt64d( fabs( (double) resDenom[y] ) + FLT_EPSILON );

            pResult[x + y * resultStep] = (float) res;
        }
    }

    return CV_OK;
}

/* -------------------------------------- Corr ---------------------------------------- */

IPCVAPI_IMPL( CvStatus, icvMatchTemplate_Corr_8s32f_C1R,
              (const char *pImage, int imageStep, CvSize roiSize,
               const char *pTemplate, int templStep, CvSize templSize,
               float *pResult, int resultStep, void *pBuffer) )
{
    char *imgBuf = 0;
    char *templBuf = 0;
    int64 *resNum = 0;
    int winLen = templSize.width * templSize.height;
    CvSize resultSize = cvSize( roiSize.width - templSize.width + 1,
                                roiSize.height - templSize.height + 1 );
    int x, y;

    CvStatus result = icvMatchTemplateEntry( pImage, imageStep, roiSize,
                                             pTemplate, templStep, templSize,
                                             pResult, resultStep, pBuffer,
                                             cv8s, 0, 0,
                                             (void **) &imgBuf, (void **) &templBuf,
                                             0, 0, (void **) &resNum, 0 );

    if( result != CV_OK )
        return result;

    resultStep /= sizeof_float;

    /* main loop - through x coordinate of the result */
    for( x = 0; x < resultSize.width; x++ )
    {
        char *imgPtr = imgBuf + x;

        /* update sums and image band buffer */
        if( x > 0 )
        {
            const char *src = pImage + x + templSize.width - 1;
            char *dst = imgPtr - 1;

            dst += templSize.width;

            for( y = 0; y < roiSize.height; y++, src += imageStep, dst += templSize.width )
            {
                dst[0] = src[0];
            }
        }

        for( y = 0; y < resultSize.height; y++, imgPtr += templSize.width )
        {
            int64 res = icvCrossCorr_8s_C1( imgPtr, templBuf, winLen );

            resNum[y] = res;
        }



        for( y = 0; y < resultSize.height; y++ )
        {
            pResult[x + y * resultStep] = (float) resNum[y];
        }
    }

    return CV_OK;
}

/* ------------------------------------ CorrNormed ------------------------------------ */

IPCVAPI_IMPL( CvStatus, icvMatchTemplate_CorrNormed_8s32f_C1R,
              (const char *pImage, int imageStep, CvSize roiSize,
               const char *pTemplate, int templStep, CvSize templSize,
               float *pResult, int resultStep, void *pBuffer) )
{
    char *imgBuf = 0;
    char *templBuf = 0;
    int64 *sqsumBuf = 0;
    int64 *resNum = 0;
    int64 *resDenom = 0;
    double templCoeff = 0;

    int winLen = templSize.width * templSize.height;
    CvSize resultSize = cvSize( roiSize.width - templSize.width + 1,
                                roiSize.height - templSize.height + 1 );
    int x, y;

    CvStatus result = icvMatchTemplateEntry( pImage, imageStep, roiSize,
                                             pTemplate, templStep, templSize,
                                             pResult, resultStep, pBuffer,
                                             cv8s, 0, 1,
                                             (void **) &imgBuf, (void **) &templBuf,
                                             0, (void **) &sqsumBuf,
                                             (void **) &resNum, (void **) &resDenom );

    if( result != CV_OK )
        return result;

    resultStep /= sizeof_float;

    /* calc common statistics for template and image */
    {
        const char *rowPtr = (const char *) imgBuf;
        int64 templSqsum = icvCrossCorr_8s_C1( templBuf, templBuf, winLen );


        templCoeff = (double) templSqsum;
        templCoeff = icvInvSqrt64d( fabs( templCoeff ) + FLT_EPSILON );

        for( y = 0; y < roiSize.height; y++, rowPtr += templSize.width )
        {
            sqsumBuf[y] = icvCrossCorr_8s_C1( rowPtr, rowPtr, templSize.width );
        }

    }

    /* main loop - through x coordinate of the result */
    for( x = 0; x < resultSize.width; x++ )
    {
        int64 sqsum = 0;
        char *imgPtr = imgBuf + x;

        /* update sums and image band buffer */
        if( x > 0 )
        {
            const char *src = pImage + x + templSize.width - 1;
            char *dst = imgPtr - 1;
            int out_val = dst[0];

            dst += templSize.width;

            for( y = 0; y < roiSize.height; y++, src += imageStep, dst += templSize.width )
            {
                int in_val = src[0];

                sqsumBuf[y] += (in_val - out_val) * (in_val + out_val);
                out_val = dst[0];
                dst[0] = (char) in_val;
            }
        }

        for( y = 0; y < templSize.height; y++ )
        {
            sqsum += sqsumBuf[y];
        }

        for( y = 0; y < resultSize.height; y++, imgPtr += templSize.width )
        {
            int64 res = icvCrossCorr_8s_C1( imgPtr, templBuf, winLen );

            if( y > 0 )
            {
                sqsum -= sqsumBuf[y - 1];
                sqsum += sqsumBuf[y + templSize.height - 1];
            }
            resNum[y] = res;
            resDenom[y] = sqsum;
        }



        for( y = 0; y < resultSize.height; y++ )
        {
            double res = ((double) resNum[y]) * templCoeff *
                icvInvSqrt64d( fabs( (double) resDenom[y] ) + FLT_EPSILON );

            pResult[x + y * resultStep] = (float) res;
        }
    }

    return CV_OK;
}


/* -------------------------------------- Coeff --------------------------------------- */

IPCVAPI_IMPL( CvStatus, icvMatchTemplate_Coeff_8s32f_C1R,
              (const char *pImage, int imageStep, CvSize roiSize,
               const char *pTemplate, int templStep, CvSize templSize,
               float *pResult, int resultStep, void *pBuffer) )
{
    char *imgBuf = 0;
    char *templBuf = 0;
    int64 *resNum = 0;
    int64 *resDenom = 0;
    int64 *sumBuf = 0;
    int winLen = templSize.width * templSize.height;
    CvSize resultSize = cvSize( roiSize.width - templSize.width + 1,
                                roiSize.height - templSize.height + 1 );
    int64 templSum = 0;
    double winCoeff = 1. / (winLen + DBL_EPSILON);
    int x, y;

    CvStatus result = icvMatchTemplateEntry( pImage, imageStep, roiSize,
                                             pTemplate, templStep, templSize,
                                             pResult, resultStep, pBuffer,
                                             cv8s, 1, 0,
                                             (void **) &imgBuf, (void **) &templBuf,
                                             (void **) &sumBuf, 0,
                                             (void **) &resNum, (void **) &resDenom );

    if( result != CV_OK )
        return result;

    resultStep /= sizeof_float;

    /* calc common statistics for template and image */
    {
        const char *rowPtr = (const char *) imgBuf;

        templSum = icvSumPixels_8s_C1( templBuf, winLen );

        for( y = 0; y < roiSize.height; y++, rowPtr += templSize.width )
        {
            sumBuf[y] = icvSumPixels_8s_C1( rowPtr, templSize.width );
        }

    }

    /* main loop - through x coordinate of the result */
    for( x = 0; x < resultSize.width; x++ )
    {
        char *imgPtr = imgBuf + x;
        int64 sum = 0;

        /* update sums and image band buffer */
        if( x > 0 )
        {
            const char *src = pImage + x + templSize.width - 1;
            char *dst = imgPtr - 1;
            int out_val = dst[0];

            dst += templSize.width;

            for( y = 0; y < roiSize.height; y++, src += imageStep, dst += templSize.width )
            {
                int in_val = src[0];

                sumBuf[y] += in_val - out_val;
                out_val = dst[0];
                dst[0] = (char) in_val;
            }
        }

        for( y = 0; y < templSize.height; y++ )
        {
            sum += sumBuf[y];
        }

        for( y = 0; y < resultSize.height; y++, imgPtr += templSize.width )
        {
            int64 res = icvCrossCorr_8s_C1( imgPtr, templBuf, winLen );

            if( y > 0 )
            {
                sum -= sumBuf[y - 1];
                sum += sumBuf[y + templSize.height - 1];
            }

⌨️ 快捷键说明

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