1 /*M///////////////////////////////////////////////////////////////////////////////////////
\r
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
\r
5 // By downloading, copying, installing or using the software you agree to this license.
\r
6 // If you do not agree to this license, do not download, install,
\r
7 // copy or use the software.
\r
10 // Intel License Agreement
\r
11 // For Open Source Computer Vision Library
\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
\r
14 // Third party copyrights are property of their respective owners.
\r
16 // Redistribution and use in source and binary forms, with or without modification,
\r
17 // are permitted provided that the following conditions are met:
\r
19 // * Redistribution's of source code must retain the above copyright notice,
\r
20 // this list of conditions and the following disclaimer.
\r
22 // * Redistribution's in binary form must reproduce the above copyright notice,
\r
23 // this list of conditions and the following disclaimer in the documentation
\r
24 // and/or other materials provided with the distribution.
\r
26 // * The name of Intel Corporation may not be used to endorse or promote products
\r
27 // derived from this software without specific prior written permission.
\r
29 // This software is provided by the copyright holders and contributors "as is" and
\r
30 // any express or implied warranties, including, but not limited to, the implied
\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
\r
33 // indirect, incidental, special, exemplary, or consequential damages
\r
34 // (including, but not limited to, procurement of substitute goods or services;
\r
35 // loss of use, data, or profits; or business interruption) however caused
\r
36 // and on any theory of liability, whether in contract, strict liability,
\r
37 // or tort (including negligence or otherwise) arising in any way out of
\r
38 // the use of this software, even if advised of the possibility of such damage.
\r
45 icvCrossCorr( const CvArr* _img, const CvArr* _templ, CvArr* _corr,
\r
46 CvPoint anchor, double delta, int borderType )
\r
48 const double block_scale = 4.5;
\r
49 const int min_block_size = 256;
\r
50 CvMat* dft_img[CV_MAX_THREADS] = {0};
\r
51 CvMat* dft_templ = 0;
\r
52 void* buf[CV_MAX_THREADS] = {0};
\r
53 int k, num_threads = 0;
\r
55 CV_FUNCNAME( "icvCrossCorr" );
\r
59 CvMat istub, *img = (CvMat*)_img;
\r
60 CvMat tstub, *templ = (CvMat*)_templ;
\r
61 CvMat cstub, *corr = (CvMat*)_corr;
\r
62 CvSize dftsize, blocksize;
\r
63 int depth, templ_depth, corr_depth, max_depth = CV_32F,
\r
64 cn, templ_cn, corr_cn, buf_size = 0,
\r
65 tile_count_x, tile_count_y, tile_count;
\r
67 CV_CALL( img = cvGetMat( img, &istub ));
\r
68 CV_CALL( templ = cvGetMat( templ, &tstub ));
\r
69 CV_CALL( corr = cvGetMat( corr, &cstub ));
\r
71 if( CV_MAT_DEPTH( img->type ) != CV_8U &&
\r
72 CV_MAT_DEPTH( img->type ) != CV_16U &&
\r
73 CV_MAT_DEPTH( img->type ) != CV_32F )
\r
74 CV_ERROR( CV_StsUnsupportedFormat,
\r
75 "The function supports only 8u, 16u and 32f data types" );
\r
77 if( !CV_ARE_DEPTHS_EQ( img, templ ) && CV_MAT_DEPTH( templ->type ) != CV_32F )
\r
78 CV_ERROR( CV_StsUnsupportedFormat,
\r
79 "Template (kernel) must be of the same depth as the input image, or be 32f" );
\r
81 if( !CV_ARE_DEPTHS_EQ( img, corr ) && CV_MAT_DEPTH( corr->type ) != CV_32F &&
\r
82 CV_MAT_DEPTH( corr->type ) != CV_64F )
\r
83 CV_ERROR( CV_StsUnsupportedFormat,
\r
84 "The output image must have the same depth as the input image, or be 32f/64f" );
\r
86 if( (!CV_ARE_CNS_EQ( img, corr ) || CV_MAT_CN(templ->type) > 1) &&
\r
87 (CV_MAT_CN( corr->type ) > 1 || !CV_ARE_CNS_EQ( img, templ)) )
\r
88 CV_ERROR( CV_StsUnsupportedFormat,
\r
89 "The output must have the same number of channels as the input (when the template has 1 channel), "
\r
90 "or the output must have 1 channel when the input and the template have the same number of channels" );
\r
92 depth = CV_MAT_DEPTH(img->type);
\r
93 cn = CV_MAT_CN(img->type);
\r
94 templ_depth = CV_MAT_DEPTH(templ->type);
\r
95 templ_cn = CV_MAT_CN(templ->type);
\r
96 corr_depth = CV_MAT_DEPTH(corr->type);
\r
97 corr_cn = CV_MAT_CN(corr->type);
\r
99 CV_Assert( corr_cn == 1 || delta == 0 );
\r
101 max_depth = MAX( max_depth, templ_depth );
\r
102 max_depth = MAX( max_depth, depth );
\r
103 max_depth = MAX( max_depth, corr_depth );
\r
104 if( depth > CV_8U )
\r
105 max_depth = CV_64F;
\r
107 if( img->cols < templ->cols || img->rows < templ->rows )
\r
108 CV_ERROR( CV_StsUnmatchedSizes,
\r
109 "Such a combination of image and template/filter size is not supported" );
\r
111 if( corr->rows > img->rows + templ->rows - 1 ||
\r
112 corr->cols > img->cols + templ->cols - 1 )
\r
113 CV_ERROR( CV_StsUnmatchedSizes,
\r
114 "output image should not be greater than (W + w - 1)x(H + h - 1)" );
\r
116 blocksize.width = cvRound(templ->cols*block_scale);
\r
117 blocksize.width = MAX( blocksize.width, min_block_size - templ->cols + 1 );
\r
118 blocksize.width = MIN( blocksize.width, corr->cols );
\r
119 blocksize.height = cvRound(templ->rows*block_scale);
\r
120 blocksize.height = MAX( blocksize.height, min_block_size - templ->rows + 1 );
\r
121 blocksize.height = MIN( blocksize.height, corr->rows );
\r
123 dftsize.width = cvGetOptimalDFTSize(blocksize.width + templ->cols - 1);
\r
124 if( dftsize.width == 1 )
\r
126 dftsize.height = cvGetOptimalDFTSize(blocksize.height + templ->rows - 1);
\r
127 if( dftsize.width <= 0 || dftsize.height <= 0 )
\r
128 CV_ERROR( CV_StsOutOfRange, "the input arrays are too big" );
\r
130 // recompute block size
\r
131 blocksize.width = dftsize.width - templ->cols + 1;
\r
132 blocksize.width = MIN( blocksize.width, corr->cols );
\r
133 blocksize.height = dftsize.height - templ->rows + 1;
\r
134 blocksize.height = MIN( blocksize.height, corr->rows );
\r
136 CV_CALL( dft_templ = cvCreateMat( dftsize.height*templ_cn, dftsize.width, max_depth ));
\r
138 num_threads = cvGetNumThreads();
\r
140 for( k = 0; k < num_threads; k++ )
\r
141 CV_CALL( dft_img[k] = cvCreateMat( dftsize.height, dftsize.width, max_depth ));
\r
143 if( templ_cn > 1 && templ_depth != max_depth )
\r
144 buf_size = templ->cols*templ->rows*CV_ELEM_SIZE(templ_depth);
\r
146 if( cn > 1 && depth != max_depth )
\r
147 buf_size = MAX( buf_size, (blocksize.width + templ->cols - 1)*
\r
148 (blocksize.height + templ->rows - 1)*CV_ELEM_SIZE(depth));
\r
150 if( (corr_cn > 1 || cn > 1) && corr_depth != max_depth )
\r
151 buf_size = MAX( buf_size, blocksize.width*blocksize.height*CV_ELEM_SIZE(corr_depth));
\r
155 for( k = 0; k < num_threads; k++ )
\r
156 CV_CALL( buf[k] = cvAlloc(buf_size) );
\r
159 // compute DFT of each template plane
\r
160 for( k = 0; k < templ_cn; k++ )
\r
162 CvMat dstub, *src, *dst, temp;
\r
163 CvMat* planes[] = { 0, 0, 0, 0 };
\r
164 int yofs = k*dftsize.height;
\r
167 dst = cvGetSubRect( dft_templ, &dstub, cvRect(0,yofs,templ->cols,templ->rows));
\r
171 planes[k] = templ_depth == max_depth ? dst :
\r
172 cvInitMatHeader( &temp, templ->rows, templ->cols, templ_depth, buf[0] );
\r
173 cvSplit( templ, planes[0], planes[1], planes[2], planes[3] );
\r
179 cvConvert( src, dst );
\r
181 if( dft_templ->cols > templ->cols )
\r
183 cvGetSubRect( dft_templ, dst, cvRect(templ->cols, yofs,
\r
184 dft_templ->cols - templ->cols, templ->rows) );
\r
187 cvGetSubRect( dft_templ, dst, cvRect(0,yofs,dftsize.width,dftsize.height) );
\r
188 cvDFT( dst, dst, CV_DXT_FORWARD + CV_DXT_SCALE, templ->rows );
\r
191 tile_count_x = (corr->cols + blocksize.width - 1)/blocksize.width;
\r
192 tile_count_y = (corr->rows + blocksize.height - 1)/blocksize.height;
\r
193 tile_count = tile_count_x*tile_count_y;
\r
197 #pragma omp parallel for num_threads(num_threads) schedule(dynamic)
\r
199 // calculate correlation by blocks
\r
200 for( k = 0; k < tile_count; k++ )
\r
202 int thread_idx = cvGetThreadNum();
\r
203 int x = (k%tile_count_x)*blocksize.width;
\r
204 int y = (k/tile_count_x)*blocksize.height;
\r
206 CvMat sstub, dstub, *src, *dst, temp;
\r
207 CvMat* planes[] = { 0, 0, 0, 0 };
\r
208 CvMat* _dft_img = dft_img[thread_idx];
\r
209 void* _buf = buf[thread_idx];
\r
210 CvSize csz = { blocksize.width, blocksize.height }, isz;
\r
211 int x0 = x - anchor.x, y0 = y - anchor.y;
\r
212 int x1 = MAX( 0, x0 ), y1 = MAX( 0, y0 ), x2, y2;
\r
213 csz.width = MIN( csz.width, corr->cols - x );
\r
214 csz.height = MIN( csz.height, corr->rows - y );
\r
215 isz.width = csz.width + templ->cols - 1;
\r
216 isz.height = csz.height + templ->rows - 1;
\r
217 x2 = MIN( img->cols, x0 + isz.width );
\r
218 y2 = MIN( img->rows, y0 + isz.height );
\r
220 for( i = 0; i < cn; i++ )
\r
222 CvMat dstub1, *dst1;
\r
223 yofs = i*dftsize.height;
\r
225 src = cvGetSubRect( img, &sstub, cvRect(x1,y1,x2-x1,y2-y1) );
\r
226 dst = cvGetSubRect( _dft_img, &dstub,
\r
227 cvRect(0,0,isz.width,isz.height) );
\r
230 if( x2 - x1 < isz.width || y2 - y1 < isz.height )
\r
231 dst1 = cvGetSubRect( _dft_img, &dstub1,
\r
232 cvRect( x1 - x0, y1 - y0, x2 - x1, y2 - y1 ));
\r
237 if( depth != max_depth )
\r
238 planes[i] = cvInitMatHeader( &temp, y2 - y1, x2 - x1, depth, _buf );
\r
239 cvSplit( src, planes[0], planes[1], planes[2], planes[3] );
\r
245 cvConvert( src, dst1 );
\r
248 cvCopyMakeBorder( dst1, dst, cvPoint(x1 - x0, y1 - y0), borderType );
\r
250 if( dftsize.width > isz.width )
\r
252 cvGetSubRect( _dft_img, dst, cvRect(isz.width, 0,
\r
253 dftsize.width - isz.width,dftsize.height) );
\r
257 cvDFT( _dft_img, _dft_img, CV_DXT_FORWARD, isz.height );
\r
258 cvGetSubRect( dft_templ, dst,
\r
259 cvRect(0,(templ_cn>1?yofs:0),dftsize.width,dftsize.height) );
\r
261 cvMulSpectrums( _dft_img, dst, _dft_img, CV_DXT_MUL_CONJ );
\r
262 cvDFT( _dft_img, _dft_img, CV_DXT_INVERSE, csz.height );
\r
264 src = cvGetSubRect( _dft_img, &sstub, cvRect(0,0,csz.width,csz.height) );
\r
265 dst = cvGetSubRect( corr, &dstub, cvRect(x,y,csz.width,csz.height) );
\r
270 if( corr_depth != max_depth )
\r
272 planes[i] = cvInitMatHeader( &temp, csz.height, csz.width,
\r
273 corr_depth, _buf );
\r
274 cvConvertScale( src, planes[i], 1, delta );
\r
276 cvMerge( planes[0], planes[1], planes[2], planes[3], dst );
\r
282 cvConvertScale( src, dst, 1, delta );
\r
285 if( max_depth > corr_depth )
\r
287 cvInitMatHeader( &temp, csz.height, csz.width,
\r
288 corr_depth, _buf );
\r
289 cvConvert( src, &temp );
\r
301 cvReleaseMat( &dft_templ );
\r
303 for( k = 0; k < num_threads; k++ )
\r
305 cvReleaseMat( &dft_img[k] );
\r
311 cv::crossCorr( const Mat& img, const Mat& templ, Mat& corr,
\r
312 Point anchor, double delta, int borderType )
\r
314 CvMat _img = img, _templ = templ, _corr = corr;
\r
315 icvCrossCorr( &_img, &_templ, &_corr, anchor, delta, borderType );
\r
319 /*****************************************************************************************/
\r
322 cvMatchTemplate( const CvArr* _img, const CvArr* _templ, CvArr* _result, int method )
\r
327 CV_FUNCNAME( "cvMatchTemplate" );
\r
331 int coi1 = 0, coi2 = 0;
\r
334 CvMat stub, *img = (CvMat*)_img;
\r
335 CvMat tstub, *templ = (CvMat*)_templ;
\r
336 CvMat rstub, *result = (CvMat*)_result;
\r
337 CvScalar templ_mean = cvScalarAll(0);
\r
338 double templ_norm = 0, templ_sum2 = 0;
\r
340 int idx = 0, idx2 = 0;
\r
341 double *p0, *p1, *p2, *p3;
\r
342 double *q0, *q1, *q2, *q3;
\r
344 int sum_step, sqsum_step;
\r
345 int num_type = method == CV_TM_CCORR || method == CV_TM_CCORR_NORMED ? 0 :
\r
346 method == CV_TM_CCOEFF || method == CV_TM_CCOEFF_NORMED ? 1 : 2;
\r
347 int is_normed = method == CV_TM_CCORR_NORMED ||
\r
348 method == CV_TM_SQDIFF_NORMED ||
\r
349 method == CV_TM_CCOEFF_NORMED;
\r
351 CV_CALL( img = cvGetMat( img, &stub, &coi1 ));
\r
352 CV_CALL( templ = cvGetMat( templ, &tstub, &coi2 ));
\r
353 CV_CALL( result = cvGetMat( result, &rstub ));
\r
355 if( CV_MAT_DEPTH( img->type ) != CV_8U &&
\r
356 CV_MAT_DEPTH( img->type ) != CV_32F )
\r
357 CV_ERROR( CV_StsUnsupportedFormat,
\r
358 "The function supports only 8u and 32f data types" );
\r
360 if( !CV_ARE_TYPES_EQ( img, templ ))
\r
361 CV_ERROR( CV_StsUnmatchedSizes, "image and template should have the same type" );
\r
363 if( CV_MAT_TYPE( result->type ) != CV_32FC1 )
\r
364 CV_ERROR( CV_StsUnsupportedFormat, "output image should have 32f type" );
\r
366 if( img->rows < templ->rows || img->cols < templ->cols )
\r
369 CV_SWAP( img, templ, t );
\r
372 if( result->rows != img->rows - templ->rows + 1 ||
\r
373 result->cols != img->cols - templ->cols + 1 )
\r
374 CV_ERROR( CV_StsUnmatchedSizes, "output image should be (W - w + 1)x(H - h + 1)" );
\r
376 if( method < CV_TM_SQDIFF || method > CV_TM_CCOEFF_NORMED )
\r
377 CV_ERROR( CV_StsBadArg, "unknown comparison method" );
\r
379 depth = CV_MAT_DEPTH(img->type);
\r
380 cn = CV_MAT_CN(img->type);
\r
382 /*if( is_normed && cn == 1 && templ->rows > 8 && templ->cols > 8 &&
\r
383 img->rows > templ->cols && img->cols > templ->cols )
\r
385 CvTemplMatchIPPFunc ipp_func =
\r
387 (method == CV_TM_SQDIFF_NORMED ? (CvTemplMatchIPPFunc)icvSqrDistanceValid_Norm_8u32f_C1R_p :
\r
388 method == CV_TM_CCORR_NORMED ? (CvTemplMatchIPPFunc)icvCrossCorrValid_Norm_8u32f_C1R_p :
\r
389 (CvTemplMatchIPPFunc)icvCrossCorrValid_NormLevel_8u32f_C1R_p) :
\r
390 (method == CV_TM_SQDIFF_NORMED ? (CvTemplMatchIPPFunc)icvSqrDistanceValid_Norm_32f_C1R_p :
\r
391 method == CV_TM_CCORR_NORMED ? (CvTemplMatchIPPFunc)icvCrossCorrValid_Norm_32f_C1R_p :
\r
392 (CvTemplMatchIPPFunc)icvCrossCorrValid_NormLevel_32f_C1R_p);
\r
396 CvSize img_size = cvGetMatSize(img), templ_size = cvGetMatSize(templ);
\r
398 IPPI_CALL( ipp_func( img->data.ptr, img->step ? img->step : CV_STUB_STEP,
\r
399 img_size, templ->data.ptr,
\r
400 templ->step ? templ->step : CV_STUB_STEP,
\r
401 templ_size, result->data.ptr,
\r
402 result->step ? result->step : CV_STUB_STEP ));
\r
403 for( i = 0; i < result->rows; i++ )
\r
405 float* rrow = (float*)(result->data.ptr + i*result->step);
\r
406 for( j = 0; j < result->cols; j++ )
\r
408 if( fabs(rrow[j]) > 1. )
\r
409 rrow[j] = rrow[j] < 0 ? -1.f : 1.f;
\r
416 CV_CALL( icvCrossCorr( img, templ, result ));
\r
418 if( method == CV_TM_CCORR )
\r
421 inv_area = 1./((double)templ->rows * templ->cols);
\r
423 CV_CALL( sum = cvCreateMat( img->rows + 1, img->cols + 1,
\r
424 CV_MAKETYPE( CV_64F, cn )));
\r
425 if( method == CV_TM_CCOEFF )
\r
427 CV_CALL( cvIntegral( img, sum, 0, 0 ));
\r
428 CV_CALL( templ_mean = cvAvg( templ ));
\r
429 q0 = q1 = q2 = q3 = 0;
\r
433 CvScalar _templ_sdv = cvScalarAll(0);
\r
434 CV_CALL( sqsum = cvCreateMat( img->rows + 1, img->cols + 1,
\r
435 CV_MAKETYPE( CV_64F, cn )));
\r
436 CV_CALL( cvIntegral( img, sum, sqsum, 0 ));
\r
437 CV_CALL( cvAvgSdv( templ, &templ_mean, &_templ_sdv ));
\r
439 templ_norm = CV_SQR(_templ_sdv.val[0]) + CV_SQR(_templ_sdv.val[1]) +
\r
440 CV_SQR(_templ_sdv.val[2]) + CV_SQR(_templ_sdv.val[3]);
\r
442 if( templ_norm < DBL_EPSILON && method == CV_TM_CCOEFF_NORMED )
\r
444 cvSet( result, cvScalarAll(1.) );
\r
448 templ_sum2 = templ_norm +
\r
449 CV_SQR(templ_mean.val[0]) + CV_SQR(templ_mean.val[1]) +
\r
450 CV_SQR(templ_mean.val[2]) + CV_SQR(templ_mean.val[3]);
\r
452 if( num_type != 1 )
\r
454 templ_mean = cvScalarAll(0);
\r
455 templ_norm = templ_sum2;
\r
458 templ_sum2 /= inv_area;
\r
459 templ_norm = sqrt(templ_norm);
\r
460 templ_norm /= sqrt(inv_area); // care of accuracy here
\r
462 q0 = (double*)sqsum->data.ptr;
\r
463 q1 = q0 + templ->cols*cn;
\r
464 q2 = (double*)(sqsum->data.ptr + templ->rows*sqsum->step);
\r
465 q3 = q2 + templ->cols*cn;
\r
468 p0 = (double*)sum->data.ptr;
\r
469 p1 = p0 + templ->cols*cn;
\r
470 p2 = (double*)(sum->data.ptr + templ->rows*sum->step);
\r
471 p3 = p2 + templ->cols*cn;
\r
473 sum_step = sum ? sum->step / sizeof(double) : 0;
\r
474 sqsum_step = sqsum ? sqsum->step / sizeof(double) : 0;
\r
476 for( i = 0; i < result->rows; i++ )
\r
478 float* rrow = (float*)(result->data.ptr + i*result->step);
\r
479 idx = i * sum_step;
\r
480 idx2 = i * sqsum_step;
\r
482 for( j = 0; j < result->cols; j++, idx += cn, idx2 += cn )
\r
484 double num = rrow[j], t;
\r
485 double wnd_mean2 = 0, wnd_sum2 = 0;
\r
487 if( num_type == 1 )
\r
489 for( k = 0; k < cn; k++ )
\r
491 t = p0[idx+k] - p1[idx+k] - p2[idx+k] + p3[idx+k];
\r
492 wnd_mean2 += CV_SQR(t);
\r
493 num -= t*templ_mean.val[k];
\r
496 wnd_mean2 *= inv_area;
\r
499 if( is_normed || num_type == 2 )
\r
501 for( k = 0; k < cn; k++ )
\r
503 t = q0[idx2+k] - q1[idx2+k] - q2[idx2+k] + q3[idx2+k];
\r
507 if( num_type == 2 )
\r
508 num = wnd_sum2 - 2*num + templ_sum2;
\r
513 t = sqrt(MAX(wnd_sum2 - wnd_mean2,0))*templ_norm;
\r
514 if( t > DBL_EPSILON )
\r
517 if( fabs(num) > 1. )
\r
518 num = num > 0 ? 1 : -1;
\r
521 num = method != CV_TM_SQDIFF_NORMED || num < DBL_EPSILON ? 0 : 1;
\r
524 rrow[j] = (float)num;
\r
530 cvReleaseMat( &sum );
\r
531 cvReleaseMat( &sqsum );
\r
534 void cv::matchTemplate( const Mat& image, const Mat& templ, Mat& result, int method )
\r
536 result.create( std::abs(image.rows - templ.rows) + 1,
\r
537 std::abs(image.cols - templ.cols) + 1, CV_32F );
\r
538 CvMat _image = image, _templ = templ, _result = result;
\r
539 cvMatchTemplate( &_image, &_templ, &_result, method );
\r