2011-02-10 04:55:11 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "test_precomp.hpp"
|
2013-04-11 21:38:33 +08:00
|
|
|
#include "opencv2/video/tracking_c.h"
|
2011-02-10 04:55:11 +08:00
|
|
|
|
2017-11-05 21:48:40 +08:00
|
|
|
namespace opencv_test { namespace {
|
|
|
|
|
2011-02-10 04:55:11 +08:00
|
|
|
/* ///////////////////// pyrlk_test ///////////////////////// */
|
|
|
|
|
|
|
|
class CV_OptFlowPyrLKTest : public cvtest::BaseTest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CV_OptFlowPyrLKTest();
|
|
|
|
protected:
|
|
|
|
void run(int);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CV_OptFlowPyrLKTest::CV_OptFlowPyrLKTest() {}
|
|
|
|
|
|
|
|
void CV_OptFlowPyrLKTest::run( int )
|
|
|
|
{
|
|
|
|
int code = cvtest::TS::OK;
|
|
|
|
|
2011-07-12 07:03:05 +08:00
|
|
|
const double success_error_level = 0.3;
|
|
|
|
const int bad_points_max = 8;
|
2011-02-10 04:55:11 +08:00
|
|
|
|
|
|
|
/* test parameters */
|
|
|
|
double max_err = 0., sum_err = 0;
|
|
|
|
int pt_cmpd = 0;
|
|
|
|
int pt_exceed = 0;
|
2016-10-17 23:13:58 +08:00
|
|
|
int merr_i = 0, merr_j = 0, merr_k = 0, merr_nan = 0;
|
2011-02-10 04:55:11 +08:00
|
|
|
char filename[1000];
|
|
|
|
|
2018-11-02 05:27:06 +08:00
|
|
|
CvPoint2D32f *v = 0, *v2 = 0;
|
|
|
|
cv::Mat _u, _v, _v2;
|
2011-02-10 04:55:11 +08:00
|
|
|
|
2018-11-02 05:27:06 +08:00
|
|
|
cv::Mat imgI, imgJ;
|
2011-02-10 04:55:11 +08:00
|
|
|
|
|
|
|
int n = 0, i = 0;
|
|
|
|
|
2018-11-02 05:27:06 +08:00
|
|
|
for(;;)
|
|
|
|
{
|
2011-02-10 04:55:11 +08:00
|
|
|
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" );
|
|
|
|
|
|
|
|
{
|
2018-11-02 05:27:06 +08:00
|
|
|
FileStorage fs(filename, FileStorage::READ);
|
|
|
|
fs["points"] >> _u;
|
|
|
|
if( _u.empty() )
|
|
|
|
{
|
|
|
|
ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
|
|
|
|
code = cvtest::TS::FAIL_MISSING_TEST_DATA;
|
|
|
|
break;
|
|
|
|
}
|
2011-02-10 04:55:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" );
|
|
|
|
|
|
|
|
{
|
2018-11-02 05:27:06 +08:00
|
|
|
FileStorage fs(filename, FileStorage::READ);
|
|
|
|
fs["points"] >> _v;
|
|
|
|
if( _v.empty() )
|
|
|
|
{
|
|
|
|
ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
|
|
|
|
code = cvtest::TS::FAIL_MISSING_TEST_DATA;
|
|
|
|
break;
|
|
|
|
}
|
2011-02-10 04:55:11 +08:00
|
|
|
}
|
|
|
|
|
2018-11-02 05:27:06 +08:00
|
|
|
if( _u.cols != 2 || _u.type() != CV_32F ||
|
|
|
|
_v.cols != 2 || _v.type() != CV_32F ||
|
|
|
|
_v.rows != _u.rows )
|
2011-02-10 04:55:11 +08:00
|
|
|
{
|
|
|
|
ts->printf( cvtest::TS::LOG, "the loaded matrices of points are not valid\n" );
|
|
|
|
code = cvtest::TS::FAIL_MISSING_TEST_DATA;
|
2018-11-02 05:27:06 +08:00
|
|
|
break;
|
2011-02-10 04:55:11 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read first image */
|
|
|
|
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_1.bmp" );
|
2018-11-02 05:27:06 +08:00
|
|
|
imgI = cv::imread( filename, cv::IMREAD_UNCHANGED );
|
2011-02-10 04:55:11 +08:00
|
|
|
|
2018-11-02 05:27:06 +08:00
|
|
|
if( imgI.empty() )
|
2011-02-10 04:55:11 +08:00
|
|
|
{
|
|
|
|
ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
|
|
|
|
code = cvtest::TS::FAIL_MISSING_TEST_DATA;
|
2018-11-02 05:27:06 +08:00
|
|
|
break;
|
2011-02-10 04:55:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read second image */
|
|
|
|
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" );
|
2018-11-02 05:27:06 +08:00
|
|
|
imgJ = cv::imread( filename, cv::IMREAD_UNCHANGED );
|
2011-02-10 04:55:11 +08:00
|
|
|
|
2018-11-02 05:27:06 +08:00
|
|
|
if( imgJ.empty() )
|
2011-02-10 04:55:11 +08:00
|
|
|
{
|
|
|
|
ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
|
|
|
|
code = cvtest::TS::FAIL_MISSING_TEST_DATA;
|
2018-11-02 05:27:06 +08:00
|
|
|
break;
|
2011-02-10 04:55:11 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2018-11-02 05:27:06 +08:00
|
|
|
n = _u.rows;
|
|
|
|
std::vector<uchar> status(n, (uchar)0);
|
2011-02-10 04:55:11 +08:00
|
|
|
|
|
|
|
/* calculate flow */
|
2018-11-02 05:27:06 +08:00
|
|
|
calcOpticalFlowPyrLK(imgI, imgJ, _u, _v2, status, cv::noArray(), Size( 41, 41 ), 4,
|
|
|
|
TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0.01f ), 0 );
|
|
|
|
|
|
|
|
v = (CvPoint2D32f*)_v.ptr();
|
|
|
|
v2 = (CvPoint2D32f*)_v2.ptr();
|
2011-02-10 04:55:11 +08:00
|
|
|
|
|
|
|
/* compare results */
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
if( status[i] != 0 )
|
|
|
|
{
|
|
|
|
double err;
|
2016-10-17 23:13:58 +08:00
|
|
|
if( cvIsNaN(v[i].x) || cvIsNaN(v[i].y) )
|
2011-02-10 04:55:11 +08:00
|
|
|
{
|
|
|
|
merr_j++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-17 23:13:58 +08:00
|
|
|
if( cvIsNaN(v2[i].x) || cvIsNaN(v2[i].y) )
|
|
|
|
{
|
|
|
|
merr_nan++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-02-10 04:55:11 +08:00
|
|
|
err = fabs(v2[i].x - v[i].x) + fabs(v2[i].y - v[i].y);
|
|
|
|
if( err > max_err )
|
|
|
|
{
|
|
|
|
max_err = err;
|
|
|
|
merr_i = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
pt_exceed += err > success_error_level;
|
|
|
|
sum_err += err;
|
|
|
|
pt_cmpd++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( !cvIsNaN( v[i].x ))
|
|
|
|
{
|
|
|
|
merr_i = i;
|
|
|
|
merr_k++;
|
|
|
|
ts->printf( cvtest::TS::LOG, "The algorithm lost the point #%d\n", i );
|
|
|
|
code = cvtest::TS::FAIL_BAD_ACCURACY;
|
2018-11-02 05:27:06 +08:00
|
|
|
break;
|
2011-02-10 04:55:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-02 05:27:06 +08:00
|
|
|
if( i < n )
|
|
|
|
break;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2011-07-12 07:03:05 +08:00
|
|
|
if( pt_exceed > bad_points_max )
|
|
|
|
{
|
|
|
|
ts->printf( cvtest::TS::LOG,
|
|
|
|
"The number of poorly tracked points is too big (>=%d)\n", pt_exceed );
|
|
|
|
code = cvtest::TS::FAIL_BAD_ACCURACY;
|
2018-11-02 05:27:06 +08:00
|
|
|
break;
|
2011-07-12 07:03:05 +08:00
|
|
|
}
|
2011-02-10 04:55:11 +08:00
|
|
|
|
|
|
|
if( max_err > 1 )
|
|
|
|
{
|
2012-03-17 17:22:31 +08:00
|
|
|
ts->printf( cvtest::TS::LOG, "Maximum tracking error is too big (=%g) at %d\n", max_err, merr_i );
|
2011-02-10 04:55:11 +08:00
|
|
|
code = cvtest::TS::FAIL_BAD_ACCURACY;
|
2018-11-02 05:27:06 +08:00
|
|
|
break;
|
2011-02-10 04:55:11 +08:00
|
|
|
}
|
|
|
|
|
2016-10-17 23:13:58 +08:00
|
|
|
if( merr_nan > 0 )
|
|
|
|
{
|
|
|
|
ts->printf( cvtest::TS::LOG, "NAN tracking result with status != 0 (%d times)\n", merr_nan );
|
|
|
|
code = cvtest::TS::FAIL_BAD_ACCURACY;
|
|
|
|
}
|
|
|
|
|
2018-11-02 05:27:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2011-02-10 04:55:11 +08:00
|
|
|
if( code < 0 )
|
|
|
|
ts->set_failed_test_info( code );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Video_OpticalFlowPyrLK, accuracy) { CV_OptFlowPyrLKTest test; test.safe_run(); }
|
|
|
|
|
2013-01-30 20:07:38 +08:00
|
|
|
TEST(Video_OpticalFlowPyrLK, submat)
|
|
|
|
{
|
|
|
|
// see bug #2075
|
|
|
|
std::string path = cvtest::TS::ptr()->get_data_path() + "../cv/shared/lena.png";
|
|
|
|
|
|
|
|
cv::Mat lenaImg = cv::imread(path);
|
|
|
|
ASSERT_FALSE(lenaImg.empty());
|
|
|
|
|
|
|
|
cv::Mat wholeImage;
|
2017-12-13 20:00:38 +08:00
|
|
|
cv::resize(lenaImg, wholeImage, cv::Size(1024, 1024), 0, 0, cv::INTER_LINEAR_EXACT);
|
2013-01-30 20:07:38 +08:00
|
|
|
|
|
|
|
cv::Mat img1 = wholeImage(cv::Rect(0, 0, 640, 360)).clone();
|
|
|
|
cv::Mat img2 = wholeImage(cv::Rect(40, 60, 640, 360));
|
|
|
|
|
|
|
|
std::vector<uchar> status;
|
|
|
|
std::vector<float> error;
|
|
|
|
std::vector<cv::Point2f> prev;
|
|
|
|
std::vector<cv::Point2f> next;
|
|
|
|
|
|
|
|
cv::RNG rng(123123);
|
|
|
|
|
|
|
|
for(int i = 0; i < 50; ++i)
|
|
|
|
{
|
|
|
|
int x = rng.uniform(0, 640);
|
|
|
|
int y = rng.uniform(0, 360);
|
|
|
|
|
|
|
|
prev.push_back(cv::Point2f((float)x, (float)y));
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NO_THROW(cv::calcOpticalFlowPyrLK(img1, img2, prev, next, status, error));
|
|
|
|
}
|
2017-11-05 21:48:40 +08:00
|
|
|
|
|
|
|
}} // namespace
|