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