fixed many warnings from GCC 4.6.1
[profile/ivi/opencv.git] / modules / video / test / test_optflowpyrlk.cpp
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 /* ///////////////////// pyrlk_test ///////////////////////// */
45
46 class CV_OptFlowPyrLKTest : public cvtest::BaseTest
47 {
48 public:
49     CV_OptFlowPyrLKTest();
50 protected:
51     void run(int);
52 };
53
54
55 CV_OptFlowPyrLKTest::CV_OptFlowPyrLKTest() {}
56
57 void CV_OptFlowPyrLKTest::run( int )
58 {
59     int code = cvtest::TS::OK;
60
61     const double success_error_level = 0.3;
62     const int bad_points_max = 8;
63
64     /* test parameters */
65     double  max_err = 0., sum_err = 0;
66     int     pt_cmpd = 0;
67     int     pt_exceed = 0;
68     int     merr_i = 0, merr_j = 0, merr_k = 0;
69     char    filename[1000];
70
71     CvPoint2D32f *u = 0, *v = 0, *v2 = 0;
72     CvMat *_u = 0, *_v = 0, *_v2 = 0;
73     char* status = 0;
74
75     IplImage* imgI = 0;
76     IplImage* imgJ = 0;
77
78     int  n = 0, i = 0;
79
80     sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" );
81     _u = (CvMat*)cvLoad( filename );
82
83     if( !_u )
84     {
85         ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
86         code = cvtest::TS::FAIL_MISSING_TEST_DATA;
87         goto _exit_;
88     }
89
90     sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" );
91     _v = (CvMat*)cvLoad( filename );
92
93     if( !_v )
94     {
95         ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
96         code = cvtest::TS::FAIL_MISSING_TEST_DATA;
97         goto _exit_;
98     }
99
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 )
102     {
103         ts->printf( cvtest::TS::LOG, "the loaded matrices of points are not valid\n" );
104         code = cvtest::TS::FAIL_MISSING_TEST_DATA;
105         goto _exit_;
106
107     }
108
109     u = (CvPoint2D32f*)_u->data.fl;
110     v = (CvPoint2D32f*)_v->data.fl;
111
112     /* allocate adidtional buffers */
113     _v2 = cvCloneMat( _u );
114     v2 = (CvPoint2D32f*)_v2->data.fl;
115
116     /* read first image */
117     sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_1.bmp" );
118     imgI = cvLoadImage( filename, -1 );
119
120     if( !imgI )
121     {
122         ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
123         code = cvtest::TS::FAIL_MISSING_TEST_DATA;
124         goto _exit_;
125     }
126
127     /* read second image */
128     sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" );
129     imgJ = cvLoadImage( filename, -1 );
130
131     if( !imgJ )
132     {
133         ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
134         code = cvtest::TS::FAIL_MISSING_TEST_DATA;
135         goto _exit_;
136     }
137     
138     n = _u->rows;
139     status = (char*)cvAlloc(n*sizeof(status[0]));
140
141     /* calculate flow */
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 );
145
146     /* compare results */
147     for( i = 0; i < n; i++ )
148     {
149         if( status[i] != 0 )
150         {
151             double err;
152             if( cvIsNaN(v[i].x) )
153             {
154                 merr_j++;
155                 continue;
156             }
157
158             err = fabs(v2[i].x - v[i].x) + fabs(v2[i].y - v[i].y);
159             if( err > max_err )
160             {
161                 max_err = err;
162                 merr_i = i;
163             }
164
165             pt_exceed += err > success_error_level;
166             sum_err += err;
167             pt_cmpd++;
168         }
169         else
170         {
171             if( !cvIsNaN( v[i].x ))
172             {
173                 merr_i = i;
174                 merr_k++;
175                 ts->printf( cvtest::TS::LOG, "The algorithm lost the point #%d\n", i );
176                 code = cvtest::TS::FAIL_BAD_ACCURACY;
177                 goto _exit_;
178             }
179         }
180     }
181     
182     if( pt_exceed > bad_points_max )
183     {
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;
187         goto _exit_;
188     }
189
190     if( max_err > 1 )
191     {
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;
194         goto _exit_;
195     }
196
197 _exit_:
198
199     cvFree( &status );
200     cvReleaseMat( &_u );
201     cvReleaseMat( &_v );
202     cvReleaseMat( &_v2 );
203     
204     cvReleaseImage( &imgI );
205     cvReleaseImage( &imgJ );
206
207     if( code < 0 )
208         ts->set_failed_test_info( code );
209 }
210
211
212 TEST(Video_OpticalFlowPyrLK, accuracy) { CV_OptFlowPyrLKTest test; test.safe_run(); }
213
214 /* End of file. */