Move the sources to trunk
[opencv] / tests / cv / src / ahmmobs.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 #if 0 /* avoid this while a substitution for IPL DCT is not ready */
45
46 #include <stdlib.h>
47 #include <assert.h>
48 #include <limits.h>
49 #include <float.h>
50
51 static char* funcs[] =
52 {
53     "cvImgToObs_DCT"
54 };
55
56 static char *test_desc[] =
57 {
58     "Comparing against IPL DCT"
59 };
60
61 /* actual parameters */
62 static int min_img_size, max_img_size;
63 static int max_dct_size;
64 static int base_iters;
65
66 static int init_hmm_obs_params = 0;
67
68 static const int img8u_range = 256;
69
70 static void read_hmm_obs_params( void )
71 {
72     if( !init_hmm_obs_params )
73     {
74         /* read tests params */
75         trsiRead( &min_img_size, "10", "Minimal width or height of image" );
76         trsiRead( &max_img_size, "300", "Maximal width or height of image" );
77         trsiRead( &max_dct_size, "24", "Maximum DCT size" );
78         trsiRead( &base_iters, "100", "Base number of iterations" );
79
80         init_hmm_obs_params = 1;
81     }
82 }
83
84
85 static  CvSize  hmm_obs_dct_get_size( IplImage* img, CvSize dctSize, CvSize delta )
86 {
87     CvSize result;
88     CvRect roi = cvGetImageROI( img );
89
90     result.width = (roi.width - dctSize.width + delta.width) / delta.width;
91     result.height = (roi.height - dctSize.height + delta.height) / delta.height;
92
93     return result;
94 }
95
96
97 static  void hmm_obs_dct_etalon( IplImage* img, char* obs, CvSize dctSize,
98                                  CvSize obsSize, CvSize delta )
99 {
100     IplImage  *src = cvCreateImage( dctSize, IPL_DEPTH_8U, 1 );
101     IplImage  *dst = cvCreateImage( dctSize, IPL_DEPTH_32F, 1 );
102
103     CvSize result = hmm_obs_dct_get_size( img, dctSize, delta );
104     
105     int  x, y, j;
106     int  obs_step = obsSize.width*sizeof(float);
107     
108     result.width *= delta.width;
109     result.height *= delta.height;
110
111     for( y = 0; y < result.height; y += delta.height )
112         for( x = 0; x < result.width; x += delta.width )
113         {
114             cvSetImageROI( img, cvRect( x, y, dctSize.width, dctSize.height ));
115             cvCopy( img, src );
116             iplDCT2D( src, dst, IPL_DCT_Forward );
117             for( j = 0; j < obsSize.height; j++ )
118             {
119                 memcpy( obs, dst->imageData + dst->widthStep*j, obs_step );
120                 obs += obs_step;
121             }
122         }
123
124     cvReleaseImage( &src );
125     cvReleaseImage( &dst );
126 }
127
128
129
130 /* ///////////////////// moments_test ///////////////////////// */
131 static int hmm_dct_test( void )
132 {
133     const double success_error_level = 1.1;
134
135     int   seed = atsGetSeed();
136     int   code = TRS_OK;
137     const int max_obs_size = 8;
138
139     /* position where the maximum error occured */
140     int   i, merr_iter = 0;
141
142     /* test parameters */
143     double  max_err = 0.;
144
145     IplImage  *img = 0;
146     IplImage  *obs = 0;
147     IplImage  *obs2 = 0;
148     AtsRandState rng_state;
149     CvSize  obs_size;
150
151     atsRandInit( &rng_state, 0, img8u_range, seed );
152
153     read_hmm_obs_params();
154
155     img = cvCreateImage( cvSize( max_img_size, max_img_size ), IPL_DEPTH_8U, 1 );
156     obs_size.height = max_img_size; 
157     obs_size.width = obs_size.height*64;
158     obs = cvCreateImage( obs_size, IPL_DEPTH_32F, 1 );
159     obs2 = cvCreateImage( obs_size, IPL_DEPTH_32F, 1 );
160
161     for( i = 0; i < base_iters; i++ )
162     {
163         CvSize size;
164         CvSize dctSize, obsSize, delta, result;
165         double err = 0;
166
167         size.width = atsRandPlain32s( &rng_state ) %
168                      (max_img_size - min_img_size + 1) + min_img_size;
169         size.height = atsRandPlain32s( &rng_state ) %
170                      (max_img_size - min_img_size + 1) + min_img_size;
171
172         dctSize.width = atsRandPlain32s( &rng_state ) % (max_dct_size - 1) + 2;
173         if( dctSize.width > size.width )
174             dctSize.width = size.width;
175         dctSize.height = atsRandPlain32s( &rng_state ) % (max_dct_size - 1) + 2;
176         if( dctSize.height > size.height )
177             dctSize.height = size.height;
178
179         obsSize.width = atsRandPlain32s( &rng_state ) % max_obs_size + 1;
180         if( obsSize.width > dctSize.width )
181             obsSize.width = dctSize.width;
182         obsSize.height = atsRandPlain32s( &rng_state ) % max_obs_size + 1;
183         if( obsSize.height > dctSize.height )
184             obsSize.height = dctSize.height;
185
186         delta.width = atsRandPlain32s( &rng_state ) % dctSize.width + 1;
187         delta.height = atsRandPlain32s( &rng_state ) % dctSize.height + 1;
188
189         cvSetImageROI( img, cvRect( 0, 0, size.width, size.height ));
190
191         result = hmm_obs_dct_get_size( img, dctSize, delta );
192
193         atsFillRandomImageEx( img, &rng_state );
194
195         OPENCV_CALL( cvImgToObs_DCT( img, (float*)(obs->imageData), dctSize, obsSize, delta ));
196
197         hmm_obs_dct_etalon( img, obs2->imageData, dctSize, obsSize, delta );
198
199         obs->width = obs2->width = result.width*obsSize.width*obsSize.height;
200         obs->height = obs2->height = result.height;
201         obs->widthStep = obs2->widthStep = obs->width*sizeof(float);
202
203         assert( obs->roi == 0 && obs2->roi == 0 );
204
205         err = cvNorm( obs, obs2, CV_C );
206
207         obs->width = obs2->width = max_img_size;
208         obs->height = obs2->height = max_img_size;
209         obs->widthStep = obs2->widthStep = obs->width*sizeof(float);
210
211         if( err > max_err )
212         {
213             merr_iter = i;
214             max_err = err;
215             if( max_err > success_error_level )
216                 goto test_exit;
217         }
218     }
219
220 test_exit:
221
222     cvReleaseImage( &img );
223     cvReleaseImage( &obs );
224     cvReleaseImage( &obs2 );
225
226     if( code == TRS_OK )
227     {
228         trsWrite( ATS_LST, "Max err is %g at iter = %d, seed = %08x",
229                            max_err, merr_iter, seed );
230
231         return max_err <= success_error_level ?
232             trsResult( TRS_OK, "No errors" ) :
233             trsResult( TRS_FAIL, "Bad accuracy" );
234     }
235     /*else
236     {
237         trsWrite( ATS_LST, "Fatal error at iter = %d, seed = %08x", i, seed );
238         return trsResult( TRS_FAIL, "Function returns error code" );
239     }*/
240 }
241
242
243 void InitAImageToHMMObs( void )
244 {
245     /* Register test functions */
246
247     trsReg( funcs[0], test_desc[0], atsAlgoClass, hmm_dct_test );
248
249 } /* InitAMoments */
250
251 #endif
252
253 /* End of file. */