Path: blob/master/modules/video/test/test_camshift.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of Intel Corporation may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041#include "test_precomp.hpp"42#include "opencv2/video/tracking_c.h"4344namespace opencv_test { namespace {4546class CV_TrackBaseTest : public cvtest::BaseTest47{48public:49CV_TrackBaseTest();50virtual ~CV_TrackBaseTest();51void clear();5253protected:54int read_params( CvFileStorage* fs );55void run_func(void);56int prepare_test_case( int test_case_idx );57int validate_test_results( int test_case_idx );58void generate_object();5960int min_log_size, max_log_size;61CvMat* img;62CvBox2D box0;63CvSize img_size;64CvTermCriteria criteria;65int img_type;66};676869CV_TrackBaseTest::CV_TrackBaseTest()70{71img = 0;72test_case_count = 100;73min_log_size = 5;74max_log_size = 8;75}767778CV_TrackBaseTest::~CV_TrackBaseTest()79{80clear();81}828384void CV_TrackBaseTest::clear()85{86cvReleaseMat( &img );87cvtest::BaseTest::clear();88}899091int CV_TrackBaseTest::read_params( CvFileStorage* fs )92{93int code = cvtest::BaseTest::read_params( fs );94if( code < 0 )95return code;9697test_case_count = cvReadInt( find_param( fs, "test_case_count" ), test_case_count );98min_log_size = cvReadInt( find_param( fs, "min_log_size" ), min_log_size );99max_log_size = cvReadInt( find_param( fs, "max_log_size" ), max_log_size );100101min_log_size = cvtest::clipInt( min_log_size, 1, 10 );102max_log_size = cvtest::clipInt( max_log_size, 1, 10 );103if( min_log_size > max_log_size )104{105int t;106CV_SWAP( min_log_size, max_log_size, t );107}108109return 0;110}111112113void CV_TrackBaseTest::generate_object()114{115int x, y;116double cx = box0.center.x;117double cy = box0.center.y;118double width = box0.size.width*0.5;119double height = box0.size.height*0.5;120double angle = box0.angle*CV_PI/180.;121double a = sin(angle), b = -cos(angle);122double inv_ww = 1./(width*width), inv_hh = 1./(height*height);123124img = cvCreateMat( img_size.height, img_size.width, img_type );125cvZero( img );126127// use the straightforward algorithm: for every pixel check if it is inside the ellipse128for( y = 0; y < img_size.height; y++ )129{130uchar* ptr = img->data.ptr + img->step*y;131float* fl = (float*)ptr;132double x_ = (y - cy)*b, y_ = (y - cy)*a;133134for( x = 0; x < img_size.width; x++ )135{136double x1 = (x - cx)*a - x_;137double y1 = (x - cx)*b + y_;138139if( x1*x1*inv_hh + y1*y1*inv_ww <= 1. )140{141if( img_type == CV_8U )142ptr[x] = (uchar)1;143else144fl[x] = (float)1.f;145}146}147}148}149150151int CV_TrackBaseTest::prepare_test_case( int test_case_idx )152{153RNG& rng = ts->get_rng();154cvtest::BaseTest::prepare_test_case( test_case_idx );155float m;156157clear();158159box0.size.width = (float)exp((cvtest::randReal(rng) * (max_log_size - min_log_size) + min_log_size)*CV_LOG2);160box0.size.height = (float)exp((cvtest::randReal(rng) * (max_log_size - min_log_size) + min_log_size)*CV_LOG2);161box0.angle = (float)(cvtest::randReal(rng)*180.);162163if( box0.size.width > box0.size.height )164{165float t;166CV_SWAP( box0.size.width, box0.size.height, t );167}168169m = MAX( box0.size.width, box0.size.height );170img_size.width = cvRound(cvtest::randReal(rng)*m*0.5 + m + 1);171img_size.height = cvRound(cvtest::randReal(rng)*m*0.5 + m + 1);172img_type = cvtest::randInt(rng) % 2 ? CV_32F : CV_8U;173img_type = CV_8U;174175box0.center.x = (float)(img_size.width*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.width - m));176box0.center.y = (float)(img_size.height*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.height - m));177178criteria = cvTermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 0.1 );179180generate_object();181182return 1;183}184185186void CV_TrackBaseTest::run_func(void)187{188}189190191int CV_TrackBaseTest::validate_test_results( int /*test_case_idx*/ )192{193return 0;194}195196197198///////////////////////// CamShift //////////////////////////////199200class CV_CamShiftTest : public CV_TrackBaseTest201{202public:203CV_CamShiftTest();204205protected:206void run_func(void);207int prepare_test_case( int test_case_idx );208int validate_test_results( int test_case_idx );209void generate_object();210211CvBox2D box;212CvRect init_rect;213CvConnectedComp comp;214int area0;215};216217218CV_CamShiftTest::CV_CamShiftTest()219{220}221222223int CV_CamShiftTest::prepare_test_case( int test_case_idx )224{225RNG& rng = ts->get_rng();226double m;227int code = CV_TrackBaseTest::prepare_test_case( test_case_idx );228int i, area;229230if( code <= 0 )231return code;232233area0 = cvCountNonZero(img);234235for(i = 0; i < 100; i++)236{237CvMat temp;238239m = MAX(box0.size.width,box0.size.height)*0.8;240init_rect.x = cvFloor(box0.center.x - m*(0.45 + cvtest::randReal(rng)*0.2));241init_rect.y = cvFloor(box0.center.y - m*(0.45 + cvtest::randReal(rng)*0.2));242init_rect.width = cvCeil(box0.center.x + m*(0.45 + cvtest::randReal(rng)*0.2) - init_rect.x);243init_rect.height = cvCeil(box0.center.y + m*(0.45 + cvtest::randReal(rng)*0.2) - init_rect.y);244245if( init_rect.x < 0 || init_rect.y < 0 ||246init_rect.x + init_rect.width >= img_size.width ||247init_rect.y + init_rect.height >= img_size.height )248continue;249250cvGetSubRect( img, &temp, init_rect );251area = cvCountNonZero( &temp );252253if( area >= 0.1*area0 )254break;255}256257return i < 100 ? code : 0;258}259260261void CV_CamShiftTest::run_func(void)262{263cvCamShift( img, init_rect, criteria, &comp, &box );264}265266267int CV_CamShiftTest::validate_test_results( int /*test_case_idx*/ )268{269int code = cvtest::TS::OK;270271double m = MAX(box0.size.width, box0.size.height), delta;272double diff_angle;273274if( cvIsNaN(box.size.width) || cvIsInf(box.size.width) || box.size.width <= 0 ||275cvIsNaN(box.size.height) || cvIsInf(box.size.height) || box.size.height <= 0 ||276cvIsNaN(box.center.x) || cvIsInf(box.center.x) ||277cvIsNaN(box.center.y) || cvIsInf(box.center.y) ||278cvIsNaN(box.angle) || cvIsInf(box.angle) || box.angle < -180 || box.angle > 180 ||279cvIsNaN(comp.area) || cvIsInf(comp.area) || comp.area <= 0 )280{281ts->printf( cvtest::TS::LOG, "Invalid CvBox2D or CvConnectedComp was returned by cvCamShift\n" );282code = cvtest::TS::FAIL_INVALID_OUTPUT;283goto _exit_;284}285286box.angle = (float)(180 - box.angle);287288if( fabs(box.size.width - box0.size.width) > box0.size.width*0.2 ||289fabs(box.size.height - box0.size.height) > box0.size.height*0.3 )290{291ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D size (=%.1f x %.1f, should be %.1f x %.1f)\n",292box.size.width, box.size.height, box0.size.width, box0.size.height );293code = cvtest::TS::FAIL_BAD_ACCURACY;294goto _exit_;295}296297if( fabs(box.center.x - box0.center.x) > m*0.1 ||298fabs(box.center.y - box0.center.y) > m*0.1 )299{300ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D position (=(%.1f, %.1f), should be (%.1f, %.1f))\n",301box.center.x, box.center.y, box0.center.x, box0.center.y );302code = cvtest::TS::FAIL_BAD_ACCURACY;303goto _exit_;304}305306if( box.angle < 0 )307box.angle += 180;308309diff_angle = fabs(box0.angle - box.angle);310diff_angle = MIN( diff_angle, fabs(box0.angle - box.angle + 180));311312if( fabs(diff_angle) > 30 && box0.size.height > box0.size.width*1.2 )313{314ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D angle (=%1.f, should be %1.f)\n",315box.angle, box0.angle );316code = cvtest::TS::FAIL_BAD_ACCURACY;317goto _exit_;318}319320delta = m*0.7;321322if( comp.rect.x < box0.center.x - delta ||323comp.rect.y < box0.center.y - delta ||324comp.rect.x + comp.rect.width > box0.center.x + delta ||325comp.rect.y + comp.rect.height > box0.center.y + delta )326{327ts->printf( cvtest::TS::LOG,328"Incorrect CvConnectedComp ((%d,%d,%d,%d) is not within (%.1f,%.1f,%.1f,%.1f))\n",329comp.rect.x, comp.rect.y, comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height,330box0.center.x - delta, box0.center.y - delta, box0.center.x + delta, box0.center.y + delta );331code = cvtest::TS::FAIL_BAD_ACCURACY;332goto _exit_;333}334335if( fabs(comp.area - area0) > area0*0.15 )336{337ts->printf( cvtest::TS::LOG,338"Incorrect CvConnectedComp area (=%.1f, should be %d)\n", comp.area, area0 );339code = cvtest::TS::FAIL_BAD_ACCURACY;340goto _exit_;341}342343_exit_:344345if( code < 0 )346{347#if 0 //defined _DEBUG && defined _WIN32348IplImage* dst = cvCreateImage( img_size, 8, 3 );349cvNamedWindow( "test", 1 );350cvCmpS( img, 0, img, CV_CMP_GT );351cvCvtColor( img, dst, CV_GRAY2BGR );352cvRectangle( dst, cvPoint(init_rect.x, init_rect.y),353cvPoint(init_rect.x + init_rect.width, init_rect.y + init_rect.height),354CV_RGB(255,0,0), 3, 8, 0 );355cvEllipseBox( dst, box, CV_RGB(0,255,0), 3, 8, 0 );356cvShowImage( "test", dst );357cvReleaseImage( &dst );358cvWaitKey();359#endif360ts->set_failed_test_info( code );361}362return code;363}364365366///////////////////////// MeanShift //////////////////////////////367368class CV_MeanShiftTest : public CV_TrackBaseTest369{370public:371CV_MeanShiftTest();372373protected:374void run_func(void);375int prepare_test_case( int test_case_idx );376int validate_test_results( int test_case_idx );377void generate_object();378379CvRect init_rect;380CvConnectedComp comp;381int area0, area;382};383384385CV_MeanShiftTest::CV_MeanShiftTest()386{387}388389390int CV_MeanShiftTest::prepare_test_case( int test_case_idx )391{392RNG& rng = ts->get_rng();393double m;394int code = CV_TrackBaseTest::prepare_test_case( test_case_idx );395int i;396397if( code <= 0 )398return code;399400area0 = cvCountNonZero(img);401402for(i = 0; i < 100; i++)403{404CvMat temp;405406m = (box0.size.width + box0.size.height)*0.5;407init_rect.x = cvFloor(box0.center.x - m*(0.4 + cvtest::randReal(rng)*0.2));408init_rect.y = cvFloor(box0.center.y - m*(0.4 + cvtest::randReal(rng)*0.2));409init_rect.width = cvCeil(box0.center.x + m*(0.4 + cvtest::randReal(rng)*0.2) - init_rect.x);410init_rect.height = cvCeil(box0.center.y + m*(0.4 + cvtest::randReal(rng)*0.2) - init_rect.y);411412if( init_rect.x < 0 || init_rect.y < 0 ||413init_rect.x + init_rect.width >= img_size.width ||414init_rect.y + init_rect.height >= img_size.height )415continue;416417cvGetSubRect( img, &temp, init_rect );418area = cvCountNonZero( &temp );419420if( area >= 0.5*area0 )421break;422}423424return i < 100 ? code : 0;425}426427428void CV_MeanShiftTest::run_func(void)429{430cvMeanShift( img, init_rect, criteria, &comp );431}432433434int CV_MeanShiftTest::validate_test_results( int /*test_case_idx*/ )435{436int code = cvtest::TS::OK;437Point2f c;438double m = MAX(box0.size.width, box0.size.height), delta;439440if( cvIsNaN(comp.area) || cvIsInf(comp.area) || comp.area <= 0 )441{442ts->printf( cvtest::TS::LOG, "Invalid CvConnectedComp was returned by cvMeanShift\n" );443code = cvtest::TS::FAIL_INVALID_OUTPUT;444goto _exit_;445}446447c.x = (float)(comp.rect.x + comp.rect.width*0.5);448c.y = (float)(comp.rect.y + comp.rect.height*0.5);449450if( fabs(c.x - box0.center.x) > m*0.1 ||451fabs(c.y - box0.center.y) > m*0.1 )452{453ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D position (=(%.1f, %.1f), should be (%.1f, %.1f))\n",454c.x, c.y, box0.center.x, box0.center.y );455code = cvtest::TS::FAIL_BAD_ACCURACY;456goto _exit_;457}458459delta = m*0.7;460461if( comp.rect.x < box0.center.x - delta ||462comp.rect.y < box0.center.y - delta ||463comp.rect.x + comp.rect.width > box0.center.x + delta ||464comp.rect.y + comp.rect.height > box0.center.y + delta )465{466ts->printf( cvtest::TS::LOG,467"Incorrect CvConnectedComp ((%d,%d,%d,%d) is not within (%.1f,%.1f,%.1f,%.1f))\n",468comp.rect.x, comp.rect.y, comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height,469box0.center.x - delta, box0.center.y - delta, box0.center.x + delta, box0.center.y + delta );470code = cvtest::TS::FAIL_BAD_ACCURACY;471goto _exit_;472}473474if( fabs((double)(comp.area - area0)) > fabs((double)(area - area0)) + area0*0.05 )475{476ts->printf( cvtest::TS::LOG,477"Incorrect CvConnectedComp area (=%.1f, should be %d)\n", comp.area, area0 );478code = cvtest::TS::FAIL_BAD_ACCURACY;479goto _exit_;480}481482_exit_:483484if( code < 0 )485{486#if 0// defined _DEBUG && defined _WIN32487IplImage* dst = cvCreateImage( img_size, 8, 3 );488cvNamedWindow( "test", 1 );489cvCmpS( img, 0, img, CV_CMP_GT );490cvCvtColor( img, dst, CV_GRAY2BGR );491cvRectangle( dst, cvPoint(init_rect.x, init_rect.y),492cvPoint(init_rect.x + init_rect.width, init_rect.y + init_rect.height),493CV_RGB(255,0,0), 3, 8, 0 );494cvRectangle( dst, cvPoint(comp.rect.x, comp.rect.y),495cvPoint(comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height),496CV_RGB(0,255,0), 3, 8, 0 );497cvShowImage( "test", dst );498cvReleaseImage( &dst );499cvWaitKey();500#endif501ts->set_failed_test_info( code );502}503return code;504}505506507TEST(Video_CAMShift, accuracy) { CV_CamShiftTest test; test.safe_run(); }508TEST(Video_MeanShift, accuracy) { CV_MeanShiftTest test; test.safe_run(); }509510}} // namespace511/* End of file. */512513514