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"
44 /* ///////////////////// pyrlk_test ///////////////////////// */
46 class CV_OptFlowPyrLKTest : public cvtest::BaseTest
49 CV_OptFlowPyrLKTest();
55 CV_OptFlowPyrLKTest::CV_OptFlowPyrLKTest() {}
57 void CV_OptFlowPyrLKTest::run( int )
59 int code = cvtest::TS::OK;
61 const double success_error_level = 0.3;
62 const int bad_points_max = 8;
65 double max_err = 0., sum_err = 0;
68 int merr_i = 0, merr_j = 0, merr_k = 0;
71 CvPoint2D32f *u = 0, *v = 0, *v2 = 0;
72 CvMat *_u = 0, *_v = 0, *_v2 = 0;
80 sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" );
81 _u = (CvMat*)cvLoad( filename );
85 ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
86 code = cvtest::TS::FAIL_MISSING_TEST_DATA;
90 sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" );
91 _v = (CvMat*)cvLoad( filename );
95 ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
96 code = cvtest::TS::FAIL_MISSING_TEST_DATA;
100 if( _u->cols != 2 || CV_MAT_TYPE(_u->type) != CV_32F ||
101 _v->cols != 2 || CV_MAT_TYPE(_v->type) != CV_32F || _v->rows != _u->rows )
103 ts->printf( cvtest::TS::LOG, "the loaded matrices of points are not valid\n" );
104 code = cvtest::TS::FAIL_MISSING_TEST_DATA;
109 u = (CvPoint2D32f*)_u->data.fl;
110 v = (CvPoint2D32f*)_v->data.fl;
112 /* allocate adidtional buffers */
113 _v2 = cvCloneMat( _u );
114 v2 = (CvPoint2D32f*)_v2->data.fl;
116 /* read first image */
117 sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_1.bmp" );
118 imgI = cvLoadImage( filename, -1 );
122 ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
123 code = cvtest::TS::FAIL_MISSING_TEST_DATA;
127 /* read second image */
128 sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" );
129 imgJ = cvLoadImage( filename, -1 );
133 ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
134 code = cvtest::TS::FAIL_MISSING_TEST_DATA;
139 status = (char*)cvAlloc(n*sizeof(status[0]));
142 cvCalcOpticalFlowPyrLK( imgI, imgJ, 0, 0, u, v2, n, cvSize( 41, 41 ),
143 4, status, 0, cvTermCriteria( CV_TERMCRIT_ITER|
144 CV_TERMCRIT_EPS, 30, 0.01f ), 0 );
146 /* compare results */
147 for( i = 0; i < n; i++ )
152 if( cvIsNaN(v[i].x) )
158 err = fabs(v2[i].x - v[i].x) + fabs(v2[i].y - v[i].y);
165 pt_exceed += err > success_error_level;
171 if( !cvIsNaN( v[i].x ))
175 ts->printf( cvtest::TS::LOG, "The algorithm lost the point #%d\n", i );
176 code = cvtest::TS::FAIL_BAD_ACCURACY;
182 if( pt_exceed > bad_points_max )
184 ts->printf( cvtest::TS::LOG,
185 "The number of poorly tracked points is too big (>=%d)\n", pt_exceed );
186 code = cvtest::TS::FAIL_BAD_ACCURACY;
192 ts->printf( cvtest::TS::LOG, "Maximum tracking error is too big (=%g) at %d\n", max_err, merr_i );
193 code = cvtest::TS::FAIL_BAD_ACCURACY;
202 cvReleaseMat( &_v2 );
204 cvReleaseImage( &imgI );
205 cvReleaseImage( &imgJ );
208 ts->set_failed_test_info( code );
212 TEST(Video_OpticalFlowPyrLK, accuracy) { CV_OptFlowPyrLKTest test; test.safe_run(); }