1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
44 #if 0 /* avoid this while a substitution for IPL DCT is not ready */
51 static char* funcs[] =
56 static char *test_desc[] =
58 "Comparing against IPL DCT"
61 /* actual parameters */
62 static int min_img_size, max_img_size;
63 static int max_dct_size;
64 static int base_iters;
66 static int init_hmm_obs_params = 0;
68 static const int img8u_range = 256;
70 static void read_hmm_obs_params( void )
72 if( !init_hmm_obs_params )
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" );
80 init_hmm_obs_params = 1;
85 static CvSize hmm_obs_dct_get_size( IplImage* img, CvSize dctSize, CvSize delta )
88 CvRect roi = cvGetImageROI( img );
90 result.width = (roi.width - dctSize.width + delta.width) / delta.width;
91 result.height = (roi.height - dctSize.height + delta.height) / delta.height;
97 static void hmm_obs_dct_etalon( IplImage* img, char* obs, CvSize dctSize,
98 CvSize obsSize, CvSize delta )
100 IplImage *src = cvCreateImage( dctSize, IPL_DEPTH_8U, 1 );
101 IplImage *dst = cvCreateImage( dctSize, IPL_DEPTH_32F, 1 );
103 CvSize result = hmm_obs_dct_get_size( img, dctSize, delta );
106 int obs_step = obsSize.width*sizeof(float);
108 result.width *= delta.width;
109 result.height *= delta.height;
111 for( y = 0; y < result.height; y += delta.height )
112 for( x = 0; x < result.width; x += delta.width )
114 cvSetImageROI( img, cvRect( x, y, dctSize.width, dctSize.height ));
116 iplDCT2D( src, dst, IPL_DCT_Forward );
117 for( j = 0; j < obsSize.height; j++ )
119 memcpy( obs, dst->imageData + dst->widthStep*j, obs_step );
124 cvReleaseImage( &src );
125 cvReleaseImage( &dst );
130 /* ///////////////////// moments_test ///////////////////////// */
131 static int hmm_dct_test( void )
133 const double success_error_level = 1.1;
135 int seed = atsGetSeed();
137 const int max_obs_size = 8;
139 /* position where the maximum error occured */
140 int i, merr_iter = 0;
142 /* test parameters */
148 AtsRandState rng_state;
151 atsRandInit( &rng_state, 0, img8u_range, seed );
153 read_hmm_obs_params();
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 );
161 for( i = 0; i < base_iters; i++ )
164 CvSize dctSize, obsSize, delta, result;
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;
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;
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;
186 delta.width = atsRandPlain32s( &rng_state ) % dctSize.width + 1;
187 delta.height = atsRandPlain32s( &rng_state ) % dctSize.height + 1;
189 cvSetImageROI( img, cvRect( 0, 0, size.width, size.height ));
191 result = hmm_obs_dct_get_size( img, dctSize, delta );
193 atsFillRandomImageEx( img, &rng_state );
195 OPENCV_CALL( cvImgToObs_DCT( img, (float*)(obs->imageData), dctSize, obsSize, delta ));
197 hmm_obs_dct_etalon( img, obs2->imageData, dctSize, obsSize, delta );
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);
203 assert( obs->roi == 0 && obs2->roi == 0 );
205 err = cvNorm( obs, obs2, CV_C );
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);
215 if( max_err > success_error_level )
222 cvReleaseImage( &img );
223 cvReleaseImage( &obs );
224 cvReleaseImage( &obs2 );
228 trsWrite( ATS_LST, "Max err is %g at iter = %d, seed = %08x",
229 max_err, merr_iter, seed );
231 return max_err <= success_error_level ?
232 trsResult( TRS_OK, "No errors" ) :
233 trsResult( TRS_FAIL, "Bad accuracy" );
237 trsWrite( ATS_LST, "Fatal error at iter = %d, seed = %08x", i, seed );
238 return trsResult( TRS_FAIL, "Function returns error code" );
243 void InitAImageToHMMObs( void )
245 /* Register test functions */
247 trsReg( funcs[0], test_desc[0], atsAlgoClass, hmm_dct_test );