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.
42
#include "test_precomp.hpp"
43
#include "opencv2/video/tracking_c.h"
48
class CV_TrackBaseTest : public cvtest::BaseTest
52
virtual ~CV_TrackBaseTest();
56
int read_params( CvFileStorage* fs );
58
int prepare_test_case( int test_case_idx );
59
int validate_test_results( int test_case_idx );
60
void generate_object();
62
int min_log_size, max_log_size;
66
CvTermCriteria criteria;
71
CV_TrackBaseTest::CV_TrackBaseTest()
74
test_case_count = 100;
80
CV_TrackBaseTest::~CV_TrackBaseTest()
86
void CV_TrackBaseTest::clear()
89
cvtest::BaseTest::clear();
93
int CV_TrackBaseTest::read_params( CvFileStorage* fs )
95
int code = cvtest::BaseTest::read_params( fs );
99
test_case_count = cvReadInt( find_param( fs, "test_case_count" ), test_case_count );
100
min_log_size = cvReadInt( find_param( fs, "min_log_size" ), min_log_size );
101
max_log_size = cvReadInt( find_param( fs, "max_log_size" ), max_log_size );
103
min_log_size = cvtest::clipInt( min_log_size, 1, 10 );
104
max_log_size = cvtest::clipInt( max_log_size, 1, 10 );
105
if( min_log_size > max_log_size )
108
CV_SWAP( min_log_size, max_log_size, t );
115
void CV_TrackBaseTest::generate_object()
118
double cx = box0.center.x;
119
double cy = box0.center.y;
120
double width = box0.size.width*0.5;
121
double height = box0.size.height*0.5;
122
double angle = box0.angle*CV_PI/180.;
123
double a = sin(angle), b = -cos(angle);
124
double inv_ww = 1./(width*width), inv_hh = 1./(height*height);
126
img = cvCreateMat( img_size.height, img_size.width, img_type );
129
// use the straightforward algorithm: for every pixel check if it is inside the ellipse
130
for( y = 0; y < img_size.height; y++ )
132
uchar* ptr = img->data.ptr + img->step*y;
133
float* fl = (float*)ptr;
134
double x_ = (y - cy)*b, y_ = (y - cy)*a;
136
for( x = 0; x < img_size.width; x++ )
138
double x1 = (x - cx)*a - x_;
139
double y1 = (x - cx)*b + y_;
141
if( x1*x1*inv_hh + y1*y1*inv_ww <= 1. )
143
if( img_type == CV_8U )
153
int CV_TrackBaseTest::prepare_test_case( int test_case_idx )
155
RNG& rng = ts->get_rng();
156
cvtest::BaseTest::prepare_test_case( test_case_idx );
161
box0.size.width = (float)exp((cvtest::randReal(rng) * (max_log_size - min_log_size) + min_log_size)*CV_LOG2);
162
box0.size.height = (float)exp((cvtest::randReal(rng) * (max_log_size - min_log_size) + min_log_size)*CV_LOG2);
163
box0.angle = (float)(cvtest::randReal(rng)*180.);
165
if( box0.size.width > box0.size.height )
168
CV_SWAP( box0.size.width, box0.size.height, t );
171
m = MAX( box0.size.width, box0.size.height );
172
img_size.width = cvRound(cvtest::randReal(rng)*m*0.5 + m + 1);
173
img_size.height = cvRound(cvtest::randReal(rng)*m*0.5 + m + 1);
174
img_type = cvtest::randInt(rng) % 2 ? CV_32F : CV_8U;
177
box0.center.x = (float)(img_size.width*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.width - m));
178
box0.center.y = (float)(img_size.height*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.height - m));
180
criteria = cvTermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 0.1 );
188
void CV_TrackBaseTest::run_func(void)
193
int CV_TrackBaseTest::validate_test_results( int /*test_case_idx*/ )
200
///////////////////////// CamShift //////////////////////////////
202
class CV_CamShiftTest : public CV_TrackBaseTest
209
int prepare_test_case( int test_case_idx );
210
int validate_test_results( int test_case_idx );
211
void generate_object();
215
CvConnectedComp comp;
220
CV_CamShiftTest::CV_CamShiftTest()
225
int CV_CamShiftTest::prepare_test_case( int test_case_idx )
227
RNG& rng = ts->get_rng();
229
int code = CV_TrackBaseTest::prepare_test_case( test_case_idx );
235
area0 = cvCountNonZero(img);
237
for(i = 0; i < 100; i++)
241
m = MAX(box0.size.width,box0.size.height)*0.8;
242
init_rect.x = cvFloor(box0.center.x - m*(0.45 + cvtest::randReal(rng)*0.2));
243
init_rect.y = cvFloor(box0.center.y - m*(0.45 + cvtest::randReal(rng)*0.2));
244
init_rect.width = cvCeil(box0.center.x + m*(0.45 + cvtest::randReal(rng)*0.2) - init_rect.x);
245
init_rect.height = cvCeil(box0.center.y + m*(0.45 + cvtest::randReal(rng)*0.2) - init_rect.y);
247
if( init_rect.x < 0 || init_rect.y < 0 ||
248
init_rect.x + init_rect.width >= img_size.width ||
249
init_rect.y + init_rect.height >= img_size.height )
252
cvGetSubRect( img, &temp, init_rect );
253
area = cvCountNonZero( &temp );
255
if( area >= 0.1*area0 )
259
return i < 100 ? code : 0;
263
void CV_CamShiftTest::run_func(void)
265
cvCamShift( img, init_rect, criteria, &comp, &box );
269
int CV_CamShiftTest::validate_test_results( int /*test_case_idx*/ )
271
int code = cvtest::TS::OK;
273
double m = MAX(box0.size.width, box0.size.height), delta;
276
if( cvIsNaN(box.size.width) || cvIsInf(box.size.width) || box.size.width <= 0 ||
277
cvIsNaN(box.size.height) || cvIsInf(box.size.height) || box.size.height <= 0 ||
278
cvIsNaN(box.center.x) || cvIsInf(box.center.x) ||
279
cvIsNaN(box.center.y) || cvIsInf(box.center.y) ||
280
cvIsNaN(box.angle) || cvIsInf(box.angle) || box.angle < -180 || box.angle > 180 ||
281
cvIsNaN(comp.area) || cvIsInf(comp.area) || comp.area <= 0 )
283
ts->printf( cvtest::TS::LOG, "Invalid CvBox2D or CvConnectedComp was returned by cvCamShift\n" );
284
code = cvtest::TS::FAIL_INVALID_OUTPUT;
288
box.angle = (float)(180 - box.angle);
290
if( fabs(box.size.width - box0.size.width) > box0.size.width*0.2 ||
291
fabs(box.size.height - box0.size.height) > box0.size.height*0.3 )
293
ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D size (=%.1f x %.1f, should be %.1f x %.1f)\n",
294
box.size.width, box.size.height, box0.size.width, box0.size.height );
295
code = cvtest::TS::FAIL_BAD_ACCURACY;
299
if( fabs(box.center.x - box0.center.x) > m*0.1 ||
300
fabs(box.center.y - box0.center.y) > m*0.1 )
302
ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D position (=(%.1f, %.1f), should be (%.1f, %.1f))\n",
303
box.center.x, box.center.y, box0.center.x, box0.center.y );
304
code = cvtest::TS::FAIL_BAD_ACCURACY;
311
diff_angle = fabs(box0.angle - box.angle);
312
diff_angle = MIN( diff_angle, fabs(box0.angle - box.angle + 180));
314
if( fabs(diff_angle) > 30 && box0.size.height > box0.size.width*1.2 )
316
ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D angle (=%1.f, should be %1.f)\n",
317
box.angle, box0.angle );
318
code = cvtest::TS::FAIL_BAD_ACCURACY;
324
if( comp.rect.x < box0.center.x - delta ||
325
comp.rect.y < box0.center.y - delta ||
326
comp.rect.x + comp.rect.width > box0.center.x + delta ||
327
comp.rect.y + comp.rect.height > box0.center.y + delta )
329
ts->printf( cvtest::TS::LOG,
330
"Incorrect CvConnectedComp ((%d,%d,%d,%d) is not within (%.1f,%.1f,%.1f,%.1f))\n",
331
comp.rect.x, comp.rect.y, comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height,
332
box0.center.x - delta, box0.center.y - delta, box0.center.x + delta, box0.center.y + delta );
333
code = cvtest::TS::FAIL_BAD_ACCURACY;
337
if( fabs(comp.area - area0) > area0*0.15 )
339
ts->printf( cvtest::TS::LOG,
340
"Incorrect CvConnectedComp area (=%.1f, should be %d)\n", comp.area, area0 );
341
code = cvtest::TS::FAIL_BAD_ACCURACY;
349
#if 0 //defined _DEBUG && defined WIN32
350
IplImage* dst = cvCreateImage( img_size, 8, 3 );
351
cvNamedWindow( "test", 1 );
352
cvCmpS( img, 0, img, CV_CMP_GT );
353
cvCvtColor( img, dst, CV_GRAY2BGR );
354
cvRectangle( dst, cvPoint(init_rect.x, init_rect.y),
355
cvPoint(init_rect.x + init_rect.width, init_rect.y + init_rect.height),
356
CV_RGB(255,0,0), 3, 8, 0 );
357
cvEllipseBox( dst, box, CV_RGB(0,255,0), 3, 8, 0 );
358
cvShowImage( "test", dst );
359
cvReleaseImage( &dst );
362
ts->set_failed_test_info( code );
368
///////////////////////// MeanShift //////////////////////////////
370
class CV_MeanShiftTest : public CV_TrackBaseTest
377
int prepare_test_case( int test_case_idx );
378
int validate_test_results( int test_case_idx );
379
void generate_object();
382
CvConnectedComp comp;
387
CV_MeanShiftTest::CV_MeanShiftTest()
392
int CV_MeanShiftTest::prepare_test_case( int test_case_idx )
394
RNG& rng = ts->get_rng();
396
int code = CV_TrackBaseTest::prepare_test_case( test_case_idx );
402
area0 = cvCountNonZero(img);
404
for(i = 0; i < 100; i++)
408
m = (box0.size.width + box0.size.height)*0.5;
409
init_rect.x = cvFloor(box0.center.x - m*(0.4 + cvtest::randReal(rng)*0.2));
410
init_rect.y = cvFloor(box0.center.y - m*(0.4 + cvtest::randReal(rng)*0.2));
411
init_rect.width = cvCeil(box0.center.x + m*(0.4 + cvtest::randReal(rng)*0.2) - init_rect.x);
412
init_rect.height = cvCeil(box0.center.y + m*(0.4 + cvtest::randReal(rng)*0.2) - init_rect.y);
414
if( init_rect.x < 0 || init_rect.y < 0 ||
415
init_rect.x + init_rect.width >= img_size.width ||
416
init_rect.y + init_rect.height >= img_size.height )
419
cvGetSubRect( img, &temp, init_rect );
420
area = cvCountNonZero( &temp );
422
if( area >= 0.5*area0 )
426
return i < 100 ? code : 0;
430
void CV_MeanShiftTest::run_func(void)
432
cvMeanShift( img, init_rect, criteria, &comp );
436
int CV_MeanShiftTest::validate_test_results( int /*test_case_idx*/ )
438
int code = cvtest::TS::OK;
440
double m = MAX(box0.size.width, box0.size.height), delta;
442
if( cvIsNaN(comp.area) || cvIsInf(comp.area) || comp.area <= 0 )
444
ts->printf( cvtest::TS::LOG, "Invalid CvConnectedComp was returned by cvMeanShift\n" );
445
code = cvtest::TS::FAIL_INVALID_OUTPUT;
449
c.x = (float)(comp.rect.x + comp.rect.width*0.5);
450
c.y = (float)(comp.rect.y + comp.rect.height*0.5);
452
if( fabs(c.x - box0.center.x) > m*0.1 ||
453
fabs(c.y - box0.center.y) > m*0.1 )
455
ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D position (=(%.1f, %.1f), should be (%.1f, %.1f))\n",
456
c.x, c.y, box0.center.x, box0.center.y );
457
code = cvtest::TS::FAIL_BAD_ACCURACY;
463
if( comp.rect.x < box0.center.x - delta ||
464
comp.rect.y < box0.center.y - delta ||
465
comp.rect.x + comp.rect.width > box0.center.x + delta ||
466
comp.rect.y + comp.rect.height > box0.center.y + delta )
468
ts->printf( cvtest::TS::LOG,
469
"Incorrect CvConnectedComp ((%d,%d,%d,%d) is not within (%.1f,%.1f,%.1f,%.1f))\n",
470
comp.rect.x, comp.rect.y, comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height,
471
box0.center.x - delta, box0.center.y - delta, box0.center.x + delta, box0.center.y + delta );
472
code = cvtest::TS::FAIL_BAD_ACCURACY;
476
if( fabs((double)(comp.area - area0)) > fabs((double)(area - area0)) + area0*0.05 )
478
ts->printf( cvtest::TS::LOG,
479
"Incorrect CvConnectedComp area (=%.1f, should be %d)\n", comp.area, area0 );
480
code = cvtest::TS::FAIL_BAD_ACCURACY;
488
#if 0// defined _DEBUG && defined WIN32
489
IplImage* dst = cvCreateImage( img_size, 8, 3 );
490
cvNamedWindow( "test", 1 );
491
cvCmpS( img, 0, img, CV_CMP_GT );
492
cvCvtColor( img, dst, CV_GRAY2BGR );
493
cvRectangle( dst, cvPoint(init_rect.x, init_rect.y),
494
cvPoint(init_rect.x + init_rect.width, init_rect.y + init_rect.height),
495
CV_RGB(255,0,0), 3, 8, 0 );
496
cvRectangle( dst, cvPoint(comp.rect.x, comp.rect.y),
497
cvPoint(comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height),
498
CV_RGB(0,255,0), 3, 8, 0 );
499
cvShowImage( "test", dst );
500
cvReleaseImage( &dst );
503
ts->set_failed_test_info( code );
509
TEST(Video_CAMShift, accuracy) { CV_CamShiftTest test; test.safe_run(); }
510
TEST(Video_MeanShift, accuracy) { CV_MeanShiftTest test; test.safe_run(); }