cvtemplmatch.cpp.svn-base
来自「非结构化路识别」· SVN-BASE 代码 · 共 1,915 行 · 第 1/5 页
SVN-BASE
1,915 行
{
pResult[x + y * resultStep] = (float) resNum[y];
}
}
return CV_OK;
}
/* ------------------------------------ CorrNormed ------------------------------------ */
IPCVAPI_IMPL( CvStatus, icvMatchTemplate_CorrNormed_8u32f_C1R,
(const uchar * pImage, int imageStep, CvSize roiSize,
const uchar * pTemplate, int templStep, CvSize templSize,
float *pResult, int resultStep, void *pBuffer) )
{
uchar *imgBuf = 0;
uchar *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,
cv8u, 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 uchar *rowPtr = (const uchar *) imgBuf;
int64 templSqsum = icvCrossCorr_8u_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_8u_C1( rowPtr, rowPtr, templSize.width );
}
}
/* main loop - through x coordinate of the result */
for( x = 0; x < resultSize.width; x++ )
{
int64 sqsum = 0;
uchar *imgPtr = imgBuf + x;
/* update sums and image band buffer */
if( x > 0 )
{
const uchar *src = pImage + x + templSize.width - 1;
uchar *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] = (uchar) 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_8u_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_8u32f_C1R,
(const uchar * pImage, int imageStep, CvSize roiSize,
const uchar * pTemplate, int templStep, CvSize templSize,
float *pResult, int resultStep, void *pBuffer) )
{
uchar *imgBuf = 0;
uchar *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,
cv8u, 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 uchar *rowPtr = (const uchar *) imgBuf;
templSum = icvSumPixels_8u_C1( templBuf, winLen );
for( y = 0; y < roiSize.height; y++, rowPtr += templSize.width )
{
sumBuf[y] = icvSumPixels_8u_C1( rowPtr, templSize.width );
}
}
/* main loop - through x coordinate of the result */
for( x = 0; x < resultSize.width; x++ )
{
uchar *imgPtr = imgBuf + x;
int64 sum = 0;
/* update sums and image band buffer */
if( x > 0 )
{
const uchar *src = pImage + x + templSize.width - 1;
uchar *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] = (uchar) 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_8u_C1( imgPtr, templBuf, winLen );
if( y > 0 )
{
sum -= sumBuf[y - 1];
sum += sumBuf[y + templSize.height - 1];
}
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_8u32f_C1R,
(const uchar * pImage, int imageStep, CvSize roiSize,
const uchar * pTemplate, int templStep, CvSize templSize,
float *pResult, int resultStep, void *pBuffer) )
{
uchar *imgBuf = 0;
uchar *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,
cv8u, 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 uchar *rowPtr = (const uchar *) imgBuf;
int64 templSqsum = icvCrossCorr_8u_C1( templBuf, templBuf, winLen );
templSum = icvSumPixels_8u_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_8u_C1( rowPtr, templSize.width );
sqsumBuf[y] = icvCrossCorr_8u_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;
uchar *imgPtr = imgBuf + x;
/* update sums and image band buffer */
if( x > 0 )
{
const uchar *src = pImage + x + templSize.width - 1;
uchar *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] = (uchar) 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_8u_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;
}
/****************************************************************************************\
* 8s flavor *
\****************************************************************************************/
/* --------------------------------------- SqDiff ------------------------------------- */
IPCVAPI_IMPL( CvStatus, icvMatchTemplate_SqDiff_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 )
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?