Move the sources to trunk
[opencv] / tests / cv / src / aoptflowpyrlk.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 "cvtest.h"
43
44 /* ///////////////////// pyrlk_test ///////////////////////// */
45
46 class CV_OptFlowPyrLKTest : public CvTest
47 {
48 public:
49     CV_OptFlowPyrLKTest();
50 protected:
51     void run(int);
52 };
53
54
55 CV_OptFlowPyrLKTest::CV_OptFlowPyrLKTest():
56     CvTest( "optflow-pyrlk", "cvCalcOpticalFlowPyrLK" )
57 {
58     support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
59 }
60
61 void CV_OptFlowPyrLKTest::run( int )
62 {
63     int code = CvTS::OK;
64
65     const double success_error_level = 0.2;
66     const int bad_points_max = 2;
67
68     /* test parameters */
69     double  max_err = 0., sum_err = 0;
70     int     pt_cmpd = 0;
71     int     pt_exceed = 0;
72     int     merr_i = 0, merr_j = 0, merr_k = 0;
73     char    filename[1000];
74
75     CvPoint2D32f *u = 0, *v = 0, *v2 = 0;
76     CvMat *_u = 0, *_v = 0, *_v2 = 0;
77     char* status = 0;
78
79     IplImage* imgI = 0;
80     IplImage* imgJ = 0;
81
82     int  n = 0, i = 0;
83
84     sprintf( filename, "%soptflow/%s", ts->get_data_path(), "lk_prev.dat" );
85     _u = (CvMat*)cvLoad( filename );
86
87     if( !_u )
88     {
89         ts->printf( CvTS::LOG, "could not read %s\n", filename );
90         code = CvTS::FAIL_MISSING_TEST_DATA;
91         goto _exit_;
92     }
93
94     sprintf( filename, "%soptflow/%s", ts->get_data_path(), "lk_next.dat" );
95     _v = (CvMat*)cvLoad( filename );
96
97     if( !_v )
98     {
99         ts->printf( CvTS::LOG, "could not read %s\n", filename );
100         code = CvTS::FAIL_MISSING_TEST_DATA;
101         goto _exit_;
102     }
103
104     if( _u->cols != 2 || CV_MAT_TYPE(_u->type) != CV_32F ||
105         _v->cols != 2 || CV_MAT_TYPE(_v->type) != CV_32F || _v->rows != _u->rows )
106     {
107         ts->printf( CvTS::LOG, "the loaded matrices of points are not valid\n" );
108         code = CvTS::FAIL_MISSING_TEST_DATA;
109         goto _exit_;
110
111     }
112
113     u = (CvPoint2D32f*)_u->data.fl;
114     v = (CvPoint2D32f*)_v->data.fl;
115
116     /* allocate adidtional buffers */
117     _v2 = cvCloneMat( _u );
118     v2 = (CvPoint2D32f*)_v2->data.fl;
119
120     /* read first image */
121     sprintf( filename, "%soptflow/%s", ts->get_data_path(), "rock_1.bmp" );
122     imgI = cvLoadImage( filename, -1 );
123
124     if( !imgI )
125     {
126         ts->printf( CvTS::LOG, "could not read %s\n", filename );
127         code = CvTS::FAIL_MISSING_TEST_DATA;
128         goto _exit_;
129     }
130
131     /* read second image */
132     sprintf( filename, "%soptflow/%s", ts->get_data_path(), "rock_2.bmp" );
133     imgJ = cvLoadImage( filename, -1 );
134
135     if( !imgJ )
136     {
137         ts->printf( CvTS::LOG, "could not read %s\n", filename );
138         code = CvTS::FAIL_MISSING_TEST_DATA;
139         goto _exit_;
140     }
141     
142     n = _u->rows;
143     status = (char*)cvAlloc(n*sizeof(status[0]));
144
145     /* calculate flow */
146     cvCalcOpticalFlowPyrLK( imgI, imgJ, 0, 0, u, v2, n, cvSize( 20, 20 ),
147                             4, status, 0, cvTermCriteria( CV_TERMCRIT_ITER|
148                             CV_TERMCRIT_EPS, 30, 0.01f ), 0 );
149
150     /* compare results */
151     for( i = 0; i < n; i++ )
152     {
153         if( status[i] != 0 )
154         {
155             double err;
156             if( cvIsNaN(v[i].x) )
157             {
158                 merr_j++;
159                 continue;
160             }
161
162             err = fabs(v2[i].x - v[i].x) + fabs(v2[i].y - v[i].y);
163             if( err > max_err )
164             {
165                 max_err = err;
166                 merr_i = i;
167             }
168
169             pt_exceed += err > success_error_level;
170             if( pt_exceed > bad_points_max )
171             {
172                 ts->printf( CvTS::LOG,
173                     "The number of poorly tracked points is too big (>=%d)\n", pt_exceed );
174                 code = CvTS::FAIL_BAD_ACCURACY;
175                 goto _exit_;
176             }
177
178             sum_err += err;
179             pt_cmpd++;
180         }
181         else
182         {
183             if( !cvIsNaN( v[i].x ))
184             {
185                 merr_i = i;
186                 merr_k++;
187                 ts->printf( CvTS::LOG, "The algorithm lost the point #%d\n", i );
188                 code = CvTS::FAIL_BAD_ACCURACY;
189                 goto _exit_;
190             }
191         }
192     }
193
194     if( max_err > 1 )
195     {
196         ts->printf( CvTS::LOG, "Maximum tracking error is too big (=%g)\n", max_err );
197         code = CvTS::FAIL_BAD_ACCURACY;
198         goto _exit_;
199     }
200
201 _exit_:
202
203     cvFree( &status );
204     cvReleaseMat( &_u );
205     cvReleaseMat( &_v );
206     cvReleaseMat( &_v2 );
207     
208     cvReleaseImage( &imgI );
209     cvReleaseImage( &imgJ );
210
211     if( code < 0 )
212         ts->set_failed_test_info( code );
213 }
214
215 CV_OptFlowPyrLKTest optflow_pyr_lk_test;
216
217 /* End of file. */