1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include "test_precomp.hpp"
43
44 using namespace cv;
45 using namespace std;
46
47 class CV_TemplMatchTest : public cvtest::ArrayTest
48 {
49 public:
50 CV_TemplMatchTest();
51
52 protected:
53 int read_params( CvFileStorage* fs );
54 void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
55 void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
56 double get_success_error_level( int test_case_idx, int i, int j );
57 void run_func();
58 void prepare_to_validation( int );
59
60 int max_template_size;
61 int method;
62 bool test_cpp;
63 };
64
65
CV_TemplMatchTest()66 CV_TemplMatchTest::CV_TemplMatchTest()
67 {
68 test_array[INPUT].push_back(NULL);
69 test_array[INPUT].push_back(NULL);
70 test_array[OUTPUT].push_back(NULL);
71 test_array[REF_OUTPUT].push_back(NULL);
72 element_wise_relative_error = false;
73 max_template_size = 100;
74 method = 0;
75 test_cpp = false;
76 }
77
78
read_params(CvFileStorage * fs)79 int CV_TemplMatchTest::read_params( CvFileStorage* fs )
80 {
81 int code = cvtest::ArrayTest::read_params( fs );
82 if( code < 0 )
83 return code;
84
85 max_template_size = cvReadInt( find_param( fs, "max_template_size" ), max_template_size );
86 max_template_size = cvtest::clipInt( max_template_size, 1, 100 );
87
88 return code;
89 }
90
91
get_minmax_bounds(int i,int j,int type,Scalar & low,Scalar & high)92 void CV_TemplMatchTest::get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high )
93 {
94 cvtest::ArrayTest::get_minmax_bounds( i, j, type, low, high );
95 int depth = CV_MAT_DEPTH(type);
96 if( depth == CV_32F )
97 {
98 low = Scalar::all(-10.);
99 high = Scalar::all(10.);
100 }
101 }
102
103
get_test_array_types_and_sizes(int test_case_idx,vector<vector<Size>> & sizes,vector<vector<int>> & types)104 void CV_TemplMatchTest::get_test_array_types_and_sizes( int test_case_idx,
105 vector<vector<Size> >& sizes, vector<vector<int> >& types )
106 {
107 RNG& rng = ts->get_rng();
108 int depth = cvtest::randInt(rng) % 2, cn = cvtest::randInt(rng) & 1 ? 3 : 1;
109 cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
110 depth = depth == 0 ? CV_8U : CV_32F;
111
112 types[INPUT][0] = types[INPUT][1] = CV_MAKETYPE(depth,cn);
113 types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_32FC1;
114
115 sizes[INPUT][1].width = cvtest::randInt(rng)%MIN(sizes[INPUT][1].width,max_template_size) + 1;
116 sizes[INPUT][1].height = cvtest::randInt(rng)%MIN(sizes[INPUT][1].height,max_template_size) + 1;
117 sizes[OUTPUT][0].width = sizes[INPUT][0].width - sizes[INPUT][1].width + 1;
118 sizes[OUTPUT][0].height = sizes[INPUT][0].height - sizes[INPUT][1].height + 1;
119 sizes[REF_OUTPUT][0] = sizes[OUTPUT][0];
120
121 method = cvtest::randInt(rng)%6;
122 test_cpp = (cvtest::randInt(rng) & 256) == 0;
123 }
124
125
get_success_error_level(int,int,int)126 double CV_TemplMatchTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
127 {
128 if( test_mat[INPUT][1].depth() == CV_8U ||
129 (method >= CV_TM_CCOEFF && test_mat[INPUT][1].cols*test_mat[INPUT][1].rows <= 2) )
130 return 1e-2;
131 else
132 return 1e-3;
133 }
134
135
run_func()136 void CV_TemplMatchTest::run_func()
137 {
138 if(!test_cpp)
139 cvMatchTemplate( test_array[INPUT][0], test_array[INPUT][1], test_array[OUTPUT][0], method );
140 else
141 {
142 cv::Mat _out = cv::cvarrToMat(test_array[OUTPUT][0]);
143 cv::matchTemplate(cv::cvarrToMat(test_array[INPUT][0]), cv::cvarrToMat(test_array[INPUT][1]), _out, method);
144 }
145 }
146
147
cvTsMatchTemplate(const CvMat * img,const CvMat * templ,CvMat * result,int method)148 static void cvTsMatchTemplate( const CvMat* img, const CvMat* templ, CvMat* result, int method )
149 {
150 int i, j, k, l;
151 int depth = CV_MAT_DEPTH(img->type), cn = CV_MAT_CN(img->type);
152 int width_n = templ->cols*cn, height = templ->rows;
153 int a_step = img->step / CV_ELEM_SIZE(img->type & CV_MAT_DEPTH_MASK);
154 int b_step = templ->step / CV_ELEM_SIZE(templ->type & CV_MAT_DEPTH_MASK);
155 CvScalar b_mean, b_sdv;
156 double b_denom = 1., b_sum2 = 0;
157 int area = templ->rows*templ->cols;
158
159 cvAvgSdv(templ, &b_mean, &b_sdv);
160
161 for( i = 0; i < cn; i++ )
162 b_sum2 += (b_sdv.val[i]*b_sdv.val[i] + b_mean.val[i]*b_mean.val[i])*area;
163
164 if( b_sdv.val[0]*b_sdv.val[0] + b_sdv.val[1]*b_sdv.val[1] +
165 b_sdv.val[2]*b_sdv.val[2] + b_sdv.val[3]*b_sdv.val[3] < DBL_EPSILON &&
166 method == CV_TM_CCOEFF_NORMED )
167 {
168 cvSet( result, cvScalarAll(1.) );
169 return;
170 }
171
172 if( method & 1 )
173 {
174 b_denom = 0;
175 if( method != CV_TM_CCOEFF_NORMED )
176 {
177 b_denom = b_sum2;
178 }
179 else
180 {
181 for( i = 0; i < cn; i++ )
182 b_denom += b_sdv.val[i]*b_sdv.val[i]*area;
183 }
184 b_denom = sqrt(b_denom);
185 if( b_denom == 0 )
186 b_denom = 1.;
187 }
188
189 assert( CV_TM_SQDIFF <= method && method <= CV_TM_CCOEFF_NORMED );
190
191 for( i = 0; i < result->rows; i++ )
192 {
193 for( j = 0; j < result->cols; j++ )
194 {
195 CvScalar a_sum(0), a_sum2(0);
196 CvScalar ccorr(0);
197 double value = 0.;
198
199 if( depth == CV_8U )
200 {
201 const uchar* a = img->data.ptr + i*img->step + j*cn;
202 const uchar* b = templ->data.ptr;
203
204 if( cn == 1 || method < CV_TM_CCOEFF )
205 {
206 for( k = 0; k < height; k++, a += a_step, b += b_step )
207 for( l = 0; l < width_n; l++ )
208 {
209 ccorr.val[0] += a[l]*b[l];
210 a_sum.val[0] += a[l];
211 a_sum2.val[0] += a[l]*a[l];
212 }
213 }
214 else
215 {
216 for( k = 0; k < height; k++, a += a_step, b += b_step )
217 for( l = 0; l < width_n; l += 3 )
218 {
219 ccorr.val[0] += a[l]*b[l];
220 ccorr.val[1] += a[l+1]*b[l+1];
221 ccorr.val[2] += a[l+2]*b[l+2];
222 a_sum.val[0] += a[l];
223 a_sum.val[1] += a[l+1];
224 a_sum.val[2] += a[l+2];
225 a_sum2.val[0] += a[l]*a[l];
226 a_sum2.val[1] += a[l+1]*a[l+1];
227 a_sum2.val[2] += a[l+2]*a[l+2];
228 }
229 }
230 }
231 else
232 {
233 const float* a = (const float*)(img->data.ptr + i*img->step) + j*cn;
234 const float* b = (const float*)templ->data.ptr;
235
236 if( cn == 1 || method < CV_TM_CCOEFF )
237 {
238 for( k = 0; k < height; k++, a += a_step, b += b_step )
239 for( l = 0; l < width_n; l++ )
240 {
241 ccorr.val[0] += a[l]*b[l];
242 a_sum.val[0] += a[l];
243 a_sum2.val[0] += a[l]*a[l];
244 }
245 }
246 else
247 {
248 for( k = 0; k < height; k++, a += a_step, b += b_step )
249 for( l = 0; l < width_n; l += 3 )
250 {
251 ccorr.val[0] += a[l]*b[l];
252 ccorr.val[1] += a[l+1]*b[l+1];
253 ccorr.val[2] += a[l+2]*b[l+2];
254 a_sum.val[0] += a[l];
255 a_sum.val[1] += a[l+1];
256 a_sum.val[2] += a[l+2];
257 a_sum2.val[0] += a[l]*a[l];
258 a_sum2.val[1] += a[l+1]*a[l+1];
259 a_sum2.val[2] += a[l+2]*a[l+2];
260 }
261 }
262 }
263
264 switch( method )
265 {
266 case CV_TM_CCORR:
267 case CV_TM_CCORR_NORMED:
268 value = ccorr.val[0];
269 break;
270 case CV_TM_SQDIFF:
271 case CV_TM_SQDIFF_NORMED:
272 value = (a_sum2.val[0] + b_sum2 - 2*ccorr.val[0]);
273 break;
274 default:
275 value = (ccorr.val[0] - a_sum.val[0]*b_mean.val[0]+
276 ccorr.val[1] - a_sum.val[1]*b_mean.val[1]+
277 ccorr.val[2] - a_sum.val[2]*b_mean.val[2]);
278 }
279
280 if( method & 1 )
281 {
282 double denom;
283
284 // calc denominator
285 if( method != CV_TM_CCOEFF_NORMED )
286 {
287 denom = a_sum2.val[0] + a_sum2.val[1] + a_sum2.val[2];
288 }
289 else
290 {
291 denom = a_sum2.val[0] - (a_sum.val[0]*a_sum.val[0])/area;
292 denom += a_sum2.val[1] - (a_sum.val[1]*a_sum.val[1])/area;
293 denom += a_sum2.val[2] - (a_sum.val[2]*a_sum.val[2])/area;
294 }
295 denom = sqrt(MAX(denom,0))*b_denom;
296 if( fabs(value) < denom )
297 value /= denom;
298 else if( fabs(value) < denom*1.125 )
299 value = value > 0 ? 1 : -1;
300 else
301 value = method != CV_TM_SQDIFF_NORMED ? 0 : 1;
302 }
303
304 ((float*)(result->data.ptr + result->step*i))[j] = (float)value;
305 }
306 }
307 }
308
309
prepare_to_validation(int)310 void CV_TemplMatchTest::prepare_to_validation( int /*test_case_idx*/ )
311 {
312 CvMat _input = test_mat[INPUT][0], _templ = test_mat[INPUT][1];
313 CvMat _output = test_mat[REF_OUTPUT][0];
314 cvTsMatchTemplate( &_input, &_templ, &_output, method );
315
316 //if( ts->get_current_test_info()->test_case_idx == 0 )
317 /*{
318 CvFileStorage* fs = cvOpenFileStorage( "_match_template.yml", 0, CV_STORAGE_WRITE );
319 cvWrite( fs, "image", &test_mat[INPUT][0] );
320 cvWrite( fs, "template", &test_mat[INPUT][1] );
321 cvWrite( fs, "ref", &test_mat[REF_OUTPUT][0] );
322 cvWrite( fs, "opencv", &test_mat[OUTPUT][0] );
323 cvWriteInt( fs, "method", method );
324 cvReleaseFileStorage( &fs );
325 }*/
326
327 if( method >= CV_TM_CCOEFF )
328 {
329 // avoid numerical stability problems in singular cases (when the results are near to 0)
330 const double delta = 10.;
331 test_mat[REF_OUTPUT][0] += Scalar::all(delta);
332 test_mat[OUTPUT][0] += Scalar::all(delta);
333 }
334 }
335
TEST(Imgproc_MatchTemplate,accuracy)336 TEST(Imgproc_MatchTemplate, accuracy) { CV_TemplMatchTest test; test.safe_run(); }
337