Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/video/test/test_optflowpyrlk.cpp
16339 views
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
#include "opencv2/video/tracking_c.h"
44
45
namespace opencv_test { namespace {
46
47
/* ///////////////////// pyrlk_test ///////////////////////// */
48
49
class CV_OptFlowPyrLKTest : public cvtest::BaseTest
50
{
51
public:
52
CV_OptFlowPyrLKTest();
53
protected:
54
void run(int);
55
};
56
57
58
CV_OptFlowPyrLKTest::CV_OptFlowPyrLKTest() {}
59
60
void CV_OptFlowPyrLKTest::run( int )
61
{
62
int code = cvtest::TS::OK;
63
64
const double success_error_level = 0.3;
65
const int bad_points_max = 8;
66
67
/* test parameters */
68
double max_err = 0., sum_err = 0;
69
int pt_cmpd = 0;
70
int pt_exceed = 0;
71
int merr_i = 0, merr_j = 0, merr_k = 0, merr_nan = 0;
72
char filename[1000];
73
74
CvPoint2D32f *u = 0, *v = 0, *v2 = 0;
75
CvMat *_u = 0, *_v = 0, *_v2 = 0;
76
char* status = 0;
77
78
IplImage imgI;
79
IplImage imgJ;
80
cv::Mat imgI2, imgJ2;
81
82
int n = 0, i = 0;
83
84
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" );
85
_u = (CvMat*)cvLoad( filename );
86
87
if( !_u )
88
{
89
ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
90
code = cvtest::TS::FAIL_MISSING_TEST_DATA;
91
goto _exit_;
92
}
93
94
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" );
95
_v = (CvMat*)cvLoad( filename );
96
97
if( !_v )
98
{
99
ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
100
code = cvtest::TS::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( cvtest::TS::LOG, "the loaded matrices of points are not valid\n" );
108
code = cvtest::TS::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().c_str(), "rock_1.bmp" );
122
imgI2 = cv::imread( filename, cv::IMREAD_UNCHANGED );
123
imgI = cvIplImage(imgI2);
124
125
if( imgI2.empty() )
126
{
127
ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
128
code = cvtest::TS::FAIL_MISSING_TEST_DATA;
129
goto _exit_;
130
}
131
132
/* read second image */
133
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" );
134
imgJ2 = cv::imread( filename, cv::IMREAD_UNCHANGED );
135
imgJ = cvIplImage(imgJ2);
136
137
if( imgJ2.empty() )
138
{
139
ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
140
code = cvtest::TS::FAIL_MISSING_TEST_DATA;
141
goto _exit_;
142
}
143
144
n = _u->rows;
145
status = (char*)cvAlloc(n*sizeof(status[0]));
146
147
/* calculate flow */
148
cvCalcOpticalFlowPyrLK( &imgI, &imgJ, 0, 0, u, v2, n, cvSize( 41, 41 ),
149
4, status, 0, cvTermCriteria( CV_TERMCRIT_ITER|
150
CV_TERMCRIT_EPS, 30, 0.01f ), 0 );
151
152
/* compare results */
153
for( i = 0; i < n; i++ )
154
{
155
if( status[i] != 0 )
156
{
157
double err;
158
if( cvIsNaN(v[i].x) || cvIsNaN(v[i].y) )
159
{
160
merr_j++;
161
continue;
162
}
163
164
if( cvIsNaN(v2[i].x) || cvIsNaN(v2[i].y) )
165
{
166
merr_nan++;
167
continue;
168
}
169
170
err = fabs(v2[i].x - v[i].x) + fabs(v2[i].y - v[i].y);
171
if( err > max_err )
172
{
173
max_err = err;
174
merr_i = i;
175
}
176
177
pt_exceed += err > success_error_level;
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( cvtest::TS::LOG, "The algorithm lost the point #%d\n", i );
188
code = cvtest::TS::FAIL_BAD_ACCURACY;
189
goto _exit_;
190
}
191
}
192
}
193
194
if( pt_exceed > bad_points_max )
195
{
196
ts->printf( cvtest::TS::LOG,
197
"The number of poorly tracked points is too big (>=%d)\n", pt_exceed );
198
code = cvtest::TS::FAIL_BAD_ACCURACY;
199
goto _exit_;
200
}
201
202
if( max_err > 1 )
203
{
204
ts->printf( cvtest::TS::LOG, "Maximum tracking error is too big (=%g) at %d\n", max_err, merr_i );
205
code = cvtest::TS::FAIL_BAD_ACCURACY;
206
goto _exit_;
207
}
208
209
if( merr_nan > 0 )
210
{
211
ts->printf( cvtest::TS::LOG, "NAN tracking result with status != 0 (%d times)\n", merr_nan );
212
code = cvtest::TS::FAIL_BAD_ACCURACY;
213
goto _exit_;
214
}
215
216
_exit_:
217
218
cvFree( &status );
219
cvReleaseMat( &_u );
220
cvReleaseMat( &_v );
221
cvReleaseMat( &_v2 );
222
223
if( code < 0 )
224
ts->set_failed_test_info( code );
225
}
226
227
228
TEST(Video_OpticalFlowPyrLK, accuracy) { CV_OptFlowPyrLKTest test; test.safe_run(); }
229
230
TEST(Video_OpticalFlowPyrLK, submat)
231
{
232
// see bug #2075
233
std::string path = cvtest::TS::ptr()->get_data_path() + "../cv/shared/lena.png";
234
235
cv::Mat lenaImg = cv::imread(path);
236
ASSERT_FALSE(lenaImg.empty());
237
238
cv::Mat wholeImage;
239
cv::resize(lenaImg, wholeImage, cv::Size(1024, 1024), 0, 0, cv::INTER_LINEAR_EXACT);
240
241
cv::Mat img1 = wholeImage(cv::Rect(0, 0, 640, 360)).clone();
242
cv::Mat img2 = wholeImage(cv::Rect(40, 60, 640, 360));
243
244
std::vector<uchar> status;
245
std::vector<float> error;
246
std::vector<cv::Point2f> prev;
247
std::vector<cv::Point2f> next;
248
249
cv::RNG rng(123123);
250
251
for(int i = 0; i < 50; ++i)
252
{
253
int x = rng.uniform(0, 640);
254
int y = rng.uniform(0, 360);
255
256
prev.push_back(cv::Point2f((float)x, (float)y));
257
}
258
259
ASSERT_NO_THROW(cv::calcOpticalFlowPyrLK(img1, img2, prev, next, status, error));
260
}
261
262
}} // namespace
263
264