Path: blob/master/modules/video/test/test_optflowpyrlk.cpp
16339 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of Intel Corporation may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041#include "test_precomp.hpp"42#include "opencv2/video/tracking_c.h"4344namespace opencv_test { namespace {4546/* ///////////////////// pyrlk_test ///////////////////////// */4748class CV_OptFlowPyrLKTest : public cvtest::BaseTest49{50public:51CV_OptFlowPyrLKTest();52protected:53void run(int);54};555657CV_OptFlowPyrLKTest::CV_OptFlowPyrLKTest() {}5859void CV_OptFlowPyrLKTest::run( int )60{61int code = cvtest::TS::OK;6263const double success_error_level = 0.3;64const int bad_points_max = 8;6566/* test parameters */67double max_err = 0., sum_err = 0;68int pt_cmpd = 0;69int pt_exceed = 0;70int merr_i = 0, merr_j = 0, merr_k = 0, merr_nan = 0;71char filename[1000];7273CvPoint2D32f *u = 0, *v = 0, *v2 = 0;74CvMat *_u = 0, *_v = 0, *_v2 = 0;75char* status = 0;7677IplImage imgI;78IplImage imgJ;79cv::Mat imgI2, imgJ2;8081int n = 0, i = 0;8283sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" );84_u = (CvMat*)cvLoad( filename );8586if( !_u )87{88ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );89code = cvtest::TS::FAIL_MISSING_TEST_DATA;90goto _exit_;91}9293sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" );94_v = (CvMat*)cvLoad( filename );9596if( !_v )97{98ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );99code = cvtest::TS::FAIL_MISSING_TEST_DATA;100goto _exit_;101}102103if( _u->cols != 2 || CV_MAT_TYPE(_u->type) != CV_32F ||104_v->cols != 2 || CV_MAT_TYPE(_v->type) != CV_32F || _v->rows != _u->rows )105{106ts->printf( cvtest::TS::LOG, "the loaded matrices of points are not valid\n" );107code = cvtest::TS::FAIL_MISSING_TEST_DATA;108goto _exit_;109110}111112u = (CvPoint2D32f*)_u->data.fl;113v = (CvPoint2D32f*)_v->data.fl;114115/* allocate adidtional buffers */116_v2 = cvCloneMat( _u );117v2 = (CvPoint2D32f*)_v2->data.fl;118119/* read first image */120sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_1.bmp" );121imgI2 = cv::imread( filename, cv::IMREAD_UNCHANGED );122imgI = cvIplImage(imgI2);123124if( imgI2.empty() )125{126ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );127code = cvtest::TS::FAIL_MISSING_TEST_DATA;128goto _exit_;129}130131/* read second image */132sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" );133imgJ2 = cv::imread( filename, cv::IMREAD_UNCHANGED );134imgJ = cvIplImage(imgJ2);135136if( imgJ2.empty() )137{138ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );139code = cvtest::TS::FAIL_MISSING_TEST_DATA;140goto _exit_;141}142143n = _u->rows;144status = (char*)cvAlloc(n*sizeof(status[0]));145146/* calculate flow */147cvCalcOpticalFlowPyrLK( &imgI, &imgJ, 0, 0, u, v2, n, cvSize( 41, 41 ),1484, status, 0, cvTermCriteria( CV_TERMCRIT_ITER|149CV_TERMCRIT_EPS, 30, 0.01f ), 0 );150151/* compare results */152for( i = 0; i < n; i++ )153{154if( status[i] != 0 )155{156double err;157if( cvIsNaN(v[i].x) || cvIsNaN(v[i].y) )158{159merr_j++;160continue;161}162163if( cvIsNaN(v2[i].x) || cvIsNaN(v2[i].y) )164{165merr_nan++;166continue;167}168169err = fabs(v2[i].x - v[i].x) + fabs(v2[i].y - v[i].y);170if( err > max_err )171{172max_err = err;173merr_i = i;174}175176pt_exceed += err > success_error_level;177sum_err += err;178pt_cmpd++;179}180else181{182if( !cvIsNaN( v[i].x ))183{184merr_i = i;185merr_k++;186ts->printf( cvtest::TS::LOG, "The algorithm lost the point #%d\n", i );187code = cvtest::TS::FAIL_BAD_ACCURACY;188goto _exit_;189}190}191}192193if( pt_exceed > bad_points_max )194{195ts->printf( cvtest::TS::LOG,196"The number of poorly tracked points is too big (>=%d)\n", pt_exceed );197code = cvtest::TS::FAIL_BAD_ACCURACY;198goto _exit_;199}200201if( max_err > 1 )202{203ts->printf( cvtest::TS::LOG, "Maximum tracking error is too big (=%g) at %d\n", max_err, merr_i );204code = cvtest::TS::FAIL_BAD_ACCURACY;205goto _exit_;206}207208if( merr_nan > 0 )209{210ts->printf( cvtest::TS::LOG, "NAN tracking result with status != 0 (%d times)\n", merr_nan );211code = cvtest::TS::FAIL_BAD_ACCURACY;212goto _exit_;213}214215_exit_:216217cvFree( &status );218cvReleaseMat( &_u );219cvReleaseMat( &_v );220cvReleaseMat( &_v2 );221222if( code < 0 )223ts->set_failed_test_info( code );224}225226227TEST(Video_OpticalFlowPyrLK, accuracy) { CV_OptFlowPyrLKTest test; test.safe_run(); }228229TEST(Video_OpticalFlowPyrLK, submat)230{231// see bug #2075232std::string path = cvtest::TS::ptr()->get_data_path() + "../cv/shared/lena.png";233234cv::Mat lenaImg = cv::imread(path);235ASSERT_FALSE(lenaImg.empty());236237cv::Mat wholeImage;238cv::resize(lenaImg, wholeImage, cv::Size(1024, 1024), 0, 0, cv::INTER_LINEAR_EXACT);239240cv::Mat img1 = wholeImage(cv::Rect(0, 0, 640, 360)).clone();241cv::Mat img2 = wholeImage(cv::Rect(40, 60, 640, 360));242243std::vector<uchar> status;244std::vector<float> error;245std::vector<cv::Point2f> prev;246std::vector<cv::Point2f> next;247248cv::RNG rng(123123);249250for(int i = 0; i < 50; ++i)251{252int x = rng.uniform(0, 640);253int y = rng.uniform(0, 360);254255prev.push_back(cv::Point2f((float)x, (float)y));256}257258ASSERT_NO_THROW(cv::calcOpticalFlowPyrLK(img1, img2, prev, next, status, error));259}260261}} // namespace262263264