Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/video/test/test_camshift.cpp
16354 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
#include "opencv2/video/tracking_c.h"
44
45
namespace opencv_test { namespace {
46
47
class CV_TrackBaseTest : public cvtest::BaseTest
48
{
49
public:
50
CV_TrackBaseTest();
51
virtual ~CV_TrackBaseTest();
52
void clear();
53
54
protected:
55
int read_params( CvFileStorage* fs );
56
void run_func(void);
57
int prepare_test_case( int test_case_idx );
58
int validate_test_results( int test_case_idx );
59
void generate_object();
60
61
int min_log_size, max_log_size;
62
CvMat* img;
63
CvBox2D box0;
64
CvSize img_size;
65
CvTermCriteria criteria;
66
int img_type;
67
};
68
69
70
CV_TrackBaseTest::CV_TrackBaseTest()
71
{
72
img = 0;
73
test_case_count = 100;
74
min_log_size = 5;
75
max_log_size = 8;
76
}
77
78
79
CV_TrackBaseTest::~CV_TrackBaseTest()
80
{
81
clear();
82
}
83
84
85
void CV_TrackBaseTest::clear()
86
{
87
cvReleaseMat( &img );
88
cvtest::BaseTest::clear();
89
}
90
91
92
int CV_TrackBaseTest::read_params( CvFileStorage* fs )
93
{
94
int code = cvtest::BaseTest::read_params( fs );
95
if( code < 0 )
96
return code;
97
98
test_case_count = cvReadInt( find_param( fs, "test_case_count" ), test_case_count );
99
min_log_size = cvReadInt( find_param( fs, "min_log_size" ), min_log_size );
100
max_log_size = cvReadInt( find_param( fs, "max_log_size" ), max_log_size );
101
102
min_log_size = cvtest::clipInt( min_log_size, 1, 10 );
103
max_log_size = cvtest::clipInt( max_log_size, 1, 10 );
104
if( min_log_size > max_log_size )
105
{
106
int t;
107
CV_SWAP( min_log_size, max_log_size, t );
108
}
109
110
return 0;
111
}
112
113
114
void CV_TrackBaseTest::generate_object()
115
{
116
int x, y;
117
double cx = box0.center.x;
118
double cy = box0.center.y;
119
double width = box0.size.width*0.5;
120
double height = box0.size.height*0.5;
121
double angle = box0.angle*CV_PI/180.;
122
double a = sin(angle), b = -cos(angle);
123
double inv_ww = 1./(width*width), inv_hh = 1./(height*height);
124
125
img = cvCreateMat( img_size.height, img_size.width, img_type );
126
cvZero( img );
127
128
// use the straightforward algorithm: for every pixel check if it is inside the ellipse
129
for( y = 0; y < img_size.height; y++ )
130
{
131
uchar* ptr = img->data.ptr + img->step*y;
132
float* fl = (float*)ptr;
133
double x_ = (y - cy)*b, y_ = (y - cy)*a;
134
135
for( x = 0; x < img_size.width; x++ )
136
{
137
double x1 = (x - cx)*a - x_;
138
double y1 = (x - cx)*b + y_;
139
140
if( x1*x1*inv_hh + y1*y1*inv_ww <= 1. )
141
{
142
if( img_type == CV_8U )
143
ptr[x] = (uchar)1;
144
else
145
fl[x] = (float)1.f;
146
}
147
}
148
}
149
}
150
151
152
int CV_TrackBaseTest::prepare_test_case( int test_case_idx )
153
{
154
RNG& rng = ts->get_rng();
155
cvtest::BaseTest::prepare_test_case( test_case_idx );
156
float m;
157
158
clear();
159
160
box0.size.width = (float)exp((cvtest::randReal(rng) * (max_log_size - min_log_size) + min_log_size)*CV_LOG2);
161
box0.size.height = (float)exp((cvtest::randReal(rng) * (max_log_size - min_log_size) + min_log_size)*CV_LOG2);
162
box0.angle = (float)(cvtest::randReal(rng)*180.);
163
164
if( box0.size.width > box0.size.height )
165
{
166
float t;
167
CV_SWAP( box0.size.width, box0.size.height, t );
168
}
169
170
m = MAX( box0.size.width, box0.size.height );
171
img_size.width = cvRound(cvtest::randReal(rng)*m*0.5 + m + 1);
172
img_size.height = cvRound(cvtest::randReal(rng)*m*0.5 + m + 1);
173
img_type = cvtest::randInt(rng) % 2 ? CV_32F : CV_8U;
174
img_type = CV_8U;
175
176
box0.center.x = (float)(img_size.width*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.width - m));
177
box0.center.y = (float)(img_size.height*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.height - m));
178
179
criteria = cvTermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 0.1 );
180
181
generate_object();
182
183
return 1;
184
}
185
186
187
void CV_TrackBaseTest::run_func(void)
188
{
189
}
190
191
192
int CV_TrackBaseTest::validate_test_results( int /*test_case_idx*/ )
193
{
194
return 0;
195
}
196
197
198
199
///////////////////////// CamShift //////////////////////////////
200
201
class CV_CamShiftTest : public CV_TrackBaseTest
202
{
203
public:
204
CV_CamShiftTest();
205
206
protected:
207
void run_func(void);
208
int prepare_test_case( int test_case_idx );
209
int validate_test_results( int test_case_idx );
210
void generate_object();
211
212
CvBox2D box;
213
CvRect init_rect;
214
CvConnectedComp comp;
215
int area0;
216
};
217
218
219
CV_CamShiftTest::CV_CamShiftTest()
220
{
221
}
222
223
224
int CV_CamShiftTest::prepare_test_case( int test_case_idx )
225
{
226
RNG& rng = ts->get_rng();
227
double m;
228
int code = CV_TrackBaseTest::prepare_test_case( test_case_idx );
229
int i, area;
230
231
if( code <= 0 )
232
return code;
233
234
area0 = cvCountNonZero(img);
235
236
for(i = 0; i < 100; i++)
237
{
238
CvMat temp;
239
240
m = MAX(box0.size.width,box0.size.height)*0.8;
241
init_rect.x = cvFloor(box0.center.x - m*(0.45 + cvtest::randReal(rng)*0.2));
242
init_rect.y = cvFloor(box0.center.y - m*(0.45 + cvtest::randReal(rng)*0.2));
243
init_rect.width = cvCeil(box0.center.x + m*(0.45 + cvtest::randReal(rng)*0.2) - init_rect.x);
244
init_rect.height = cvCeil(box0.center.y + m*(0.45 + cvtest::randReal(rng)*0.2) - init_rect.y);
245
246
if( init_rect.x < 0 || init_rect.y < 0 ||
247
init_rect.x + init_rect.width >= img_size.width ||
248
init_rect.y + init_rect.height >= img_size.height )
249
continue;
250
251
cvGetSubRect( img, &temp, init_rect );
252
area = cvCountNonZero( &temp );
253
254
if( area >= 0.1*area0 )
255
break;
256
}
257
258
return i < 100 ? code : 0;
259
}
260
261
262
void CV_CamShiftTest::run_func(void)
263
{
264
cvCamShift( img, init_rect, criteria, &comp, &box );
265
}
266
267
268
int CV_CamShiftTest::validate_test_results( int /*test_case_idx*/ )
269
{
270
int code = cvtest::TS::OK;
271
272
double m = MAX(box0.size.width, box0.size.height), delta;
273
double diff_angle;
274
275
if( cvIsNaN(box.size.width) || cvIsInf(box.size.width) || box.size.width <= 0 ||
276
cvIsNaN(box.size.height) || cvIsInf(box.size.height) || box.size.height <= 0 ||
277
cvIsNaN(box.center.x) || cvIsInf(box.center.x) ||
278
cvIsNaN(box.center.y) || cvIsInf(box.center.y) ||
279
cvIsNaN(box.angle) || cvIsInf(box.angle) || box.angle < -180 || box.angle > 180 ||
280
cvIsNaN(comp.area) || cvIsInf(comp.area) || comp.area <= 0 )
281
{
282
ts->printf( cvtest::TS::LOG, "Invalid CvBox2D or CvConnectedComp was returned by cvCamShift\n" );
283
code = cvtest::TS::FAIL_INVALID_OUTPUT;
284
goto _exit_;
285
}
286
287
box.angle = (float)(180 - box.angle);
288
289
if( fabs(box.size.width - box0.size.width) > box0.size.width*0.2 ||
290
fabs(box.size.height - box0.size.height) > box0.size.height*0.3 )
291
{
292
ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D size (=%.1f x %.1f, should be %.1f x %.1f)\n",
293
box.size.width, box.size.height, box0.size.width, box0.size.height );
294
code = cvtest::TS::FAIL_BAD_ACCURACY;
295
goto _exit_;
296
}
297
298
if( fabs(box.center.x - box0.center.x) > m*0.1 ||
299
fabs(box.center.y - box0.center.y) > m*0.1 )
300
{
301
ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D position (=(%.1f, %.1f), should be (%.1f, %.1f))\n",
302
box.center.x, box.center.y, box0.center.x, box0.center.y );
303
code = cvtest::TS::FAIL_BAD_ACCURACY;
304
goto _exit_;
305
}
306
307
if( box.angle < 0 )
308
box.angle += 180;
309
310
diff_angle = fabs(box0.angle - box.angle);
311
diff_angle = MIN( diff_angle, fabs(box0.angle - box.angle + 180));
312
313
if( fabs(diff_angle) > 30 && box0.size.height > box0.size.width*1.2 )
314
{
315
ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D angle (=%1.f, should be %1.f)\n",
316
box.angle, box0.angle );
317
code = cvtest::TS::FAIL_BAD_ACCURACY;
318
goto _exit_;
319
}
320
321
delta = m*0.7;
322
323
if( comp.rect.x < box0.center.x - delta ||
324
comp.rect.y < box0.center.y - delta ||
325
comp.rect.x + comp.rect.width > box0.center.x + delta ||
326
comp.rect.y + comp.rect.height > box0.center.y + delta )
327
{
328
ts->printf( cvtest::TS::LOG,
329
"Incorrect CvConnectedComp ((%d,%d,%d,%d) is not within (%.1f,%.1f,%.1f,%.1f))\n",
330
comp.rect.x, comp.rect.y, comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height,
331
box0.center.x - delta, box0.center.y - delta, box0.center.x + delta, box0.center.y + delta );
332
code = cvtest::TS::FAIL_BAD_ACCURACY;
333
goto _exit_;
334
}
335
336
if( fabs(comp.area - area0) > area0*0.15 )
337
{
338
ts->printf( cvtest::TS::LOG,
339
"Incorrect CvConnectedComp area (=%.1f, should be %d)\n", comp.area, area0 );
340
code = cvtest::TS::FAIL_BAD_ACCURACY;
341
goto _exit_;
342
}
343
344
_exit_:
345
346
if( code < 0 )
347
{
348
#if 0 //defined _DEBUG && defined _WIN32
349
IplImage* dst = cvCreateImage( img_size, 8, 3 );
350
cvNamedWindow( "test", 1 );
351
cvCmpS( img, 0, img, CV_CMP_GT );
352
cvCvtColor( img, dst, CV_GRAY2BGR );
353
cvRectangle( dst, cvPoint(init_rect.x, init_rect.y),
354
cvPoint(init_rect.x + init_rect.width, init_rect.y + init_rect.height),
355
CV_RGB(255,0,0), 3, 8, 0 );
356
cvEllipseBox( dst, box, CV_RGB(0,255,0), 3, 8, 0 );
357
cvShowImage( "test", dst );
358
cvReleaseImage( &dst );
359
cvWaitKey();
360
#endif
361
ts->set_failed_test_info( code );
362
}
363
return code;
364
}
365
366
367
///////////////////////// MeanShift //////////////////////////////
368
369
class CV_MeanShiftTest : public CV_TrackBaseTest
370
{
371
public:
372
CV_MeanShiftTest();
373
374
protected:
375
void run_func(void);
376
int prepare_test_case( int test_case_idx );
377
int validate_test_results( int test_case_idx );
378
void generate_object();
379
380
CvRect init_rect;
381
CvConnectedComp comp;
382
int area0, area;
383
};
384
385
386
CV_MeanShiftTest::CV_MeanShiftTest()
387
{
388
}
389
390
391
int CV_MeanShiftTest::prepare_test_case( int test_case_idx )
392
{
393
RNG& rng = ts->get_rng();
394
double m;
395
int code = CV_TrackBaseTest::prepare_test_case( test_case_idx );
396
int i;
397
398
if( code <= 0 )
399
return code;
400
401
area0 = cvCountNonZero(img);
402
403
for(i = 0; i < 100; i++)
404
{
405
CvMat temp;
406
407
m = (box0.size.width + box0.size.height)*0.5;
408
init_rect.x = cvFloor(box0.center.x - m*(0.4 + cvtest::randReal(rng)*0.2));
409
init_rect.y = cvFloor(box0.center.y - m*(0.4 + cvtest::randReal(rng)*0.2));
410
init_rect.width = cvCeil(box0.center.x + m*(0.4 + cvtest::randReal(rng)*0.2) - init_rect.x);
411
init_rect.height = cvCeil(box0.center.y + m*(0.4 + cvtest::randReal(rng)*0.2) - init_rect.y);
412
413
if( init_rect.x < 0 || init_rect.y < 0 ||
414
init_rect.x + init_rect.width >= img_size.width ||
415
init_rect.y + init_rect.height >= img_size.height )
416
continue;
417
418
cvGetSubRect( img, &temp, init_rect );
419
area = cvCountNonZero( &temp );
420
421
if( area >= 0.5*area0 )
422
break;
423
}
424
425
return i < 100 ? code : 0;
426
}
427
428
429
void CV_MeanShiftTest::run_func(void)
430
{
431
cvMeanShift( img, init_rect, criteria, &comp );
432
}
433
434
435
int CV_MeanShiftTest::validate_test_results( int /*test_case_idx*/ )
436
{
437
int code = cvtest::TS::OK;
438
Point2f c;
439
double m = MAX(box0.size.width, box0.size.height), delta;
440
441
if( cvIsNaN(comp.area) || cvIsInf(comp.area) || comp.area <= 0 )
442
{
443
ts->printf( cvtest::TS::LOG, "Invalid CvConnectedComp was returned by cvMeanShift\n" );
444
code = cvtest::TS::FAIL_INVALID_OUTPUT;
445
goto _exit_;
446
}
447
448
c.x = (float)(comp.rect.x + comp.rect.width*0.5);
449
c.y = (float)(comp.rect.y + comp.rect.height*0.5);
450
451
if( fabs(c.x - box0.center.x) > m*0.1 ||
452
fabs(c.y - box0.center.y) > m*0.1 )
453
{
454
ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D position (=(%.1f, %.1f), should be (%.1f, %.1f))\n",
455
c.x, c.y, box0.center.x, box0.center.y );
456
code = cvtest::TS::FAIL_BAD_ACCURACY;
457
goto _exit_;
458
}
459
460
delta = m*0.7;
461
462
if( comp.rect.x < box0.center.x - delta ||
463
comp.rect.y < box0.center.y - delta ||
464
comp.rect.x + comp.rect.width > box0.center.x + delta ||
465
comp.rect.y + comp.rect.height > box0.center.y + delta )
466
{
467
ts->printf( cvtest::TS::LOG,
468
"Incorrect CvConnectedComp ((%d,%d,%d,%d) is not within (%.1f,%.1f,%.1f,%.1f))\n",
469
comp.rect.x, comp.rect.y, comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height,
470
box0.center.x - delta, box0.center.y - delta, box0.center.x + delta, box0.center.y + delta );
471
code = cvtest::TS::FAIL_BAD_ACCURACY;
472
goto _exit_;
473
}
474
475
if( fabs((double)(comp.area - area0)) > fabs((double)(area - area0)) + area0*0.05 )
476
{
477
ts->printf( cvtest::TS::LOG,
478
"Incorrect CvConnectedComp area (=%.1f, should be %d)\n", comp.area, area0 );
479
code = cvtest::TS::FAIL_BAD_ACCURACY;
480
goto _exit_;
481
}
482
483
_exit_:
484
485
if( code < 0 )
486
{
487
#if 0// defined _DEBUG && defined _WIN32
488
IplImage* dst = cvCreateImage( img_size, 8, 3 );
489
cvNamedWindow( "test", 1 );
490
cvCmpS( img, 0, img, CV_CMP_GT );
491
cvCvtColor( img, dst, CV_GRAY2BGR );
492
cvRectangle( dst, cvPoint(init_rect.x, init_rect.y),
493
cvPoint(init_rect.x + init_rect.width, init_rect.y + init_rect.height),
494
CV_RGB(255,0,0), 3, 8, 0 );
495
cvRectangle( dst, cvPoint(comp.rect.x, comp.rect.y),
496
cvPoint(comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height),
497
CV_RGB(0,255,0), 3, 8, 0 );
498
cvShowImage( "test", dst );
499
cvReleaseImage( &dst );
500
cvWaitKey();
501
#endif
502
ts->set_failed_test_info( code );
503
}
504
return code;
505
}
506
507
508
TEST(Video_CAMShift, accuracy) { CV_CamShiftTest test; test.safe_run(); }
509
TEST(Video_MeanShift, accuracy) { CV_MeanShiftTest test; test.safe_run(); }
510
511
}} // namespace
512
/* End of file. */
513
514