cvtemplmatch.cpp.svn-base

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

SVN-BASE
1,915
字号
            resNum[y] = res;
            resDenom[y] = sum;
        }



        for( y = 0; y < resultSize.height; y++ )
        {
            double res = ((double) resNum[y]) - winCoeff * templSum * ((double) resDenom[y]);

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

    return CV_OK;
}


/* ------------------------------------ CoeffNormed ----------------------------------- */

IPCVAPI_IMPL( CvStatus, icvMatchTemplate_CoeffNormed_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 *sumBuf = 0;
    int64 *sqsumBuf = 0;
    int64 *resNum = 0;
    int64 *resDenom = 0;
    int64 templSum = 0;
    double templCoeff = 0;

    int winLen = templSize.width * templSize.height;
    double winCoeff = 1. / (winLen + DBL_EPSILON);

    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, 1, 1,
                                             (void **) &imgBuf, (void **) &templBuf,
                                             (void **) &sumBuf, (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 );

        templSum = icvSumPixels_8s_C1( templBuf, winLen );


        templCoeff = (double) templSqsum - ((double) templSum) * templSum * winCoeff;
        templCoeff = icvInvSqrt64d( fabs( templCoeff ) + FLT_EPSILON );

        for( y = 0; y < roiSize.height; y++, rowPtr += templSize.width )
        {
            sumBuf[y] = icvSumPixels_8s_C1( 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 sum = 0;
        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];

                sumBuf[y] += in_val - out_val;
                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++ )
        {
            sum += sumBuf[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 )
            {
                sum -= sumBuf[y - 1];
                sum += sumBuf[y + templSize.height - 1];
                sqsum -= sqsumBuf[y - 1];
                sqsum += sqsumBuf[y + templSize.height - 1];
            }
            resNum[y] = res;
            resDenom[y] = sum;
            resDenom[y + resultSize.height] = sqsum;
        }



        for( y = 0; y < resultSize.height; y++ )
        {
            double sum = ((double) resDenom[y]);
            double wsum = winCoeff * sum;
            double res = ((double) resNum[y]) - wsum * templSum;
            double nrm_s = ((double) resDenom[y + resultSize.height]) - wsum * sum;

            res *= templCoeff * icvInvSqrt64d( fabs( nrm_s ) + FLT_EPSILON );
            pResult[x + y * resultStep] = (float) res;
        }
    }

    return CV_OK;
}


/****************************************************************************************\
*                                      32f flavor                                        *
\****************************************************************************************/

/* --------------------------------------- SqDiff ------------------------------------- */

IPCVAPI_IMPL( CvStatus, icvMatchTemplate_SqDiff_32f_C1R,
              (const float *pImage, int imageStep, CvSize roiSize,
               const float *pTemplate, int templStep, CvSize templSize,
               float *pResult, int resultStep, void *pBuffer) )
{
    float *imgBuf = 0;
    float *templBuf = 0;
    double *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,
                                             cv32f, 0, 0,
                                             (void **) &imgBuf, (void **) &templBuf,
                                             0, 0, (void **) &resNum, 0 );

    if( result != CV_OK )
        return result;

    imageStep /= sizeof_float;
    templStep /= sizeof_float;
    resultStep /= sizeof_float;

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

        /* update sums and image band buffer */
        if( x > 0 )
        {
            const float *src = pImage + x + templSize.width - 1;
            float *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 )
        {
            double res = icvCmpBlocksL2_32f_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_32f_C1R,
              (const float *pImage, int imageStep, CvSize roiSize,
               const float *pTemplate, int templStep, CvSize templSize,
               float *pResult, int resultStep, void *pBuffer) )
{
    float *imgBuf = 0;
    float *templBuf = 0;
    double *sqsumBuf = 0;
    double *resNum = 0;
    double *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,
                                             cv32f, 0, 1,
                                             (void **) &imgBuf, (void **) &templBuf,
                                             0, (void **) &sqsumBuf,
                                             (void **) &resNum, (void **) &resDenom );

    if( result != CV_OK )
        return result;

    imageStep /= sizeof_float;
    templStep /= sizeof_float;
    resultStep /= sizeof_float;

    /* calc common statistics for template and image */
    {
        const float *rowPtr = (const float *) imgBuf;
        double templSqsum = icvCrossCorr_32f_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_32f_C1( rowPtr, rowPtr, templSize.width );
        }

    }

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

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

            dst += templSize.width;

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

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

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

        for( y = 0; y < resultSize.height; y++, img_ptr += templSize.width )
        {
            double res = icvCmpBlocksL2_32f_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_32f_C1R,
              (const float *pImage, int imageStep, CvSize roiSize,
               const float *pTemplate, int templStep, CvSize templSize,
               float *pResult, int resultStep, void *pBuffer) )
{
    float *imgBuf = 0;
    float *templBuf = 0;
    double *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,
                                             cv32f, 0, 0,
                                             (void **) &imgBuf, (void **) &templBuf,
                                             0, 0, (void **) &resNum, 0 );

    if( result != CV_OK )
        return result;

    imageStep /= sizeof_float;
    templStep /= sizeof_float;
    resultStep /= sizeof_float;

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

        /* update sums and image band buffer */
        if( x > 0 )
        {
            const float *src = pImage + x + templSize.width - 1;
            float *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 )
        {
            double res = icvCrossCorr_32f_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_32f_C1R,
              (const float *pImage, int imageStep, CvSize roiSize,
               const float *pTemplate, int templStep, CvSize templSize,

⌨️ 快捷键说明

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