Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/video/test/test_accum.cpp
16344 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/imgproc/imgproc_c.h"
44
45
namespace opencv_test { namespace {
46
47
class CV_AccumBaseTest : public cvtest::ArrayTest
48
{
49
public:
50
CV_AccumBaseTest();
51
52
protected:
53
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
54
double get_success_error_level( int test_case_idx, int i, int j );
55
double alpha;
56
};
57
58
59
CV_AccumBaseTest::CV_AccumBaseTest()
60
{
61
test_array[INPUT].push_back(NULL);
62
test_array[INPUT_OUTPUT].push_back(NULL);
63
test_array[REF_INPUT_OUTPUT].push_back(NULL);
64
test_array[MASK].push_back(NULL);
65
optional_mask = true;
66
element_wise_relative_error = false;
67
} // ctor
68
69
70
void CV_AccumBaseTest::get_test_array_types_and_sizes( int test_case_idx,
71
vector<vector<Size> >& sizes, vector<vector<int> >& types )
72
{
73
RNG& rng = ts->get_rng();
74
int depth = cvtest::randInt(rng) % 4, cn = cvtest::randInt(rng) & 1 ? 3 : 1;
75
int accdepth = (int)(cvtest::randInt(rng) % 2 + 1);
76
int i, input_count = (int)test_array[INPUT].size();
77
cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
78
depth = depth == 0 ? CV_8U : depth == 1 ? CV_16U : depth == 2 ? CV_32F : CV_64F;
79
accdepth = accdepth == 1 ? CV_32F : CV_64F;
80
accdepth = MAX(accdepth, depth);
81
82
for( i = 0; i < input_count; i++ )
83
types[INPUT][i] = CV_MAKETYPE(depth,cn);
84
85
types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = CV_MAKETYPE(accdepth,cn);
86
87
alpha = cvtest::randReal(rng);
88
}
89
90
91
double CV_AccumBaseTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
92
{
93
return test_mat[INPUT_OUTPUT][0].depth() < CV_64F ||
94
test_mat[INPUT][0].depth() == CV_32F ? FLT_EPSILON*100 : DBL_EPSILON*1000;
95
}
96
97
98
/// acc
99
class CV_AccTest : public CV_AccumBaseTest
100
{
101
public:
102
CV_AccTest() { }
103
protected:
104
void run_func();
105
void prepare_to_validation( int );
106
};
107
108
109
void CV_AccTest::run_func(void)
110
{
111
cvAcc( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], test_array[MASK][0] );
112
}
113
114
115
void CV_AccTest::prepare_to_validation( int )
116
{
117
const Mat& src = test_mat[INPUT][0];
118
Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
119
const Mat& mask = test_array[MASK][0] ? test_mat[MASK][0] : Mat();
120
Mat temp;
121
cvtest::add( src, 1, dst, 1, cvScalarAll(0.), temp, dst.type() );
122
cvtest::copy( temp, dst, mask );
123
}
124
125
126
/// square acc
127
class CV_SquareAccTest : public CV_AccumBaseTest
128
{
129
public:
130
CV_SquareAccTest();
131
protected:
132
void run_func();
133
void prepare_to_validation( int );
134
};
135
136
137
CV_SquareAccTest::CV_SquareAccTest()
138
{
139
}
140
141
142
void CV_SquareAccTest::run_func()
143
{
144
cvSquareAcc( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], test_array[MASK][0] );
145
}
146
147
148
void CV_SquareAccTest::prepare_to_validation( int )
149
{
150
const Mat& src = test_mat[INPUT][0];
151
Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
152
const Mat& mask = test_array[MASK][0] ? test_mat[MASK][0] : Mat();
153
Mat temp;
154
155
cvtest::convert( src, temp, dst.type() );
156
cvtest::multiply( temp, temp, temp, 1 );
157
cvtest::add( temp, 1, dst, 1, cvScalarAll(0.), temp, dst.depth() );
158
cvtest::copy( temp, dst, mask );
159
}
160
161
162
/// multiply acc
163
class CV_MultiplyAccTest : public CV_AccumBaseTest
164
{
165
public:
166
CV_MultiplyAccTest();
167
protected:
168
void run_func();
169
void prepare_to_validation( int );
170
};
171
172
173
CV_MultiplyAccTest::CV_MultiplyAccTest()
174
{
175
test_array[INPUT].push_back(NULL);
176
}
177
178
179
void CV_MultiplyAccTest::run_func()
180
{
181
cvMultiplyAcc( test_array[INPUT][0], test_array[INPUT][1],
182
test_array[INPUT_OUTPUT][0], test_array[MASK][0] );
183
}
184
185
186
void CV_MultiplyAccTest::prepare_to_validation( int )
187
{
188
const Mat& src1 = test_mat[INPUT][0];
189
const Mat& src2 = test_mat[INPUT][1];
190
Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
191
const Mat& mask = test_array[MASK][0] ? test_mat[MASK][0] : Mat();
192
Mat temp1, temp2;
193
194
cvtest::convert( src1, temp1, dst.type() );
195
cvtest::convert( src2, temp2, dst.type() );
196
197
cvtest::multiply( temp1, temp2, temp1, 1 );
198
cvtest::add( temp1, 1, dst, 1, cvScalarAll(0.), temp1, dst.depth() );
199
cvtest::copy( temp1, dst, mask );
200
}
201
202
203
/// running average
204
class CV_RunningAvgTest : public CV_AccumBaseTest
205
{
206
public:
207
CV_RunningAvgTest();
208
protected:
209
void run_func();
210
void prepare_to_validation( int );
211
};
212
213
214
CV_RunningAvgTest::CV_RunningAvgTest()
215
{
216
}
217
218
219
void CV_RunningAvgTest::run_func()
220
{
221
cvRunningAvg( test_array[INPUT][0], test_array[INPUT_OUTPUT][0],
222
alpha, test_array[MASK][0] );
223
}
224
225
226
void CV_RunningAvgTest::prepare_to_validation( int )
227
{
228
const Mat& src = test_mat[INPUT][0];
229
Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
230
Mat temp;
231
const Mat& mask = test_array[MASK][0] ? test_mat[MASK][0] : Mat();
232
double a[1], b[1];
233
int accdepth = test_mat[INPUT_OUTPUT][0].depth();
234
CvMat A = cvMat(1,1,accdepth,a), B = cvMat(1,1,accdepth,b);
235
cvSetReal1D( &A, 0, alpha);
236
cvSetReal1D( &B, 0, 1 - cvGetReal1D(&A, 0));
237
238
cvtest::convert( src, temp, dst.type() );
239
cvtest::add( src, cvGetReal1D(&A, 0), dst, cvGetReal1D(&B, 0), cvScalarAll(0.), temp, temp.depth() );
240
cvtest::copy( temp, dst, mask );
241
}
242
243
244
TEST(Video_Acc, accuracy) { CV_AccTest test; test.safe_run(); }
245
TEST(Video_AccSquared, accuracy) { CV_SquareAccTest test; test.safe_run(); }
246
TEST(Video_AccProduct, accuracy) { CV_MultiplyAccTest test; test.safe_run(); }
247
TEST(Video_RunningAvg, accuracy) { CV_RunningAvgTest test; test.safe_run(); }
248
249
}} // namespace
250
251