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 //////////////////////////////////////////////////////////
52 /////////////////////////////////////////////////// CCORR //////////////////////////////////////////////////////////////
59 static bool sumTemplate(InputArray _src, UMat & result)
61 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
62 int wdepth = std::max(CV_32S, depth), wtype = CV_MAKE_TYPE(wdepth, cn);
63 size_t wgs = ocl::Device::getDefault().maxWorkGroupSize();
66 while (wgs2_aligned < (int)wgs)
71 ocl::Kernel k("calcSum", ocl::imgproc::match_template_oclsrc,
72 format("-D CALC_SUM -D T=%s -D WT=%s -D cn=%d -D convertToWT=%s -D WGS=%d -D WGS2_ALIGNED=%d -D wdepth=%d",
73 ocl::typeToStr(type), ocl::typeToStr(wtype), cn,
74 ocl::convertTypeStr(depth, wdepth, cn, cvt),
75 (int)wgs, wgs2_aligned, wdepth));
79 UMat src = _src.getUMat();
80 result.create(1, 1, CV_32FC1);
82 ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
83 resarg = ocl::KernelArg::PtrWriteOnly(result);
85 k.args(srcarg, src.cols, (int)src.total(), resarg);
87 size_t globalsize = wgs;
88 return k.run(1, &globalsize, &wgs, false);
91 static bool matchTemplateNaive_CCORR(InputArray _image, InputArray _templ, OutputArray _result)
93 int type = _image.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
94 int wdepth = std::max(depth, CV_32S), wtype = CV_MAKE_TYPE(wdepth, cn);
97 ocl::Kernel k("matchTemplate_Naive_CCORR", ocl::imgproc::match_template_oclsrc,
98 format("-D CCORR -D T=%s -D WT=%s -D convertToWT=%s -D cn=%d -D wdepth=%d", ocl::typeToStr(type), ocl::typeToStr(wtype),
99 ocl::convertTypeStr(depth, wdepth, cn, cvt), cn, wdepth));
103 UMat image = _image.getUMat(), templ = _templ.getUMat();
104 _result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32FC1);
105 UMat result = _result.getUMat();
107 k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ),
108 ocl::KernelArg::WriteOnly(result));
110 size_t globalsize[2] = { result.cols, result.rows };
111 return k.run(2, globalsize, NULL, false);
114 static bool matchTemplate_CCORR_NORMED(InputArray _image, InputArray _templ, OutputArray _result)
116 matchTemplate(_image, _templ, _result, CV_TM_CCORR);
118 int type = _image.type(), cn = CV_MAT_CN(type);
120 ocl::Kernel k("matchTemplate_CCORR_NORMED", ocl::imgproc::match_template_oclsrc,
121 format("-D CCORR_NORMED -D T=%s -D cn=%d", ocl::typeToStr(type), cn));
125 UMat image = _image.getUMat(), templ = _templ.getUMat();
126 _result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32FC1);
127 UMat result = _result.getUMat();
129 UMat image_sums, image_sqsums;
130 integral(image.reshape(1), image_sums, image_sqsums, CV_32F, CV_32F);
133 if (!sumTemplate(templ, templ_sqsum))
136 k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::ReadWrite(result),
137 templ.rows, templ.cols, ocl::KernelArg::PtrReadOnly(templ_sqsum));
139 size_t globalsize[2] = { result.cols, result.rows };
140 return k.run(2, globalsize, NULL, false);
143 ////////////////////////////////////// SQDIFF //////////////////////////////////////////////////////////////
145 static bool matchTemplateNaive_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result)
147 int type = _image.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
148 int wdepth = std::max(depth, CV_32S), wtype = CV_MAKE_TYPE(wdepth, cn);
151 ocl::Kernel k("matchTemplate_Naive_SQDIFF", ocl::imgproc::match_template_oclsrc,
152 format("-D SQDIFF -D T=%s -D WT=%s -D convertToWT=%s -D cn=%d -D wdepth=%d", ocl::typeToStr(type),
153 ocl::typeToStr(wtype), ocl::convertTypeStr(depth, wdepth, cn, cvt), cn, wdepth));
157 UMat image = _image.getUMat(), templ = _templ.getUMat();
158 _result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F);
159 UMat result = _result.getUMat();
161 k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ),
162 ocl::KernelArg::WriteOnly(result));
164 size_t globalsize[2] = { result.cols, result.rows };
165 return k.run(2, globalsize, NULL, false);
168 static bool matchTemplate_SQDIFF_NORMED(InputArray _image, InputArray _templ, OutputArray _result)
170 matchTemplate(_image, _templ, _result, CV_TM_CCORR);
172 int type = _image.type(), cn = CV_MAT_CN(type);
174 ocl::Kernel k("matchTemplate_SQDIFF_NORMED", ocl::imgproc::match_template_oclsrc,
175 format("-D SQDIFF_NORMED -D T=%s -D cn=%d", ocl::typeToStr(type), cn));
179 UMat image = _image.getUMat(), templ = _templ.getUMat();
180 _result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F);
181 UMat result = _result.getUMat();
183 UMat image_sums, image_sqsums;
184 integral(image.reshape(1), image_sums, image_sqsums, CV_32F, CV_32F);
187 if (!sumTemplate(_templ, templ_sqsum))
190 k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::ReadWrite(result),
191 templ.rows, templ.cols, ocl::KernelArg::PtrReadOnly(templ_sqsum));
193 size_t globalsize[2] = { result.cols, result.rows };
194 return k.run(2, globalsize, NULL, false);
197 ///////////////////////////////////// CCOEFF /////////////////////////////////////////////////////////////////
199 static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArray _result)
201 matchTemplate(_image, _templ, _result, CV_TM_CCORR);
203 UMat image_sums, temp;
204 integral(_image, temp);
206 if (temp.depth() == CV_64F)
207 temp.convertTo(image_sums, CV_32F);
211 int type = image_sums.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
213 ocl::Kernel k("matchTemplate_Prepared_CCOEFF", ocl::imgproc::match_template_oclsrc,
214 format("-D CCOEFF -D T=%s -D elem_type=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn));
218 UMat templ = _templ.getUMat();
219 Size size = _image.size(), tsize = templ.size();
220 _result.create(size.height - templ.rows + 1, size.width - templ.cols + 1, CV_32F);
221 UMat result = _result.getUMat();
225 float templ_sum = static_cast<float>(sum(_templ)[0]) / tsize.area();
227 k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result),
228 templ.rows, templ.cols, templ_sum);
232 Vec4f templ_sum = Vec4f::all(0);
233 templ_sum = sum(templ) / tsize.area();
236 k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols,
237 templ_sum[0], templ_sum[1]);
239 k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols,
240 templ_sum[0], templ_sum[1], templ_sum[2], templ_sum[3]);
243 size_t globalsize[2] = { result.cols, result.rows };
244 return k.run(2, globalsize, NULL, false);
247 static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, OutputArray _result)
249 matchTemplate(_image, _templ, _result, CV_TM_CCORR);
251 UMat temp, image_sums, image_sqsums;
252 integral(_image, image_sums, image_sqsums, CV_32F, CV_32F);
254 int type = image_sums.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
256 ocl::Kernel k("matchTemplate_CCOEFF_NORMED", ocl::imgproc::match_template_oclsrc,
257 format("-D CCOEFF_NORMED -D type=%s -D elem_type=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn));
261 UMat templ = _templ.getUMat();
262 Size size = _image.size(), tsize = templ.size();
263 _result.create(size.height - templ.rows + 1, size.width - templ.cols + 1, CV_32F);
264 UMat result = _result.getUMat();
266 float scale = 1.f / tsize.area();
270 float templ_sum = (float)sum(templ)[0];
272 multiply(templ, templ, temp, 1, CV_32F);
273 float templ_sqsum = (float)sum(temp)[0];
275 templ_sqsum -= scale * templ_sum * templ_sum;
278 if (templ_sqsum < DBL_EPSILON)
280 result = Scalar::all(1);
284 k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums),
285 ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, scale, templ_sum, templ_sqsum);
289 Vec4f templ_sum = Vec4f::all(0), templ_sqsum = Vec4f::all(0);
290 templ_sum = sum(templ);
292 multiply(templ, templ, temp, 1, CV_32F);
293 templ_sqsum = sum(temp);
295 float templ_sqsum_sum = 0;
296 for (int i = 0; i < cn; i ++)
297 templ_sqsum_sum += templ_sqsum[i] - scale * templ_sum[i] * templ_sum[i];
301 if (templ_sqsum_sum < DBL_EPSILON)
303 result = Scalar::all(1);
308 k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums),
309 ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, scale,
310 templ_sum[0], templ_sum[1], templ_sqsum_sum);
312 k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums),
313 ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, scale,
314 templ_sum[0], templ_sum[1], templ_sum[2], templ_sum[3], templ_sqsum_sum);
317 size_t globalsize[2] = { result.cols, result.rows };
318 return k.run(2, globalsize, NULL, false);
321 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
323 static bool ocl_matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method)
325 int cn = _img.channels();
327 if (cn == 3 || cn > 4)
330 typedef bool (*Caller)(InputArray _img, InputArray _templ, OutputArray _result);
332 static const Caller callers[] =
334 matchTemplateNaive_SQDIFF, matchTemplate_SQDIFF_NORMED, matchTemplateNaive_CCORR,
335 matchTemplate_CCORR_NORMED, matchTemplate_CCOEFF, matchTemplate_CCOEFF_NORMED
337 const Caller caller = callers[method];
339 return caller(_img, _templ, _result);
344 #if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
346 typedef IppStatus (CV_STDCALL * ippimatchTemplate)(const void*, int, IppiSize, const void*, int, IppiSize, Ipp32f* , int , IppEnum , Ipp8u*);
348 static bool ipp_crossCorr(const Mat& src, const Mat& tpl, Mat& dst)
350 if (src.channels()!= 1)
355 IppiSize srcRoiSize = {src.cols,src.rows};
356 IppiSize tplRoiSize = {tpl.cols,tpl.rows};
361 int depth = src.depth();
363 ippimatchTemplate ippFunc =
364 depth==CV_8U ? (ippimatchTemplate)ippiCrossCorrNorm_8u32f_C1R:
365 depth==CV_32F? (ippimatchTemplate)ippiCrossCorrNorm_32f_C1R: 0;
370 IppEnum funCfg = (IppEnum)(ippAlgAuto | ippiNormNone | ippiROIValid);
372 status = ippiCrossCorrNormGetBufferSize(srcRoiSize, tplRoiSize, funCfg, &bufSize);
376 pBuffer = ippsMalloc_8u( bufSize );
378 status = ippFunc(src.data, (int)src.step, srcRoiSize, tpl.data, (int)tpl.step, tplRoiSize, (Ipp32f*)dst.data, (int)dst.step, funCfg, pBuffer);
384 static bool ipp_sqrDistance(const Mat& src, const Mat& tpl, Mat& dst)
386 if (src.channels()!= 1)
391 IppiSize srcRoiSize = {src.cols,src.rows};
392 IppiSize tplRoiSize = {tpl.cols,tpl.rows};
397 int depth = src.depth();
399 ippimatchTemplate ippFunc =
400 depth==CV_8U ? (ippimatchTemplate)ippiSqrDistanceNorm_8u32f_C1R:
401 depth==CV_32F? (ippimatchTemplate)ippiSqrDistanceNorm_32f_C1R: 0;
406 IppEnum funCfg = (IppEnum)(ippAlgAuto | ippiNormNone | ippiROIValid);
408 status = ippiSqrDistanceNormGetBufferSize(srcRoiSize, tplRoiSize, funCfg, &bufSize);
412 pBuffer = ippsMalloc_8u( bufSize );
414 status = ippFunc(src.data, (int)src.step, srcRoiSize, tpl.data, (int)tpl.step, tplRoiSize, (Ipp32f*)dst.data, (int)dst.step, funCfg, pBuffer);
422 void crossCorr( const Mat& img, const Mat& _templ, Mat& corr,
423 Size corrsize, int ctype,
424 Point anchor, double delta, int borderType )
426 #if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
427 if (ipp_crossCorr(img, _templ, corr))
431 const double blockScale = 4.5;
432 const int minBlockSize = 256;
433 std::vector<uchar> buf;
436 int depth = img.depth(), cn = img.channels();
437 int tdepth = templ.depth(), tcn = templ.channels();
438 int cdepth = CV_MAT_DEPTH(ctype), ccn = CV_MAT_CN(ctype);
440 CV_Assert( img.dims <= 2 && templ.dims <= 2 && corr.dims <= 2 );
442 if( depth != tdepth && tdepth != std::max(CV_32F, depth) )
444 _templ.convertTo(templ, std::max(CV_32F, depth));
445 tdepth = templ.depth();
448 CV_Assert( depth == tdepth || tdepth == CV_32F);
449 CV_Assert( corrsize.height <= img.rows + templ.rows - 1 &&
450 corrsize.width <= img.cols + templ.cols - 1 );
452 CV_Assert( ccn == 1 || delta == 0 );
454 corr.create(corrsize, ctype);
456 int maxDepth = depth > CV_8S ? CV_64F : std::max(std::max(CV_32F, tdepth), cdepth);
457 Size blocksize, dftsize;
459 blocksize.width = cvRound(templ.cols*blockScale);
460 blocksize.width = std::max( blocksize.width, minBlockSize - templ.cols + 1 );
461 blocksize.width = std::min( blocksize.width, corr.cols );
462 blocksize.height = cvRound(templ.rows*blockScale);
463 blocksize.height = std::max( blocksize.height, minBlockSize - templ.rows + 1 );
464 blocksize.height = std::min( blocksize.height, corr.rows );
466 dftsize.width = std::max(getOptimalDFTSize(blocksize.width + templ.cols - 1), 2);
467 dftsize.height = getOptimalDFTSize(blocksize.height + templ.rows - 1);
468 if( dftsize.width <= 0 || dftsize.height <= 0 )
469 CV_Error( CV_StsOutOfRange, "the input arrays are too big" );
471 // recompute block size
472 blocksize.width = dftsize.width - templ.cols + 1;
473 blocksize.width = MIN( blocksize.width, corr.cols );
474 blocksize.height = dftsize.height - templ.rows + 1;
475 blocksize.height = MIN( blocksize.height, corr.rows );
477 Mat dftTempl( dftsize.height*tcn, dftsize.width, maxDepth );
478 Mat dftImg( dftsize, maxDepth );
480 int i, k, bufSize = 0;
481 if( tcn > 1 && tdepth != maxDepth )
482 bufSize = templ.cols*templ.rows*CV_ELEM_SIZE(tdepth);
484 if( cn > 1 && depth != maxDepth )
485 bufSize = std::max( bufSize, (blocksize.width + templ.cols - 1)*
486 (blocksize.height + templ.rows - 1)*CV_ELEM_SIZE(depth));
488 if( (ccn > 1 || cn > 1) && cdepth != maxDepth )
489 bufSize = std::max( bufSize, blocksize.width*blocksize.height*CV_ELEM_SIZE(cdepth));
493 // compute DFT of each template plane
494 for( k = 0; k < tcn; k++ )
496 int yofs = k*dftsize.height;
498 Mat dst(dftTempl, Rect(0, yofs, dftsize.width, dftsize.height));
499 Mat dst1(dftTempl, Rect(0, yofs, templ.cols, templ.rows));
503 src = tdepth == maxDepth ? dst1 : Mat(templ.size(), tdepth, &buf[0]);
504 int pairs[] = {k, 0};
505 mixChannels(&templ, 1, &src, 1, pairs, 1);
508 if( dst1.data != src.data )
509 src.convertTo(dst1, dst1.depth());
511 if( dst.cols > templ.cols )
513 Mat part(dst, Range(0, templ.rows), Range(templ.cols, dst.cols));
514 part = Scalar::all(0);
516 dft(dst, dst, 0, templ.rows);
519 int tileCountX = (corr.cols + blocksize.width - 1)/blocksize.width;
520 int tileCountY = (corr.rows + blocksize.height - 1)/blocksize.height;
521 int tileCount = tileCountX * tileCountY;
523 Size wholeSize = img.size();
527 if( !(borderType & BORDER_ISOLATED) )
529 img.locateROI(wholeSize, roiofs);
530 img0.adjustROI(roiofs.y, wholeSize.height-img.rows-roiofs.y,
531 roiofs.x, wholeSize.width-img.cols-roiofs.x);
533 borderType |= BORDER_ISOLATED;
535 // calculate correlation by blocks
536 for( i = 0; i < tileCount; i++ )
538 int x = (i%tileCountX)*blocksize.width;
539 int y = (i/tileCountX)*blocksize.height;
541 Size bsz(std::min(blocksize.width, corr.cols - x),
542 std::min(blocksize.height, corr.rows - y));
543 Size dsz(bsz.width + templ.cols - 1, bsz.height + templ.rows - 1);
544 int x0 = x - anchor.x + roiofs.x, y0 = y - anchor.y + roiofs.y;
545 int x1 = std::max(0, x0), y1 = std::max(0, y0);
546 int x2 = std::min(img0.cols, x0 + dsz.width);
547 int y2 = std::min(img0.rows, y0 + dsz.height);
548 Mat src0(img0, Range(y1, y2), Range(x1, x2));
549 Mat dst(dftImg, Rect(0, 0, dsz.width, dsz.height));
550 Mat dst1(dftImg, Rect(x1-x0, y1-y0, x2-x1, y2-y1));
551 Mat cdst(corr, Rect(x, y, bsz.width, bsz.height));
553 for( k = 0; k < cn; k++ )
556 dftImg = Scalar::all(0);
560 src = depth == maxDepth ? dst1 : Mat(y2-y1, x2-x1, depth, &buf[0]);
561 int pairs[] = {k, 0};
562 mixChannels(&src0, 1, &src, 1, pairs, 1);
565 if( dst1.data != src.data )
566 src.convertTo(dst1, dst1.depth());
568 if( x2 - x1 < dsz.width || y2 - y1 < dsz.height )
569 copyMakeBorder(dst1, dst, y1-y0, dst.rows-dst1.rows-(y1-y0),
570 x1-x0, dst.cols-dst1.cols-(x1-x0), borderType);
572 dft( dftImg, dftImg, 0, dsz.height );
573 Mat dftTempl1(dftTempl, Rect(0, tcn > 1 ? k*dftsize.height : 0,
574 dftsize.width, dftsize.height));
575 mulSpectrums(dftImg, dftTempl1, dftImg, 0, true);
576 dft( dftImg, dftImg, DFT_INVERSE + DFT_SCALE, bsz.height );
578 src = dftImg(Rect(0, 0, bsz.width, bsz.height));
582 if( cdepth != maxDepth )
584 Mat plane(bsz, cdepth, &buf[0]);
585 src.convertTo(plane, cdepth, 1, delta);
588 int pairs[] = {0, k};
589 mixChannels(&src, 1, &cdst, 1, pairs, 1);
594 src.convertTo(cdst, cdepth, 1, delta);
597 if( maxDepth != cdepth )
599 Mat plane(bsz, cdepth, &buf[0]);
600 src.convertTo(plane, cdepth);
603 add(src, cdst, cdst);
611 ////////////////////////////////////////////////////////////////////////////////////////////////////////
613 void cv::matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method )
615 CV_Assert( CV_TM_SQDIFF <= method && method <= CV_TM_CCOEFF_NORMED );
616 CV_Assert( (_img.depth() == CV_8U || _img.depth() == CV_32F) && _img.type() == _templ.type() && _img.dims() <= 2 );
618 bool needswap = _img.size().height < _templ.size().height || _img.size().width < _templ.size().width;
621 CV_Assert(_img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width);
624 CV_OCL_RUN(_img.dims() <= 2 && _result.isUMat(),
625 (!needswap ? ocl_matchTemplate(_img, _templ, _result, method) : ocl_matchTemplate(_templ, _img, _result, method)))
627 int numType = method == CV_TM_CCORR || method == CV_TM_CCORR_NORMED ? 0 :
628 method == CV_TM_CCOEFF || method == CV_TM_CCOEFF_NORMED ? 1 : 2;
629 bool isNormed = method == CV_TM_CCORR_NORMED ||
630 method == CV_TM_SQDIFF_NORMED ||
631 method == CV_TM_CCOEFF_NORMED;
633 Mat img = _img.getMat(), templ = _templ.getMat();
635 std::swap(img, templ);
637 Size corrSize(img.cols - templ.cols + 1, img.rows - templ.rows + 1);
638 _result.create(corrSize, CV_32F);
639 Mat result = _result.getMat();
641 #ifdef HAVE_TEGRA_OPTIMIZATION
642 if (tegra::matchTemplate(img, templ, result, method))
646 #if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
647 if (method == CV_TM_SQDIFF && ipp_sqrDistance(img, templ, result))
651 int cn = img.channels();
652 crossCorr( img, templ, result, result.size(), result.type(), Point(0,0), 0, 0);
654 if( method == CV_TM_CCORR )
657 double invArea = 1./((double)templ.rows * templ.cols);
660 Scalar templMean, templSdv;
661 double *q0 = 0, *q1 = 0, *q2 = 0, *q3 = 0;
662 double templNorm = 0, templSum2 = 0;
664 if( method == CV_TM_CCOEFF )
666 integral(img, sum, CV_64F);
667 templMean = mean(templ);
671 integral(img, sum, sqsum, CV_64F);
672 meanStdDev( templ, templMean, templSdv );
674 templNorm = templSdv[0]*templSdv[0] + templSdv[1]*templSdv[1] + templSdv[2]*templSdv[2] + templSdv[3]*templSdv[3];
676 if( templNorm < DBL_EPSILON && method == CV_TM_CCOEFF_NORMED )
678 result = Scalar::all(1);
682 templSum2 = templNorm + templMean[0]*templMean[0] + templMean[1]*templMean[1] + templMean[2]*templMean[2] + templMean[3]*templMean[3];
686 templMean = Scalar::all(0);
687 templNorm = templSum2;
690 templSum2 /= invArea;
691 templNorm = std::sqrt(templNorm);
692 templNorm /= std::sqrt(invArea); // care of accuracy here
694 q0 = (double*)sqsum.data;
695 q1 = q0 + templ.cols*cn;
696 q2 = (double*)(sqsum.data + templ.rows*sqsum.step);
697 q3 = q2 + templ.cols*cn;
700 double* p0 = (double*)sum.data;
701 double* p1 = p0 + templ.cols*cn;
702 double* p2 = (double*)(sum.data + templ.rows*sum.step);
703 double* p3 = p2 + templ.cols*cn;
705 int sumstep = sum.data ? (int)(sum.step / sizeof(double)) : 0;
706 int sqstep = sqsum.data ? (int)(sqsum.step / sizeof(double)) : 0;
710 for( i = 0; i < result.rows; i++ )
712 float* rrow = (float*)(result.data + i*result.step);
713 int idx = i * sumstep;
714 int idx2 = i * sqstep;
716 for( j = 0; j < result.cols; j++, idx += cn, idx2 += cn )
718 double num = rrow[j], t;
719 double wndMean2 = 0, wndSum2 = 0;
723 for( k = 0; k < cn; k++ )
725 t = p0[idx+k] - p1[idx+k] - p2[idx+k] + p3[idx+k];
727 num -= t*templMean[k];
733 if( isNormed || numType == 2 )
735 for( k = 0; k < cn; k++ )
737 t = q0[idx2+k] - q1[idx2+k] - q2[idx2+k] + q3[idx2+k];
743 num = wndSum2 - 2*num + templSum2;
750 t = std::sqrt(MAX(wndSum2 - wndMean2,0))*templNorm;
753 else if( fabs(num) < t*1.125 )
754 num = num > 0 ? 1 : -1;
756 num = method != CV_TM_SQDIFF_NORMED ? 0 : 1;
759 rrow[j] = (float)num;
766 cvMatchTemplate( const CvArr* _img, const CvArr* _templ, CvArr* _result, int method )
768 cv::Mat img = cv::cvarrToMat(_img), templ = cv::cvarrToMat(_templ),
769 result = cv::cvarrToMat(_result);
770 CV_Assert( result.size() == cv::Size(std::abs(img.cols - templ.cols) + 1,
771 std::abs(img.rows - templ.rows) + 1) &&
772 result.type() == CV_32F );
773 matchTemplate(img, templ, result, method);