1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #include "precomp.hpp"
43 #include "opencl_kernels.hpp"
45 //////////////////////////////////////////////////matchTemplate//////////////////////////////////////////////////////////
48 static bool matchTemplate_CCORR(const UMat &image, const UMat &templ, UMat &result);
49 static bool matchTemplate_CCORR_NORMED(const UMat &image, const UMat &templ, UMat &result);
51 static bool matchTemplate_SQDIFF(const UMat &image, const UMat &templ, UMat &result);
52 static bool matchTemplate_SQDIFF_NORMED (const UMat &image, const UMat &templ, UMat &result);
54 static bool matchTemplate_CCOEFF(const UMat &image, const UMat &templ, UMat &result);
55 static bool matchTemplate_CCOEFF_NORMED(const UMat &image, const UMat &templ, UMat &result);
57 static bool matchTemplateNaive_CCORR (const UMat &image, const UMat &templ, UMat &result, int cn);
58 static bool matchTemplateNaive_SQDIFF(const UMat &image, const UMat &templ, UMat &result, int cn);
60 static bool useNaive(int method, int depth, Size size)
63 if (method == TM_SQDIFF && depth == CV_32F)
65 else if(method == TM_CCORR || (method == TM_SQDIFF && depth == CV_8U))
66 return size.height < 18 && size.width < 18;
70 #define UNUSED(x) (void)(x);
71 UNUSED(method) UNUSED(depth) UNUSED(size)
77 ///////////////////////////////////////////////////CCORR//////////////////////////////////////////////////////////////
79 static bool matchTemplate_CCORR(const UMat &image, const UMat &templ, UMat &result)
81 if (useNaive(TM_CCORR, image.depth(), templ.size()) )
82 return matchTemplateNaive_CCORR(image, templ, result, image.channels());
87 static bool matchTemplateNaive_CCORR (const UMat &image, const UMat &templ, UMat &result, int cn)
89 int type = image.type();
90 int depth = CV_MAT_DEPTH(type);
92 CV_Assert(result.channels() == 1);
94 const char * kernelName = "matchTemplate_Naive_CCORR";
96 ocl::Kernel k (kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
100 size_t globalsize[2] = {result.cols, result.rows};
101 size_t localsize[2] = {16, 16};
103 return k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ), ocl::KernelArg::WriteOnly(result)).run(2,globalsize,localsize,true);
106 static bool matchTemplate_CCORR_NORMED(const UMat &image, const UMat &templ, UMat &result)
108 if (!matchTemplate_CCORR(image, templ, result))
111 int type = image.type();
112 int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
114 const char * kernelName = "matchTemplate_CCORR_NORMED";
116 ocl::Kernel k(kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
120 UMat temp, image_sums, image_sqsums;
121 integral(image.reshape(1), image_sums, temp);
123 if(temp.depth() == CV_64F)
124 temp.convertTo(image_sqsums, CV_32F);
129 templ.reshape(1).convertTo(templ_resh, CV_32F);
131 multiply(templ_resh, templ_resh, temp);
132 unsigned long long templ_sqsum = (unsigned long long)sum(temp)[0];
134 size_t globalsize[2] = {result.cols, result.rows};
135 size_t localsize[2] = {16, 16};
137 return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sqsum).run(2,globalsize,localsize,true);
140 //////////////////////////////////////SQDIFF//////////////////////////////////////////////////////////////
142 static bool matchTemplate_SQDIFF(const UMat &image, const UMat &templ, UMat &result)
144 if (useNaive(TM_SQDIFF, image.depth(), templ.size()))
146 return matchTemplateNaive_SQDIFF(image, templ, result, image.channels());;
152 static bool matchTemplateNaive_SQDIFF(const UMat &image, const UMat &templ, UMat &result, int cn)
154 int type = image.type();
155 int depth = CV_MAT_DEPTH(type);
157 CV_Assert(result.channels() == 1);
159 const char * kernelName = "matchTemplate_Naive_SQDIFF";
161 ocl::Kernel k (kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
165 size_t globalsize[2] = {result.cols, result.rows};
166 size_t localsize[2] = {16, 16};
168 return k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ), ocl::KernelArg::WriteOnly(result)).run(2,globalsize,localsize,true);
171 static bool matchTemplate_SQDIFF_NORMED (const UMat &image, const UMat &templ, UMat &result)
173 if (!matchTemplate_CCORR(image, templ, result))
176 int type = image.type();
177 int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
179 const char * kernelName = "matchTemplate_SQDIFF_NORMED";
181 ocl::Kernel k(kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
185 UMat temp, image_sums, image_sqsums;
186 integral(image.reshape(1), image_sums, temp);
188 if(temp.depth() == CV_64F)
189 temp.convertTo(image_sqsums, CV_32F);
194 templ.reshape(1).convertTo(templ_resh, CV_32F);
196 multiply(templ_resh, templ_resh, temp);
197 unsigned long long templ_sqsum = (unsigned long long)sum(temp)[0];
199 size_t globalsize[2] = {result.cols, result.rows};
200 size_t localsize[2] = {16, 16};
202 return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sqsum).run(2,globalsize,localsize,true);
205 /////////////////////////////////////CCOEFF/////////////////////////////////////////////////////////////////
207 static bool matchTemplate_CCOEFF(const UMat &image, const UMat &templ, UMat &result)
209 if (!matchTemplate_CCORR(image, templ, result))
213 integral(image, image_sums);
215 int type = image_sums.type();
216 int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
218 const char * kernelName;
221 kernelName = "matchTemplate_Prepared_CCOEFF_C1";
223 kernelName = "matchTemplate_Prepared_CCOEFF_C2";
225 kernelName = "matchTemplate_Prepared_CCOEFF_C4";
227 ocl::Kernel k(kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
231 size_t globalsize[2] = {result.cols, result.rows};
232 size_t localsize[2] = {16, 16};
236 float templ_sum = (float)sum(templ)[0]/ templ.size().area();
237 return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sum).run(2,globalsize,localsize,true);
241 Vec4f templ_sum = Vec4f::all(0);
242 templ_sum = sum(templ)/ templ.size().area();
244 return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols,
245 templ_sum[0],templ_sum[1]).run(2,globalsize,localsize,true);
247 return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols,
248 templ_sum[0],templ_sum[1],templ_sum[2],templ_sum[3]).run(2,globalsize,localsize,true);
252 static bool matchTemplate_CCOEFF_NORMED(const UMat &image, const UMat &templ, UMat &result)
255 image.convertTo(imagef, CV_32F);
256 templ.convertTo(templf, CV_32F);
258 if(!matchTemplate_CCORR(imagef, templf, result))
261 const char * kernelName;
263 UMat temp, image_sums, image_sqsums;
264 integral(image,image_sums, temp);
266 int type = image_sums.type();
267 int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
270 kernelName = "matchTemplate_CCOEFF_NORMED_C1";
272 kernelName = "matchTemplate_CCOEFF_NORMED_C2";
274 kernelName = "matchTemplate_CCOEFF_NORMED_C4";
276 ocl::Kernel k(kernelName, ocl::imgproc::match_template_oclsrc,
277 format("-D type=%s -D elem_type=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn));
281 if(temp.depth() == CV_64F)
282 temp.convertTo(image_sqsums, CV_32F);
286 size_t globalsize[2] = {result.cols, result.rows};
287 size_t localsize[2] = {16, 16};
289 float scale = 1.f / templ.size().area();
293 float templ_sum = (float)sum(templ)[0];
295 multiply(templf, templf, temp);
296 float templ_sqsum = (float)sum(temp)[0];
298 templ_sqsum -= scale * templ_sum * templ_sum;
301 if (templ_sqsum < DBL_EPSILON)
303 result = Scalar::all(1);
307 return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums),ocl::KernelArg::ReadOnlyNoSize(image_sqsums),
308 ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, scale, templ_sum, templ_sqsum)
309 .run(2,globalsize,localsize,true);
313 Vec4f templ_sum = Vec4f::all(0);
314 Vec4f templ_sqsum = Vec4f::all(0);
316 templ_sum = sum(templ);
318 multiply(templf, templf, temp);
319 templ_sqsum = sum(temp);
321 float templ_sqsum_sum = 0;
322 for(int i = 0; i < cn; i ++)
324 templ_sqsum_sum += templ_sqsum[i] - scale * templ_sum[i] * templ_sum[i];
329 if (templ_sqsum_sum < DBL_EPSILON)
331 result = Scalar::all(1);
336 return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums),
337 ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, scale,
338 templ_sum[0],templ_sum[1], templ_sqsum_sum)
339 .run(2,globalsize,localsize,true);
341 return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums),
342 ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, scale,
343 templ_sum[0],templ_sum[1],templ_sum[2],templ_sum[3], templ_sqsum_sum)
344 .run(2,globalsize,localsize,true);
349 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
351 static bool ocl_matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method)
353 int type = _img.type();
354 int cn = CV_MAT_CN(type);
356 CV_Assert( cn == _templ.channels() && cn!=3 && cn<=4);
358 typedef bool (*Caller)(const UMat &, const UMat &, UMat &);
360 const Caller callers[] =
362 matchTemplate_SQDIFF, matchTemplate_SQDIFF_NORMED, matchTemplate_CCORR,
363 matchTemplate_CCORR_NORMED, matchTemplate_CCOEFF, matchTemplate_CCOEFF_NORMED
366 Caller caller = callers[method];
368 UMat image = _img.getUMat();
369 UMat templ = _templ.getUMat(), result;
370 _result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F);
371 result = _result.getUMat();
372 return caller(image, templ, result);
379 void crossCorr( const Mat& img, const Mat& _templ, Mat& corr,
380 Size corrsize, int ctype,
381 Point anchor, double delta, int borderType )
383 const double blockScale = 4.5;
384 const int minBlockSize = 256;
385 std::vector<uchar> buf;
388 int depth = img.depth(), cn = img.channels();
389 int tdepth = templ.depth(), tcn = templ.channels();
390 int cdepth = CV_MAT_DEPTH(ctype), ccn = CV_MAT_CN(ctype);
392 CV_Assert( img.dims <= 2 && templ.dims <= 2 && corr.dims <= 2 );
394 if( depth != tdepth && tdepth != std::max(CV_32F, depth) )
396 _templ.convertTo(templ, std::max(CV_32F, depth));
397 tdepth = templ.depth();
400 CV_Assert( depth == tdepth || tdepth == CV_32F);
401 CV_Assert( corrsize.height <= img.rows + templ.rows - 1 &&
402 corrsize.width <= img.cols + templ.cols - 1 );
404 CV_Assert( ccn == 1 || delta == 0 );
406 corr.create(corrsize, ctype);
408 int maxDepth = depth > CV_8S ? CV_64F : std::max(std::max(CV_32F, tdepth), cdepth);
409 Size blocksize, dftsize;
411 blocksize.width = cvRound(templ.cols*blockScale);
412 blocksize.width = std::max( blocksize.width, minBlockSize - templ.cols + 1 );
413 blocksize.width = std::min( blocksize.width, corr.cols );
414 blocksize.height = cvRound(templ.rows*blockScale);
415 blocksize.height = std::max( blocksize.height, minBlockSize - templ.rows + 1 );
416 blocksize.height = std::min( blocksize.height, corr.rows );
418 dftsize.width = std::max(getOptimalDFTSize(blocksize.width + templ.cols - 1), 2);
419 dftsize.height = getOptimalDFTSize(blocksize.height + templ.rows - 1);
420 if( dftsize.width <= 0 || dftsize.height <= 0 )
421 CV_Error( CV_StsOutOfRange, "the input arrays are too big" );
423 // recompute block size
424 blocksize.width = dftsize.width - templ.cols + 1;
425 blocksize.width = MIN( blocksize.width, corr.cols );
426 blocksize.height = dftsize.height - templ.rows + 1;
427 blocksize.height = MIN( blocksize.height, corr.rows );
429 Mat dftTempl( dftsize.height*tcn, dftsize.width, maxDepth );
430 Mat dftImg( dftsize, maxDepth );
432 int i, k, bufSize = 0;
433 if( tcn > 1 && tdepth != maxDepth )
434 bufSize = templ.cols*templ.rows*CV_ELEM_SIZE(tdepth);
436 if( cn > 1 && depth != maxDepth )
437 bufSize = std::max( bufSize, (blocksize.width + templ.cols - 1)*
438 (blocksize.height + templ.rows - 1)*CV_ELEM_SIZE(depth));
440 if( (ccn > 1 || cn > 1) && cdepth != maxDepth )
441 bufSize = std::max( bufSize, blocksize.width*blocksize.height*CV_ELEM_SIZE(cdepth));
445 // compute DFT of each template plane
446 for( k = 0; k < tcn; k++ )
448 int yofs = k*dftsize.height;
450 Mat dst(dftTempl, Rect(0, yofs, dftsize.width, dftsize.height));
451 Mat dst1(dftTempl, Rect(0, yofs, templ.cols, templ.rows));
455 src = tdepth == maxDepth ? dst1 : Mat(templ.size(), tdepth, &buf[0]);
456 int pairs[] = {k, 0};
457 mixChannels(&templ, 1, &src, 1, pairs, 1);
460 if( dst1.data != src.data )
461 src.convertTo(dst1, dst1.depth());
463 if( dst.cols > templ.cols )
465 Mat part(dst, Range(0, templ.rows), Range(templ.cols, dst.cols));
466 part = Scalar::all(0);
468 dft(dst, dst, 0, templ.rows);
471 int tileCountX = (corr.cols + blocksize.width - 1)/blocksize.width;
472 int tileCountY = (corr.rows + blocksize.height - 1)/blocksize.height;
473 int tileCount = tileCountX * tileCountY;
475 Size wholeSize = img.size();
479 if( !(borderType & BORDER_ISOLATED) )
481 img.locateROI(wholeSize, roiofs);
482 img0.adjustROI(roiofs.y, wholeSize.height-img.rows-roiofs.y,
483 roiofs.x, wholeSize.width-img.cols-roiofs.x);
485 borderType |= BORDER_ISOLATED;
487 // calculate correlation by blocks
488 for( i = 0; i < tileCount; i++ )
490 int x = (i%tileCountX)*blocksize.width;
491 int y = (i/tileCountX)*blocksize.height;
493 Size bsz(std::min(blocksize.width, corr.cols - x),
494 std::min(blocksize.height, corr.rows - y));
495 Size dsz(bsz.width + templ.cols - 1, bsz.height + templ.rows - 1);
496 int x0 = x - anchor.x + roiofs.x, y0 = y - anchor.y + roiofs.y;
497 int x1 = std::max(0, x0), y1 = std::max(0, y0);
498 int x2 = std::min(img0.cols, x0 + dsz.width);
499 int y2 = std::min(img0.rows, y0 + dsz.height);
500 Mat src0(img0, Range(y1, y2), Range(x1, x2));
501 Mat dst(dftImg, Rect(0, 0, dsz.width, dsz.height));
502 Mat dst1(dftImg, Rect(x1-x0, y1-y0, x2-x1, y2-y1));
503 Mat cdst(corr, Rect(x, y, bsz.width, bsz.height));
505 for( k = 0; k < cn; k++ )
508 dftImg = Scalar::all(0);
512 src = depth == maxDepth ? dst1 : Mat(y2-y1, x2-x1, depth, &buf[0]);
513 int pairs[] = {k, 0};
514 mixChannels(&src0, 1, &src, 1, pairs, 1);
517 if( dst1.data != src.data )
518 src.convertTo(dst1, dst1.depth());
520 if( x2 - x1 < dsz.width || y2 - y1 < dsz.height )
521 copyMakeBorder(dst1, dst, y1-y0, dst.rows-dst1.rows-(y1-y0),
522 x1-x0, dst.cols-dst1.cols-(x1-x0), borderType);
524 dft( dftImg, dftImg, 0, dsz.height );
525 Mat dftTempl1(dftTempl, Rect(0, tcn > 1 ? k*dftsize.height : 0,
526 dftsize.width, dftsize.height));
527 mulSpectrums(dftImg, dftTempl1, dftImg, 0, true);
528 dft( dftImg, dftImg, DFT_INVERSE + DFT_SCALE, bsz.height );
530 src = dftImg(Rect(0, 0, bsz.width, bsz.height));
534 if( cdepth != maxDepth )
536 Mat plane(bsz, cdepth, &buf[0]);
537 src.convertTo(plane, cdepth, 1, delta);
540 int pairs[] = {0, k};
541 mixChannels(&src, 1, &cdst, 1, pairs, 1);
546 src.convertTo(cdst, cdepth, 1, delta);
549 if( maxDepth != cdepth )
551 Mat plane(bsz, cdepth, &buf[0]);
552 src.convertTo(plane, cdepth);
555 add(src, cdst, cdst);
563 ////////////////////////////////////////////////////////////////////////////////////////////////////////
565 void cv::matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method )
567 CV_Assert( CV_TM_SQDIFF <= method && method <= CV_TM_CCOEFF_NORMED );
569 CV_Assert( (_img.depth() == CV_8U || _img.depth() == CV_32F) && _img.type() == _templ.type() );
571 CV_Assert(_img.size().height >= _templ.size().height && _img.size().width >= _templ.size().width);
573 CV_Assert(_img.dims() <= 2);
575 bool use_opencl = ocl::useOpenCL() && _result.isUMat();
576 if ( use_opencl && ocl_matchTemplate(_img,_templ,_result,method))
579 int numType = method == CV_TM_CCORR || method == CV_TM_CCORR_NORMED ? 0 :
580 method == CV_TM_CCOEFF || method == CV_TM_CCOEFF_NORMED ? 1 : 2;
581 bool isNormed = method == CV_TM_CCORR_NORMED ||
582 method == CV_TM_SQDIFF_NORMED ||
583 method == CV_TM_CCOEFF_NORMED;
585 Mat img = _img.getMat(), templ = _templ.getMat();
586 if( img.rows < templ.rows || img.cols < templ.cols )
587 std::swap(img, templ);
589 Size corrSize(img.cols - templ.cols + 1, img.rows - templ.rows + 1);
590 _result.create(corrSize, CV_32F);
591 Mat result = _result.getMat();
593 #ifdef HAVE_TEGRA_OPTIMIZATION
594 if (tegra::matchTemplate(img, templ, result, method))
598 int cn = img.channels();
599 crossCorr( img, templ, result, result.size(), result.type(), Point(0,0), 0, 0);
601 if( method == CV_TM_CCORR )
604 double invArea = 1./((double)templ.rows * templ.cols);
607 Scalar templMean, templSdv;
608 double *q0 = 0, *q1 = 0, *q2 = 0, *q3 = 0;
609 double templNorm = 0, templSum2 = 0;
611 if( method == CV_TM_CCOEFF )
613 integral(img, sum, CV_64F);
614 templMean = mean(templ);
618 integral(img, sum, sqsum, CV_64F);
619 meanStdDev( templ, templMean, templSdv );
621 templNorm = templSdv[0]*templSdv[0] + templSdv[1]*templSdv[1] + templSdv[2]*templSdv[2] + templSdv[3]*templSdv[3];
623 if( templNorm < DBL_EPSILON && method == CV_TM_CCOEFF_NORMED )
625 result = Scalar::all(1);
629 templSum2 = templNorm + templMean[0]*templMean[0] + templMean[1]*templMean[1] + templMean[2]*templMean[2] + templMean[3]*templMean[3];
633 templMean = Scalar::all(0);
634 templNorm = templSum2;
637 templSum2 /= invArea;
638 templNorm = std::sqrt(templNorm);
639 templNorm /= std::sqrt(invArea); // care of accuracy here
641 q0 = (double*)sqsum.data;
642 q1 = q0 + templ.cols*cn;
643 q2 = (double*)(sqsum.data + templ.rows*sqsum.step);
644 q3 = q2 + templ.cols*cn;
647 double* p0 = (double*)sum.data;
648 double* p1 = p0 + templ.cols*cn;
649 double* p2 = (double*)(sum.data + templ.rows*sum.step);
650 double* p3 = p2 + templ.cols*cn;
652 int sumstep = sum.data ? (int)(sum.step / sizeof(double)) : 0;
653 int sqstep = sqsum.data ? (int)(sqsum.step / sizeof(double)) : 0;
657 for( i = 0; i < result.rows; i++ )
659 float* rrow = (float*)(result.data + i*result.step);
660 int idx = i * sumstep;
661 int idx2 = i * sqstep;
663 for( j = 0; j < result.cols; j++, idx += cn, idx2 += cn )
665 double num = rrow[j], t;
666 double wndMean2 = 0, wndSum2 = 0;
670 for( k = 0; k < cn; k++ )
672 t = p0[idx+k] - p1[idx+k] - p2[idx+k] + p3[idx+k];
674 num -= t*templMean[k];
680 if( isNormed || numType == 2 )
682 for( k = 0; k < cn; k++ )
684 t = q0[idx2+k] - q1[idx2+k] - q2[idx2+k] + q3[idx2+k];
690 num = wndSum2 - 2*num + templSum2;
697 t = std::sqrt(MAX(wndSum2 - wndMean2,0))*templNorm;
700 else if( fabs(num) < t*1.125 )
701 num = num > 0 ? 1 : -1;
703 num = method != CV_TM_SQDIFF_NORMED ? 0 : 1;
706 rrow[j] = (float)num;
713 cvMatchTemplate( const CvArr* _img, const CvArr* _templ, CvArr* _result, int method )
715 cv::Mat img = cv::cvarrToMat(_img), templ = cv::cvarrToMat(_templ),
716 result = cv::cvarrToMat(_result);
717 CV_Assert( result.size() == cv::Size(std::abs(img.cols - templ.cols) + 1,
718 std::abs(img.rows - templ.rows) + 1) &&
719 result.type() == CV_32F );
720 matchTemplate(img, templ, result, method);