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.
44 static const char* templmatch_param_names[] = { "template_size", "method", "size", "channels", "depth", 0 };
45 static const int templmatch_depths[] = { CV_8U, CV_32F, -1 };
46 static const int templmatch_channels[] = { 1, 3, -1 };
48 static const CvSize templmatch_sizes[] = {{320, 240}, {1024,768}, {-1,-1}};
49 static const CvSize templmatch_whole_sizes[] = {{320,240}, {1024,768}, {-1,-1}};
50 static const CvSize templmatch_template_sizes[] = {{15,15}, {60,60}, {-1,-1}};
51 static const char* templmatch_methods[] = { "sqdiff", "sqdiff_norm", "ccorr", "ccorr_normed", "ccoeff", "ccoeff_normed", 0 };
53 class CV_TemplMatchTest : public CvArrTest
59 int read_params( CvFileStorage* fs );
60 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
61 void get_minmax_bounds( int i, int j, int type, CvScalar* low, CvScalar* high );
62 double get_success_error_level( int test_case_idx, int i, int j );
64 void prepare_to_validation( int );
66 int write_default_params(CvFileStorage* fs);
67 void get_timing_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types,
68 CvSize** whole_sizes, bool *are_images );
69 void print_timing_params( int test_case_idx, char* ptr, int params_left );
71 int max_template_size;
76 CV_TemplMatchTest::CV_TemplMatchTest()
77 : CvArrTest( "match-template", "cvMatchTemplate", "" )
79 test_array[INPUT].push(NULL);
80 test_array[INPUT].push(NULL);
81 test_array[OUTPUT].push(NULL);
82 test_array[REF_OUTPUT].push(NULL);
83 element_wise_relative_error = false;
84 max_template_size = 100;
87 size_list = templmatch_sizes;
88 whole_size_list = templmatch_whole_sizes;
89 cn_list = templmatch_channels;
90 depth_list = templmatch_depths;
92 default_timing_param_names = templmatch_param_names;
96 int CV_TemplMatchTest::read_params( CvFileStorage* fs )
98 int code = CvArrTest::read_params( fs );
102 if( ts->get_testing_mode() == CvTS::CORRECTNESS_CHECK_MODE )
104 max_template_size = cvReadInt( find_param( fs, "max_template_size" ), max_template_size );
105 max_template_size = cvTsClipInt( max_template_size, 1, 100 );
112 int CV_TemplMatchTest::write_default_params( CvFileStorage* fs )
114 int code = CvArrTest::write_default_params( fs );
118 if( ts->get_testing_mode() == CvTS::CORRECTNESS_CHECK_MODE )
120 write_param( fs, "max_template_size", max_template_size );
125 start_write_param( fs );
127 cvStartWriteStruct( fs, "template_size", CV_NODE_SEQ+CV_NODE_FLOW );
128 for( i = 0; templmatch_template_sizes[i].width >= 0; i++ )
130 cvStartWriteStruct( fs, 0, CV_NODE_SEQ+CV_NODE_FLOW );
131 cvWriteInt( fs, 0, templmatch_template_sizes[i].width );
132 cvWriteInt( fs, 0, templmatch_template_sizes[i].height );
133 cvEndWriteStruct(fs);
135 cvEndWriteStruct(fs);
137 write_string_list( fs, "method", templmatch_methods );
144 void CV_TemplMatchTest::get_minmax_bounds( int i, int j, int type, CvScalar* low, CvScalar* high )
146 CvArrTest::get_minmax_bounds( i, j, type, low, high );
147 int depth = CV_MAT_DEPTH(type);
148 if( depth == CV_32F )
150 *low = cvScalarAll(-10.);
151 *high = cvScalarAll(10.);
156 void CV_TemplMatchTest::get_test_array_types_and_sizes( int test_case_idx,
157 CvSize** sizes, int** types )
159 CvRNG* rng = ts->get_rng();
160 int depth = cvTsRandInt(rng) % 2, cn = cvTsRandInt(rng) & 1 ? 3 : 1;
161 CvArrTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
162 depth = depth == 0 ? CV_8U : CV_32F;
164 types[INPUT][0] = types[INPUT][1] = CV_MAKETYPE(depth,cn);
165 types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_32FC1;
167 sizes[INPUT][1].width = cvTsRandInt(rng)%MIN(sizes[INPUT][1].width,max_template_size) + 1;
168 sizes[INPUT][1].height = cvTsRandInt(rng)%MIN(sizes[INPUT][1].height,max_template_size) + 1;
169 sizes[OUTPUT][0].width = sizes[INPUT][0].width - sizes[INPUT][1].width + 1;
170 sizes[OUTPUT][0].height = sizes[INPUT][0].height - sizes[INPUT][1].height + 1;
171 sizes[REF_OUTPUT][0] = sizes[OUTPUT][0];
173 method = cvTsRandInt(rng)%6;
177 void CV_TemplMatchTest::get_timing_test_array_types_and_sizes( int test_case_idx,
178 CvSize** sizes, int** types, CvSize** whole_sizes, bool *are_images )
180 CvArrTest::get_timing_test_array_types_and_sizes( test_case_idx, sizes, types,
181 whole_sizes, are_images );
182 const char* method_str = cvReadString( find_timing_param( "method" ), "ccorr" );
183 const CvFileNode* node = find_timing_param( "template_size" );
184 CvSize templ_size, result_size;
186 assert( node && CV_NODE_IS_SEQ( node->tag ));
188 method = strncmp( method_str, "sqdiff", 6 ) == 0 ? CV_TM_SQDIFF :
189 strncmp( method_str, "ccorr", 5 ) == 0 ? CV_TM_CCORR : CV_TM_CCOEFF;
190 method += strstr( method_str, "_normed" ) != 0;
192 cvReadRawData( ts->get_file_storage(), node, &templ_size, "2i" );
194 sizes[INPUT][1] = whole_sizes[INPUT][1] = templ_size;
195 result_size.width = sizes[INPUT][0].width - templ_size.width + 1;
196 result_size.height = sizes[INPUT][0].height - templ_size.height + 1;
197 assert( result_size.width > 0 && result_size.height > 0 );
198 sizes[OUTPUT][0] = whole_sizes[OUTPUT][0] = result_size;
200 types[OUTPUT][0] = CV_32FC1;
204 void CV_TemplMatchTest::print_timing_params( int test_case_idx, char* ptr, int params_left )
206 sprintf( ptr, "%s,", cvReadString( find_timing_param( "method" ), "ccorr" ) );
208 sprintf( ptr, "templ_size=%dx%d,", test_mat[INPUT][1].width, test_mat[INPUT][1].height );
212 CvArrTest::print_timing_params( test_case_idx, ptr, params_left );
216 double CV_TemplMatchTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
218 if( CV_MAT_DEPTH(test_mat[INPUT][1].type) == CV_8U ||
219 method >= CV_TM_CCOEFF && test_mat[INPUT][1].cols*test_mat[INPUT][1].rows <= 2 )
226 void CV_TemplMatchTest::run_func()
228 cvMatchTemplate( test_array[INPUT][0], test_array[INPUT][1], test_array[OUTPUT][0], method );
232 static void cvTsMatchTemplate( const CvMat* img, const CvMat* templ, CvMat* result, int method )
235 int depth = CV_MAT_DEPTH(img->type), cn = CV_MAT_CN(img->type);
236 int width_n = templ->cols*cn, height = templ->rows;
237 int a_step = img->step / CV_ELEM_SIZE(img->type & CV_MAT_DEPTH_MASK);
238 int b_step = templ->step / CV_ELEM_SIZE(templ->type & CV_MAT_DEPTH_MASK);
239 CvScalar b_mean, b_sdv;
240 double b_denom = 1., b_sum2 = 0;
241 int area = templ->rows*templ->cols;
243 cvTsMeanStdDevNonZero( templ, 0, &b_mean, &b_sdv, 0 );
245 for( i = 0; i < cn; i++ )
246 b_sum2 += (b_sdv.val[i]*b_sdv.val[i] + b_mean.val[i]*b_mean.val[i])*area;
248 if( CV_SQR(b_sdv.val[0]) + CV_SQR(b_sdv.val[1]) +
249 CV_SQR(b_sdv.val[2]) + CV_SQR(b_sdv.val[3]) < DBL_EPSILON &&
250 method == CV_TM_CCOEFF_NORMED )
252 cvSet( result, cvScalarAll(1.) );
259 if( method != CV_TM_CCOEFF_NORMED )
265 for( i = 0; i < cn; i++ )
266 b_denom += b_sdv.val[i]*b_sdv.val[i]*area;
268 b_denom = sqrt(b_denom);
273 assert( CV_TM_SQDIFF <= method && method <= CV_TM_CCOEFF_NORMED );
275 for( i = 0; i < result->rows; i++ )
277 for( j = 0; j < result->cols; j++ )
279 CvScalar a_sum = {{ 0, 0, 0, 0 }}, a_sum2 = {{ 0, 0, 0, 0 }};
280 CvScalar ccorr = {{ 0, 0, 0, 0 }};
285 const uchar* a = img->data.ptr + i*img->step + j*cn;
286 const uchar* b = templ->data.ptr;
288 if( cn == 1 || method < CV_TM_CCOEFF )
290 for( k = 0; k < height; k++, a += a_step, b += b_step )
291 for( l = 0; l < width_n; l++ )
293 ccorr.val[0] += a[l]*b[l];
294 a_sum.val[0] += a[l];
295 a_sum2.val[0] += a[l]*a[l];
300 for( k = 0; k < height; k++, a += a_step, b += b_step )
301 for( l = 0; l < width_n; l += 3 )
303 ccorr.val[0] += a[l]*b[l];
304 ccorr.val[1] += a[l+1]*b[l+1];
305 ccorr.val[2] += a[l+2]*b[l+2];
306 a_sum.val[0] += a[l];
307 a_sum.val[1] += a[l+1];
308 a_sum.val[2] += a[l+2];
309 a_sum2.val[0] += a[l]*a[l];
310 a_sum2.val[1] += a[l+1]*a[l+1];
311 a_sum2.val[2] += a[l+2]*a[l+2];
317 const float* a = (const float*)(img->data.ptr + i*img->step) + j*cn;
318 const float* b = (const float*)templ->data.ptr;
320 if( cn == 1 || method < CV_TM_CCOEFF )
322 for( k = 0; k < height; k++, a += a_step, b += b_step )
323 for( l = 0; l < width_n; l++ )
325 ccorr.val[0] += a[l]*b[l];
326 a_sum.val[0] += a[l];
327 a_sum2.val[0] += a[l]*a[l];
332 for( k = 0; k < height; k++, a += a_step, b += b_step )
333 for( l = 0; l < width_n; l += 3 )
335 ccorr.val[0] += a[l]*b[l];
336 ccorr.val[1] += a[l+1]*b[l+1];
337 ccorr.val[2] += a[l+2]*b[l+2];
338 a_sum.val[0] += a[l];
339 a_sum.val[1] += a[l+1];
340 a_sum.val[2] += a[l+2];
341 a_sum2.val[0] += a[l]*a[l];
342 a_sum2.val[1] += a[l+1]*a[l+1];
343 a_sum2.val[2] += a[l+2]*a[l+2];
351 case CV_TM_CCORR_NORMED:
352 value = ccorr.val[0];
355 case CV_TM_SQDIFF_NORMED:
356 value = (a_sum2.val[0] + b_sum2 - 2*ccorr.val[0]);
359 value = (ccorr.val[0] - a_sum.val[0]*b_mean.val[0]+
360 ccorr.val[1] - a_sum.val[1]*b_mean.val[1]+
361 ccorr.val[2] - a_sum.val[2]*b_mean.val[2]);
369 if( method != CV_TM_CCOEFF_NORMED )
371 denom = a_sum2.val[0] + a_sum2.val[1] + a_sum2.val[2];
375 denom = a_sum2.val[0] - (a_sum.val[0]*a_sum.val[0])/area;
376 denom += a_sum2.val[1] - (a_sum.val[1]*a_sum.val[1])/area;
377 denom += a_sum2.val[2] - (a_sum.val[2]*a_sum.val[2])/area;
379 denom = sqrt(MAX(denom,0))*b_denom;
380 if( denom > DBL_EPSILON )
383 if( fabs(value) > 1 )
384 value = value < 0 ? -1. : 1.;
387 value = method != CV_TM_SQDIFF_NORMED || value < DBL_EPSILON ? 0 : 1;
390 ((float*)(result->data.ptr + result->step*i))[j] = (float)value;
396 void CV_TemplMatchTest::prepare_to_validation( int /*test_case_idx*/ )
398 cvTsMatchTemplate( &test_mat[INPUT][0], &test_mat[INPUT][1],
399 &test_mat[REF_OUTPUT][0], method );
401 //if( ts->get_current_test_info()->test_case_idx == 0 )
403 CvFileStorage* fs = cvOpenFileStorage( "_match_template.yml", 0, CV_STORAGE_WRITE );
404 cvWrite( fs, "image", &test_mat[INPUT][0] );
405 cvWrite( fs, "template", &test_mat[INPUT][1] );
406 cvWrite( fs, "ref", &test_mat[REF_OUTPUT][0] );
407 cvWrite( fs, "opencv", &test_mat[OUTPUT][0] );
408 cvWriteInt( fs, "method", method );
409 cvReleaseFileStorage( &fs );
412 if( method >= CV_TM_CCOEFF )
414 // avoid numerical stability problems in singular cases (when the results are near to 0)
415 const double delta = 10.;
416 cvTsAdd( &test_mat[REF_OUTPUT][0], cvScalar(1.), 0, cvScalar(0.),
417 cvScalar(delta), &test_mat[REF_OUTPUT][0], 0 );
418 cvTsAdd( &test_mat[OUTPUT][0], cvScalar(1.), 0, cvScalar(0.),
419 cvScalar(delta), &test_mat[OUTPUT][0], 0 );
424 CV_TemplMatchTest templ_match;