Move the sources to trunk
[opencv] / tests / cv / src / aaccum.cpp
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 "cvtest.h"
43
44 static const char* accum_param_names[] = { "size", "channels", "depth", "use_mask", 0 };
45 static const CvSize accum_sizes[] = {{30,30}, {320, 240}, {720,480}, {-1,-1}};
46 static const CvSize accum_whole_sizes[] = {{320,240}, {320, 240}, {720,480}, {-1,-1}};
47 static const int accum_depths[] = { CV_8U, CV_32F, -1 };
48 static const int accum_channels[] = { 1, 3, -1 };
49
50 class CV_AccumBaseTestImpl : public CvArrTest
51 {
52 public:
53     CV_AccumBaseTestImpl( const char* test_name, const char* test_funcs );
54
55 protected:
56     void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
57     double get_success_error_level( int test_case_idx, int i, int j );
58     void get_timing_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types,
59                                                 CvSize** whole_sizes, bool *are_images );
60     double alpha;
61 };
62
63
64 CV_AccumBaseTestImpl::CV_AccumBaseTestImpl( const char* test_name, const char* test_funcs )
65     : CvArrTest( test_name, test_funcs, "" )
66 {
67     test_array[INPUT].push(NULL);
68     test_array[INPUT_OUTPUT].push(NULL);
69     test_array[REF_INPUT_OUTPUT].push(NULL);
70     test_array[TEMP].push(NULL);
71     test_array[MASK].push(NULL);
72     optional_mask = true;
73     element_wise_relative_error = false;
74
75     default_timing_param_names = 0;
76     depth_list = accum_depths;
77     size_list = accum_sizes;
78     whole_size_list = accum_whole_sizes;
79     cn_list = accum_channels;
80 }
81
82
83 void CV_AccumBaseTestImpl::get_test_array_types_and_sizes( int test_case_idx,
84                                                 CvSize** sizes, int** types )
85 {
86     CvRNG* rng = ts->get_rng();
87     int depth = cvTsRandInt(rng) % 2, cn = cvTsRandInt(rng) & 1 ? 3 : 1;
88     int i, input_count = test_array[INPUT].size();
89     CvArrTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
90     depth = depth == 0 ? CV_8U : CV_32F;
91
92     for( i = 0; i < input_count; i++ )
93         types[INPUT][i] = CV_MAKETYPE(depth,cn);
94     types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = types[TEMP][0] = CV_MAKETYPE(CV_32F,cn);
95
96     alpha = cvTsRandReal(rng);
97 }
98
99
100 double CV_AccumBaseTestImpl::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
101 {
102     return FLT_EPSILON*10;
103 }
104
105
106 void CV_AccumBaseTestImpl::get_timing_test_array_types_and_sizes( int test_case_idx,
107                 CvSize** sizes, int** types, CvSize** whole_sizes, bool *are_images )
108 {
109     CvArrTest::get_timing_test_array_types_and_sizes( test_case_idx, sizes, types,
110                                                       whole_sizes, are_images );
111     types[INPUT_OUTPUT][0] = CV_MAKETYPE(CV_32F, CV_MAT_CN(types[INPUT][0]));
112     alpha = 0.333333333333333;
113 }
114
115
116 CV_AccumBaseTestImpl accum_base( "accum", "" );
117
118
119 class CV_AccumBaseTest : public CV_AccumBaseTestImpl
120 {
121 public:
122     CV_AccumBaseTest( const char* test_name, const char* test_funcs );
123 };
124
125
126 CV_AccumBaseTest::CV_AccumBaseTest( const char* test_name, const char* test_funcs )
127     : CV_AccumBaseTestImpl( test_name, test_funcs )
128 {
129     depth_list = 0;
130     size_list = 0;
131     whole_size_list = 0;
132     cn_list = 0;
133
134     default_timing_param_names = accum_param_names;
135 }
136
137
138 /// acc
139 class CV_AccTest : public CV_AccumBaseTest
140 {
141 public:
142     CV_AccTest();
143 protected:
144     void run_func();
145     void prepare_to_validation( int );
146 };
147
148
149 CV_AccTest::CV_AccTest()
150     : CV_AccumBaseTest( "accum-acc", "cvAcc" )
151 {
152 }
153
154
155 void CV_AccTest::run_func()
156 {
157     cvAcc( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], test_array[MASK][0] );
158 }
159
160
161 void CV_AccTest::prepare_to_validation( int )
162 {
163     const CvMat* src = &test_mat[INPUT][0];
164     CvMat* dst = &test_mat[REF_INPUT_OUTPUT][0];
165     CvMat* temp = &test_mat[TEMP][0];
166     const CvMat* mask = test_array[MASK][0] ? &test_mat[MASK][0] : 0;
167
168     cvTsAdd( src, cvScalarAll(1.), dst, cvScalarAll(1.), cvScalarAll(0.), temp, 0 );
169     cvTsCopy( temp, dst, mask );
170 }
171
172 CV_AccTest acc_test;
173
174
175 /// square acc
176 class CV_SquareAccTest : public CV_AccumBaseTest
177 {
178 public:
179     CV_SquareAccTest();
180 protected:
181     void run_func();
182     void prepare_to_validation( int );
183 };
184
185
186 CV_SquareAccTest::CV_SquareAccTest()
187     : CV_AccumBaseTest( "accum-squareacc", "cvSquareAcc" )
188 {
189 }
190
191
192 void CV_SquareAccTest::run_func()
193 {
194     cvSquareAcc( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], test_array[MASK][0] );
195 }
196
197
198 void CV_SquareAccTest::prepare_to_validation( int )
199 {
200     const CvMat* src = &test_mat[INPUT][0];
201     CvMat* dst = &test_mat[REF_INPUT_OUTPUT][0];
202     CvMat* temp = &test_mat[TEMP][0];
203     const CvMat* mask = test_array[MASK][0] ? &test_mat[MASK][0] : 0;
204
205     cvTsMul( src, src, cvScalarAll(1.), temp );
206     cvTsAdd( temp, cvScalarAll(1.), dst, cvScalarAll(1.), cvScalarAll(0.), temp, 0 );
207     cvTsCopy( temp, dst, mask );
208 }
209
210 CV_SquareAccTest squareacc_test;
211
212
213 /// multiply acc
214 class CV_MultiplyAccTest : public CV_AccumBaseTest
215 {
216 public:
217     CV_MultiplyAccTest();
218 protected:
219     void run_func();
220     void prepare_to_validation( int );
221 };
222
223
224 CV_MultiplyAccTest::CV_MultiplyAccTest()
225     : CV_AccumBaseTest( "accum-mulacc", "cvMultiplyAcc" )
226 {
227     test_array[INPUT].push(NULL);
228 }
229
230
231 void CV_MultiplyAccTest::run_func()
232 {
233     cvMultiplyAcc( test_array[INPUT][0], test_array[INPUT][1],
234                    test_array[INPUT_OUTPUT][0], test_array[MASK][0] );
235 }
236
237
238 void CV_MultiplyAccTest::prepare_to_validation( int )
239 {
240     const CvMat* src1 = &test_mat[INPUT][0];
241     const CvMat* src2 = &test_mat[INPUT][1];
242     CvMat* dst = &test_mat[REF_INPUT_OUTPUT][0];
243     CvMat* temp = &test_mat[TEMP][0];
244     const CvMat* mask = test_array[MASK][0] ? &test_mat[MASK][0] : 0;
245
246     cvTsMul( src1, src2, cvScalarAll(1.), temp );
247     cvTsAdd( temp, cvScalarAll(1.), dst, cvScalarAll(1.), cvScalarAll(0.), temp, 0 );
248     cvTsCopy( temp, dst, mask );
249 }
250
251 CV_MultiplyAccTest mulacc_test;
252
253
254 /// running average
255 class CV_RunningAvgTest : public CV_AccumBaseTest
256 {
257 public:
258     CV_RunningAvgTest();
259 protected:
260     void run_func();
261     void prepare_to_validation( int );
262 };
263
264
265 CV_RunningAvgTest::CV_RunningAvgTest()
266     : CV_AccumBaseTest( "accum-runavg", "cvRunningAvg" )
267 {
268 }
269
270
271 void CV_RunningAvgTest::run_func()
272 {
273     cvRunningAvg( test_array[INPUT][0], test_array[INPUT_OUTPUT][0],
274                   alpha, test_array[MASK][0] );
275 }
276
277
278 void CV_RunningAvgTest::prepare_to_validation( int )
279 {
280     const CvMat* src = &test_mat[INPUT][0];
281     CvMat* dst = &test_mat[REF_INPUT_OUTPUT][0];
282     CvMat* temp = &test_mat[TEMP][0];
283     const CvMat* mask = test_array[MASK][0] ? &test_mat[MASK][0] : 0;
284     float a[1], b[1];
285     CvMat A = cvMat(1,1,CV_32F,a), B = cvMat(1,1,CV_32F,b);
286     cvSetReal1D( &A, 0, alpha);
287     cvSetReal1D( &B, 0, 1 - A.data.fl[0] );
288
289     cvTsAdd( src, cvScalarAll(A.data.fl[0]), dst, cvScalarAll(B.data.fl[0]), cvScalarAll(0.), temp, 0 );
290     cvTsCopy( temp, dst, mask );
291 }
292
293 CV_RunningAvgTest runavg_test;
294