Update to 2.0.0 tree from current Fremantle build
[opencv] / src / cv / cvtemplmatch.cpp
diff --git a/src/cv/cvtemplmatch.cpp b/src/cv/cvtemplmatch.cpp
new file mode 100644 (file)
index 0000000..d6512ce
--- /dev/null
@@ -0,0 +1,542 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                        Intel License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000, Intel Corporation, all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other materials provided with the distribution.\r
+//\r
+//   * The name of Intel Corporation may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#include "_cv.h"\r
+\r
+void\r
+icvCrossCorr( const CvArr* _img, const CvArr* _templ, CvArr* _corr,\r
+              CvPoint anchor, double delta, int borderType )\r
+{\r
+    const double block_scale = 4.5;\r
+    const int min_block_size = 256;\r
+    CvMat* dft_img[CV_MAX_THREADS] = {0};\r
+    CvMat* dft_templ = 0;\r
+    void* buf[CV_MAX_THREADS] = {0};\r
+    int k, num_threads = 0;\r
+    \r
+    CV_FUNCNAME( "icvCrossCorr" );\r
+    \r
+    __BEGIN__;\r
+\r
+    CvMat istub, *img = (CvMat*)_img;\r
+    CvMat tstub, *templ = (CvMat*)_templ;\r
+    CvMat cstub, *corr = (CvMat*)_corr;\r
+    CvSize dftsize, blocksize;\r
+    int depth, templ_depth, corr_depth, max_depth = CV_32F,\r
+        cn, templ_cn, corr_cn, buf_size = 0,\r
+        tile_count_x, tile_count_y, tile_count;\r
+\r
+    CV_CALL( img = cvGetMat( img, &istub ));\r
+    CV_CALL( templ = cvGetMat( templ, &tstub ));\r
+    CV_CALL( corr = cvGetMat( corr, &cstub ));\r
+\r
+    if( CV_MAT_DEPTH( img->type ) != CV_8U &&\r
+        CV_MAT_DEPTH( img->type ) != CV_16U &&\r
+        CV_MAT_DEPTH( img->type ) != CV_32F )\r
+        CV_ERROR( CV_StsUnsupportedFormat,\r
+        "The function supports only 8u, 16u and 32f data types" );\r
+\r
+    if( !CV_ARE_DEPTHS_EQ( img, templ ) && CV_MAT_DEPTH( templ->type ) != CV_32F )\r
+        CV_ERROR( CV_StsUnsupportedFormat,\r
+        "Template (kernel) must be of the same depth as the input image, or be 32f" );\r
+    \r
+    if( !CV_ARE_DEPTHS_EQ( img, corr ) && CV_MAT_DEPTH( corr->type ) != CV_32F &&\r
+        CV_MAT_DEPTH( corr->type ) != CV_64F )\r
+        CV_ERROR( CV_StsUnsupportedFormat,\r
+        "The output image must have the same depth as the input image, or be 32f/64f" );\r
+\r
+    if( (!CV_ARE_CNS_EQ( img, corr ) || CV_MAT_CN(templ->type) > 1) &&\r
+        (CV_MAT_CN( corr->type ) > 1 || !CV_ARE_CNS_EQ( img, templ)) )\r
+        CV_ERROR( CV_StsUnsupportedFormat,\r
+        "The output must have the same number of channels as the input (when the template has 1 channel), "\r
+        "or the output must have 1 channel when the input and the template have the same number of channels" );\r
+\r
+    depth = CV_MAT_DEPTH(img->type);\r
+    cn = CV_MAT_CN(img->type);\r
+    templ_depth = CV_MAT_DEPTH(templ->type);\r
+    templ_cn = CV_MAT_CN(templ->type);\r
+    corr_depth = CV_MAT_DEPTH(corr->type);\r
+    corr_cn = CV_MAT_CN(corr->type);\r
+\r
+    CV_Assert( corr_cn == 1 || delta == 0 );\r
+\r
+    max_depth = MAX( max_depth, templ_depth );\r
+    max_depth = MAX( max_depth, depth );\r
+    max_depth = MAX( max_depth, corr_depth );\r
+    if( depth > CV_8U )\r
+        max_depth = CV_64F;\r
+\r
+    if( img->cols < templ->cols || img->rows < templ->rows )\r
+        CV_ERROR( CV_StsUnmatchedSizes,\r
+        "Such a combination of image and template/filter size is not supported" );\r
+\r
+    if( corr->rows > img->rows + templ->rows - 1 ||\r
+        corr->cols > img->cols + templ->cols - 1 )\r
+        CV_ERROR( CV_StsUnmatchedSizes,\r
+        "output image should not be greater than (W + w - 1)x(H + h - 1)" );\r
+\r
+    blocksize.width = cvRound(templ->cols*block_scale);\r
+    blocksize.width = MAX( blocksize.width, min_block_size - templ->cols + 1 );\r
+    blocksize.width = MIN( blocksize.width, corr->cols );\r
+    blocksize.height = cvRound(templ->rows*block_scale);\r
+    blocksize.height = MAX( blocksize.height, min_block_size - templ->rows + 1 );\r
+    blocksize.height = MIN( blocksize.height, corr->rows );\r
+\r
+    dftsize.width = cvGetOptimalDFTSize(blocksize.width + templ->cols - 1);\r
+    if( dftsize.width == 1 )\r
+        dftsize.width = 2;\r
+    dftsize.height = cvGetOptimalDFTSize(blocksize.height + templ->rows - 1);\r
+    if( dftsize.width <= 0 || dftsize.height <= 0 )\r
+        CV_ERROR( CV_StsOutOfRange, "the input arrays are too big" );\r
+\r
+    // recompute block size\r
+    blocksize.width = dftsize.width - templ->cols + 1;\r
+    blocksize.width = MIN( blocksize.width, corr->cols );\r
+    blocksize.height = dftsize.height - templ->rows + 1;\r
+    blocksize.height = MIN( blocksize.height, corr->rows );\r
+\r
+    CV_CALL( dft_templ = cvCreateMat( dftsize.height*templ_cn, dftsize.width, max_depth ));\r
+\r
+    num_threads = cvGetNumThreads();\r
+\r
+    for( k = 0; k < num_threads; k++ )\r
+        CV_CALL( dft_img[k] = cvCreateMat( dftsize.height, dftsize.width, max_depth ));\r
+\r
+    if( templ_cn > 1 && templ_depth != max_depth )\r
+        buf_size = templ->cols*templ->rows*CV_ELEM_SIZE(templ_depth);\r
+\r
+    if( cn > 1 && depth != max_depth )\r
+        buf_size = MAX( buf_size, (blocksize.width + templ->cols - 1)*\r
+            (blocksize.height + templ->rows - 1)*CV_ELEM_SIZE(depth));\r
+\r
+    if( (corr_cn > 1 || cn > 1) && corr_depth != max_depth )\r
+        buf_size = MAX( buf_size, blocksize.width*blocksize.height*CV_ELEM_SIZE(corr_depth));\r
+\r
+    if( buf_size > 0 )\r
+    {\r
+        for( k = 0; k < num_threads; k++ )\r
+            CV_CALL( buf[k] = cvAlloc(buf_size) );\r
+    }\r
+\r
+    // compute DFT of each template plane\r
+    for( k = 0; k < templ_cn; k++ )\r
+    {\r
+        CvMat dstub, *src, *dst, temp;\r
+        CvMat* planes[] = { 0, 0, 0, 0 };\r
+        int yofs = k*dftsize.height;\r
+\r
+        src = templ;\r
+        dst = cvGetSubRect( dft_templ, &dstub, cvRect(0,yofs,templ->cols,templ->rows));\r
+    \r
+        if( templ_cn > 1 )\r
+        {\r
+            planes[k] = templ_depth == max_depth ? dst :\r
+                cvInitMatHeader( &temp, templ->rows, templ->cols, templ_depth, buf[0] );\r
+            cvSplit( templ, planes[0], planes[1], planes[2], planes[3] );\r
+            src = planes[k];\r
+            planes[k] = 0;\r
+        }\r
+\r
+        if( dst != src )\r
+            cvConvert( src, dst );\r
+\r
+        if( dft_templ->cols > templ->cols )\r
+        {\r
+            cvGetSubRect( dft_templ, dst, cvRect(templ->cols, yofs,\r
+                          dft_templ->cols - templ->cols, templ->rows) );\r
+            cvZero( dst );\r
+        }\r
+        cvGetSubRect( dft_templ, dst, cvRect(0,yofs,dftsize.width,dftsize.height) );\r
+        cvDFT( dst, dst, CV_DXT_FORWARD + CV_DXT_SCALE, templ->rows );\r
+    }\r
+\r
+    tile_count_x = (corr->cols + blocksize.width - 1)/blocksize.width;\r
+    tile_count_y = (corr->rows + blocksize.height - 1)/blocksize.height;\r
+    tile_count = tile_count_x*tile_count_y;\r
+\r
+    {\r
+#ifdef _OPENMP\r
+    #pragma omp parallel for num_threads(num_threads) schedule(dynamic)\r
+#endif\r
+    // calculate correlation by blocks\r
+    for( k = 0; k < tile_count; k++ )\r
+    {\r
+        int thread_idx = cvGetThreadNum();\r
+        int x = (k%tile_count_x)*blocksize.width;\r
+        int y = (k/tile_count_x)*blocksize.height;\r
+        int i, yofs;\r
+        CvMat sstub, dstub, *src, *dst, temp;\r
+        CvMat* planes[] = { 0, 0, 0, 0 };\r
+        CvMat* _dft_img = dft_img[thread_idx];\r
+        void* _buf = buf[thread_idx];\r
+        CvSize csz = { blocksize.width, blocksize.height }, isz;\r
+        int x0 = x - anchor.x, y0 = y - anchor.y;\r
+        int x1 = MAX( 0, x0 ), y1 = MAX( 0, y0 ), x2, y2;\r
+        csz.width = MIN( csz.width, corr->cols - x );\r
+        csz.height = MIN( csz.height, corr->rows - y );\r
+        isz.width = csz.width + templ->cols - 1;\r
+        isz.height = csz.height + templ->rows - 1;\r
+        x2 = MIN( img->cols, x0 + isz.width );\r
+        y2 = MIN( img->rows, y0 + isz.height );\r
+        \r
+        for( i = 0; i < cn; i++ )\r
+        {\r
+            CvMat dstub1, *dst1;\r
+            yofs = i*dftsize.height;\r
+\r
+            src = cvGetSubRect( img, &sstub, cvRect(x1,y1,x2-x1,y2-y1) );\r
+            dst = cvGetSubRect( _dft_img, &dstub,\r
+                cvRect(0,0,isz.width,isz.height) );\r
+            dst1 = dst;\r
+            \r
+            if( x2 - x1 < isz.width || y2 - y1 < isz.height )\r
+                dst1 = cvGetSubRect( _dft_img, &dstub1,\r
+                    cvRect( x1 - x0, y1 - y0, x2 - x1, y2 - y1 ));\r
+\r
+            if( cn > 1 )\r
+            {\r
+                planes[i] = dst1;\r
+                if( depth != max_depth )\r
+                    planes[i] = cvInitMatHeader( &temp, y2 - y1, x2 - x1, depth, _buf );\r
+                cvSplit( src, planes[0], planes[1], planes[2], planes[3] );\r
+                src = planes[i];\r
+                planes[i] = 0;\r
+            }\r
+\r
+            if( dst1 != src )\r
+                cvConvert( src, dst1 );\r
+\r
+            if( dst != dst1 )\r
+                cvCopyMakeBorder( dst1, dst, cvPoint(x1 - x0, y1 - y0), borderType );\r
+\r
+            if( dftsize.width > isz.width )\r
+            {\r
+                cvGetSubRect( _dft_img, dst, cvRect(isz.width, 0,\r
+                      dftsize.width - isz.width,dftsize.height) );\r
+                cvZero( dst );\r
+            }\r
+\r
+            cvDFT( _dft_img, _dft_img, CV_DXT_FORWARD, isz.height );\r
+            cvGetSubRect( dft_templ, dst,\r
+                cvRect(0,(templ_cn>1?yofs:0),dftsize.width,dftsize.height) );\r
+\r
+            cvMulSpectrums( _dft_img, dst, _dft_img, CV_DXT_MUL_CONJ );\r
+            cvDFT( _dft_img, _dft_img, CV_DXT_INVERSE, csz.height );\r
+\r
+            src = cvGetSubRect( _dft_img, &sstub, cvRect(0,0,csz.width,csz.height) );\r
+            dst = cvGetSubRect( corr, &dstub, cvRect(x,y,csz.width,csz.height) );\r
+\r
+            if( corr_cn > 1 )\r
+            {\r
+                planes[i] = src;\r
+                if( corr_depth != max_depth )\r
+                {\r
+                    planes[i] = cvInitMatHeader( &temp, csz.height, csz.width,\r
+                                                 corr_depth, _buf );\r
+                    cvConvertScale( src, planes[i], 1, delta );\r
+                }\r
+                cvMerge( planes[0], planes[1], planes[2], planes[3], dst );\r
+                planes[i] = 0;                    \r
+            }\r
+            else\r
+            {\r
+                if( i == 0 )\r
+                    cvConvertScale( src, dst, 1, delta );\r
+                else\r
+                {\r
+                    if( max_depth > corr_depth )\r
+                    {\r
+                        cvInitMatHeader( &temp, csz.height, csz.width,\r
+                                         corr_depth, _buf );\r
+                        cvConvert( src, &temp );\r
+                        src = &temp;\r
+                    }\r
+                    cvAcc( src, dst );\r
+                }\r
+            }\r
+        }\r
+    }\r
+    }\r
+\r
+    __END__;\r
+\r
+    cvReleaseMat( &dft_templ );\r
+\r
+    for( k = 0; k < num_threads; k++ )\r
+    {\r
+        cvReleaseMat( &dft_img[k] );\r
+        cvFree( &buf[k] );\r
+    }\r
+}\r
+\r
+void\r
+cv::crossCorr( const Mat& img, const Mat& templ, Mat& corr,\r
+           Point anchor, double delta, int borderType )\r
+{\r
+    CvMat _img = img, _templ = templ, _corr = corr;\r
+    icvCrossCorr( &_img, &_templ, &_corr, anchor, delta, borderType );\r
+}\r
+\r
+\r
+/*****************************************************************************************/\r
+\r
+CV_IMPL void\r
+cvMatchTemplate( const CvArr* _img, const CvArr* _templ, CvArr* _result, int method )\r
+{\r
+    CvMat* sum = 0;\r
+    CvMat* sqsum = 0;\r
+    \r
+    CV_FUNCNAME( "cvMatchTemplate" );\r
+\r
+    __BEGIN__;\r
+\r
+    int coi1 = 0, coi2 = 0;\r
+    int depth, cn;\r
+    int i, j, k;\r
+    CvMat stub, *img = (CvMat*)_img;\r
+    CvMat tstub, *templ = (CvMat*)_templ;\r
+    CvMat rstub, *result = (CvMat*)_result;\r
+    CvScalar templ_mean = cvScalarAll(0);\r
+    double templ_norm = 0, templ_sum2 = 0;\r
+    \r
+    int idx = 0, idx2 = 0;\r
+    double *p0, *p1, *p2, *p3;\r
+    double *q0, *q1, *q2, *q3;\r
+    double inv_area;\r
+    int sum_step, sqsum_step;\r
+    int num_type = method == CV_TM_CCORR || method == CV_TM_CCORR_NORMED ? 0 :\r
+                   method == CV_TM_CCOEFF || method == CV_TM_CCOEFF_NORMED ? 1 : 2;\r
+    int is_normed = method == CV_TM_CCORR_NORMED ||\r
+                    method == CV_TM_SQDIFF_NORMED ||\r
+                    method == CV_TM_CCOEFF_NORMED;\r
+\r
+    CV_CALL( img = cvGetMat( img, &stub, &coi1 ));\r
+    CV_CALL( templ = cvGetMat( templ, &tstub, &coi2 ));\r
+    CV_CALL( result = cvGetMat( result, &rstub ));\r
+\r
+    if( CV_MAT_DEPTH( img->type ) != CV_8U &&\r
+        CV_MAT_DEPTH( img->type ) != CV_32F )\r
+        CV_ERROR( CV_StsUnsupportedFormat,\r
+        "The function supports only 8u and 32f data types" );\r
+\r
+    if( !CV_ARE_TYPES_EQ( img, templ ))\r
+        CV_ERROR( CV_StsUnmatchedSizes, "image and template should have the same type" );\r
+\r
+    if( CV_MAT_TYPE( result->type ) != CV_32FC1 )\r
+        CV_ERROR( CV_StsUnsupportedFormat, "output image should have 32f type" );\r
+\r
+    if( img->rows < templ->rows || img->cols < templ->cols )\r
+    {\r
+        CvMat* t;\r
+        CV_SWAP( img, templ, t );\r
+    }\r
+\r
+    if( result->rows != img->rows - templ->rows + 1 ||\r
+        result->cols != img->cols - templ->cols + 1 )\r
+        CV_ERROR( CV_StsUnmatchedSizes, "output image should be (W - w + 1)x(H - h + 1)" );\r
+\r
+    if( method < CV_TM_SQDIFF || method > CV_TM_CCOEFF_NORMED )\r
+        CV_ERROR( CV_StsBadArg, "unknown comparison method" );\r
+\r
+    depth = CV_MAT_DEPTH(img->type);\r
+    cn = CV_MAT_CN(img->type);\r
+\r
+    /*if( is_normed && cn == 1 && templ->rows > 8 && templ->cols > 8 &&\r
+        img->rows > templ->cols && img->cols > templ->cols )\r
+    {\r
+        CvTemplMatchIPPFunc ipp_func =\r
+            depth == CV_8U ?\r
+            (method == CV_TM_SQDIFF_NORMED ? (CvTemplMatchIPPFunc)icvSqrDistanceValid_Norm_8u32f_C1R_p :\r
+            method == CV_TM_CCORR_NORMED ? (CvTemplMatchIPPFunc)icvCrossCorrValid_Norm_8u32f_C1R_p :\r
+            (CvTemplMatchIPPFunc)icvCrossCorrValid_NormLevel_8u32f_C1R_p) :\r
+            (method == CV_TM_SQDIFF_NORMED ? (CvTemplMatchIPPFunc)icvSqrDistanceValid_Norm_32f_C1R_p :\r
+            method == CV_TM_CCORR_NORMED ? (CvTemplMatchIPPFunc)icvCrossCorrValid_Norm_32f_C1R_p :\r
+            (CvTemplMatchIPPFunc)icvCrossCorrValid_NormLevel_32f_C1R_p);\r
+\r
+        if( ipp_func )\r
+        {\r
+            CvSize img_size = cvGetMatSize(img), templ_size = cvGetMatSize(templ);\r
+\r
+            IPPI_CALL( ipp_func( img->data.ptr, img->step ? img->step : CV_STUB_STEP,\r
+                                 img_size, templ->data.ptr,\r
+                                 templ->step ? templ->step : CV_STUB_STEP,\r
+                                 templ_size, result->data.ptr,\r
+                                 result->step ? result->step : CV_STUB_STEP ));\r
+            for( i = 0; i < result->rows; i++ )\r
+            {\r
+                float* rrow = (float*)(result->data.ptr + i*result->step);\r
+                for( j = 0; j < result->cols; j++ )\r
+                {\r
+                    if( fabs(rrow[j]) > 1. )\r
+                        rrow[j] = rrow[j] < 0 ? -1.f : 1.f;\r
+                }\r
+            }\r
+            EXIT;\r
+        }\r
+    }*/\r
+\r
+    CV_CALL( icvCrossCorr( img, templ, result ));\r
+\r
+    if( method == CV_TM_CCORR )\r
+        EXIT;\r
+\r
+    inv_area = 1./((double)templ->rows * templ->cols);\r
+\r
+    CV_CALL( sum = cvCreateMat( img->rows + 1, img->cols + 1,\r
+                                CV_MAKETYPE( CV_64F, cn )));\r
+    if( method == CV_TM_CCOEFF )\r
+    {\r
+        CV_CALL( cvIntegral( img, sum, 0, 0 ));\r
+        CV_CALL( templ_mean = cvAvg( templ ));\r
+        q0 = q1 = q2 = q3 = 0;\r
+    }\r
+    else\r
+    {\r
+        CvScalar _templ_sdv = cvScalarAll(0);\r
+        CV_CALL( sqsum = cvCreateMat( img->rows + 1, img->cols + 1,\r
+                                      CV_MAKETYPE( CV_64F, cn )));\r
+        CV_CALL( cvIntegral( img, sum, sqsum, 0 ));\r
+        CV_CALL( cvAvgSdv( templ, &templ_mean, &_templ_sdv ));\r
+\r
+        templ_norm = CV_SQR(_templ_sdv.val[0]) + CV_SQR(_templ_sdv.val[1]) +\r
+                    CV_SQR(_templ_sdv.val[2]) + CV_SQR(_templ_sdv.val[3]);\r
+\r
+        if( templ_norm < DBL_EPSILON && method == CV_TM_CCOEFF_NORMED )\r
+        {\r
+            cvSet( result, cvScalarAll(1.) );\r
+            EXIT;\r
+        }\r
+        \r
+        templ_sum2 = templ_norm +\r
+                     CV_SQR(templ_mean.val[0]) + CV_SQR(templ_mean.val[1]) +\r
+                     CV_SQR(templ_mean.val[2]) + CV_SQR(templ_mean.val[3]);\r
+\r
+        if( num_type != 1 )\r
+        {\r
+            templ_mean = cvScalarAll(0);\r
+            templ_norm = templ_sum2;\r
+        }\r
+        \r
+        templ_sum2 /= inv_area;\r
+        templ_norm = sqrt(templ_norm);\r
+        templ_norm /= sqrt(inv_area); // care of accuracy here\r
+\r
+        q0 = (double*)sqsum->data.ptr;\r
+        q1 = q0 + templ->cols*cn;\r
+        q2 = (double*)(sqsum->data.ptr + templ->rows*sqsum->step);\r
+        q3 = q2 + templ->cols*cn;\r
+    }\r
+\r
+    p0 = (double*)sum->data.ptr;\r
+    p1 = p0 + templ->cols*cn;\r
+    p2 = (double*)(sum->data.ptr + templ->rows*sum->step);\r
+    p3 = p2 + templ->cols*cn;\r
+\r
+    sum_step = sum ? sum->step / sizeof(double) : 0;\r
+    sqsum_step = sqsum ? sqsum->step / sizeof(double) : 0;\r
+\r
+    for( i = 0; i < result->rows; i++ )\r
+    {\r
+        float* rrow = (float*)(result->data.ptr + i*result->step);\r
+        idx = i * sum_step;\r
+        idx2 = i * sqsum_step;\r
+\r
+        for( j = 0; j < result->cols; j++, idx += cn, idx2 += cn )\r
+        {\r
+            double num = rrow[j], t;\r
+            double wnd_mean2 = 0, wnd_sum2 = 0;\r
+            \r
+            if( num_type == 1 )\r
+            {\r
+                for( k = 0; k < cn; k++ )\r
+                {\r
+                    t = p0[idx+k] - p1[idx+k] - p2[idx+k] + p3[idx+k];\r
+                    wnd_mean2 += CV_SQR(t);\r
+                    num -= t*templ_mean.val[k];\r
+                }\r
+\r
+                wnd_mean2 *= inv_area;\r
+            }\r
+\r
+            if( is_normed || num_type == 2 )\r
+            {\r
+                for( k = 0; k < cn; k++ )\r
+                {\r
+                    t = q0[idx2+k] - q1[idx2+k] - q2[idx2+k] + q3[idx2+k];\r
+                    wnd_sum2 += t;\r
+                }\r
+\r
+                if( num_type == 2 )\r
+                    num = wnd_sum2 - 2*num + templ_sum2;\r
+            }\r
+\r
+            if( is_normed )\r
+            {\r
+                t = sqrt(MAX(wnd_sum2 - wnd_mean2,0))*templ_norm;\r
+                if( t > DBL_EPSILON )\r
+                {\r
+                    num /= t;\r
+                    if( fabs(num) > 1. )\r
+                        num = num > 0 ? 1 : -1;\r
+                }\r
+                else\r
+                    num = method != CV_TM_SQDIFF_NORMED || num < DBL_EPSILON ? 0 : 1;\r
+            }\r
+\r
+            rrow[j] = (float)num;\r
+        }\r
+    }\r
+        \r
+    __END__;\r
+\r
+    cvReleaseMat( &sum );\r
+    cvReleaseMat( &sqsum );\r
+}\r
+\r
+void cv::matchTemplate( const Mat& image, const Mat& templ, Mat& result, int method )\r
+{\r
+    result.create( std::abs(image.rows - templ.rows) + 1,\r
+                   std::abs(image.cols - templ.cols) + 1, CV_32F );\r
+    CvMat _image = image, _templ = templ, _result = result;\r
+    cvMatchTemplate( &_image, &_templ, &_result, method );    \r
+}\r
+\r
+/* End of file. */\r