Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/video/test/test_tvl1optflow.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
44
namespace opencv_test { namespace {
45
46
//#define DUMP
47
48
// first four bytes, should be the same in little endian
49
const float FLO_TAG_FLOAT = 202021.25f; // check for this when READING the file
50
51
#ifdef DUMP
52
// binary file format for flow data specified here:
53
// http://vision.middlebury.edu/flow/data/
54
void writeOpticalFlowToFile(const Mat_<Point2f>& flow, const string& fileName)
55
{
56
const char FLO_TAG_STRING[] = "PIEH"; // use this when WRITING the file
57
ofstream file(fileName.c_str(), ios_base::binary);
58
59
file << FLO_TAG_STRING;
60
61
file.write((const char*) &flow.cols, sizeof(int));
62
file.write((const char*) &flow.rows, sizeof(int));
63
64
for (int i = 0; i < flow.rows; ++i)
65
{
66
for (int j = 0; j < flow.cols; ++j)
67
{
68
const Point2f u = flow(i, j);
69
70
file.write((const char*) &u.x, sizeof(float));
71
file.write((const char*) &u.y, sizeof(float));
72
}
73
}
74
}
75
#endif
76
77
// binary file format for flow data specified here:
78
// http://vision.middlebury.edu/flow/data/
79
void readOpticalFlowFromFile(Mat_<Point2f>& flow, const string& fileName)
80
{
81
std::ifstream file(fileName.c_str(), std::ios_base::binary);
82
83
float tag;
84
file.read((char*) &tag, sizeof(float));
85
CV_Assert( tag == FLO_TAG_FLOAT );
86
87
Size size;
88
89
file.read((char*) &size.width, sizeof(int));
90
file.read((char*) &size.height, sizeof(int));
91
92
flow.create(size);
93
94
for (int i = 0; i < flow.rows; ++i)
95
{
96
for (int j = 0; j < flow.cols; ++j)
97
{
98
Point2f u;
99
100
file.read((char*) &u.x, sizeof(float));
101
file.read((char*) &u.y, sizeof(float));
102
103
flow(i, j) = u;
104
}
105
}
106
file.close();
107
}
108
109
bool isFlowCorrect(Point2f u)
110
{
111
return !cvIsNaN(u.x) && !cvIsNaN(u.y) && (fabs(u.x) < 1e9) && (fabs(u.y) < 1e9);
112
}
113
114
void check(const Mat_<Point2f>& gold, const Mat_<Point2f>& flow, double threshold = 0.1, double expectedAccuracy = 0.95)
115
{
116
threshold = threshold*threshold;
117
118
size_t gold_counter = 0;
119
size_t valid_counter = 0;
120
121
for (int i = 0; i < gold.rows; ++i)
122
{
123
for (int j = 0; j < gold.cols; ++j)
124
{
125
const Point2f u1 = gold(i, j);
126
const Point2f u2 = flow(i, j);
127
128
if (isFlowCorrect(u1))
129
{
130
gold_counter++;
131
if (isFlowCorrect(u2))
132
{
133
const Point2f diff = u1 - u2;
134
double err = diff.ddot(diff);
135
if (err <= threshold)
136
valid_counter++;
137
}
138
}
139
}
140
}
141
EXPECT_GE(valid_counter, expectedAccuracy * gold_counter);
142
}
143
144
TEST(Video_calcOpticalFlowDual_TVL1, Regression)
145
{
146
const string frame1_path = TS::ptr()->get_data_path() + "optflow/RubberWhale1.png";
147
const string frame2_path = TS::ptr()->get_data_path() + "optflow/RubberWhale2.png";
148
const string gold_flow_path = TS::ptr()->get_data_path() + "optflow/tvl1_flow.flo";
149
150
Mat frame1 = imread(frame1_path, IMREAD_GRAYSCALE);
151
Mat frame2 = imread(frame2_path, IMREAD_GRAYSCALE);
152
ASSERT_FALSE(frame1.empty());
153
ASSERT_FALSE(frame2.empty());
154
155
Mat_<Point2f> flow;
156
Ptr<DualTVL1OpticalFlow> tvl1 = cv::DualTVL1OpticalFlow::create();
157
158
tvl1->calc(frame1, frame2, flow);
159
160
#ifdef DUMP
161
writeOpticalFlowToFile(flow, gold_flow_path);
162
#else
163
Mat_<Point2f> gold;
164
readOpticalFlowFromFile(gold, gold_flow_path);
165
166
ASSERT_EQ(gold.rows, flow.rows);
167
ASSERT_EQ(gold.cols, flow.cols);
168
169
check(gold, flow);
170
#endif
171
}
172
173
}} // namespace
174
175