Path: blob/master/modules/video/test/test_tvl1optflow.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"4243namespace opencv_test { namespace {4445//#define DUMP4647// first four bytes, should be the same in little endian48const float FLO_TAG_FLOAT = 202021.25f; // check for this when READING the file4950#ifdef DUMP51// binary file format for flow data specified here:52// http://vision.middlebury.edu/flow/data/53void writeOpticalFlowToFile(const Mat_<Point2f>& flow, const string& fileName)54{55const char FLO_TAG_STRING[] = "PIEH"; // use this when WRITING the file56ofstream file(fileName.c_str(), ios_base::binary);5758file << FLO_TAG_STRING;5960file.write((const char*) &flow.cols, sizeof(int));61file.write((const char*) &flow.rows, sizeof(int));6263for (int i = 0; i < flow.rows; ++i)64{65for (int j = 0; j < flow.cols; ++j)66{67const Point2f u = flow(i, j);6869file.write((const char*) &u.x, sizeof(float));70file.write((const char*) &u.y, sizeof(float));71}72}73}74#endif7576// binary file format for flow data specified here:77// http://vision.middlebury.edu/flow/data/78void readOpticalFlowFromFile(Mat_<Point2f>& flow, const string& fileName)79{80std::ifstream file(fileName.c_str(), std::ios_base::binary);8182float tag;83file.read((char*) &tag, sizeof(float));84CV_Assert( tag == FLO_TAG_FLOAT );8586Size size;8788file.read((char*) &size.width, sizeof(int));89file.read((char*) &size.height, sizeof(int));9091flow.create(size);9293for (int i = 0; i < flow.rows; ++i)94{95for (int j = 0; j < flow.cols; ++j)96{97Point2f u;9899file.read((char*) &u.x, sizeof(float));100file.read((char*) &u.y, sizeof(float));101102flow(i, j) = u;103}104}105file.close();106}107108bool isFlowCorrect(Point2f u)109{110return !cvIsNaN(u.x) && !cvIsNaN(u.y) && (fabs(u.x) < 1e9) && (fabs(u.y) < 1e9);111}112113void check(const Mat_<Point2f>& gold, const Mat_<Point2f>& flow, double threshold = 0.1, double expectedAccuracy = 0.95)114{115threshold = threshold*threshold;116117size_t gold_counter = 0;118size_t valid_counter = 0;119120for (int i = 0; i < gold.rows; ++i)121{122for (int j = 0; j < gold.cols; ++j)123{124const Point2f u1 = gold(i, j);125const Point2f u2 = flow(i, j);126127if (isFlowCorrect(u1))128{129gold_counter++;130if (isFlowCorrect(u2))131{132const Point2f diff = u1 - u2;133double err = diff.ddot(diff);134if (err <= threshold)135valid_counter++;136}137}138}139}140EXPECT_GE(valid_counter, expectedAccuracy * gold_counter);141}142143TEST(Video_calcOpticalFlowDual_TVL1, Regression)144{145const string frame1_path = TS::ptr()->get_data_path() + "optflow/RubberWhale1.png";146const string frame2_path = TS::ptr()->get_data_path() + "optflow/RubberWhale2.png";147const string gold_flow_path = TS::ptr()->get_data_path() + "optflow/tvl1_flow.flo";148149Mat frame1 = imread(frame1_path, IMREAD_GRAYSCALE);150Mat frame2 = imread(frame2_path, IMREAD_GRAYSCALE);151ASSERT_FALSE(frame1.empty());152ASSERT_FALSE(frame2.empty());153154Mat_<Point2f> flow;155Ptr<DualTVL1OpticalFlow> tvl1 = cv::DualTVL1OpticalFlow::create();156157tvl1->calc(frame1, frame2, flow);158159#ifdef DUMP160writeOpticalFlowToFile(flow, gold_flow_path);161#else162Mat_<Point2f> gold;163readOpticalFlowFromFile(gold, gold_flow_path);164165ASSERT_EQ(gold.rows, flow.rows);166ASSERT_EQ(gold.cols, flow.cols);167168check(gold, flow);169#endif170}171172}} // namespace173174175