Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / tests / cv / src / asnakes.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 /*  This is regression test for Snakes functions of OpenCV.
45 //  This test will generate fixed figure, read initial position
46 //  of snake from file, run OpenCV function and compare result
47 //  position of snake with position(from file) which must be resulting.
48 //
49 //  Test is considered to be succesfull if resultant positions
50 //  are identical.
51 */
52
53 class CV_SnakeTest : public CvTest
54 {
55 public:
56     CV_SnakeTest();
57 protected:
58     void run(int);
59 };
60
61 #define SCAN  0
62
63 CV_SnakeTest::CV_SnakeTest():
64     CvTest( "snakes", "cvSnakeImage" )
65 {
66     support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
67 }
68
69 void CV_SnakeTest::run( int /*start_from*/ )
70 {
71     int code = CvTS::OK;
72     static const char* file_name[] =
73     {
74         "ring",
75         "square"
76     };
77
78     const int numfig_image = 1;
79     const int numfig_grad  = 1;
80
81     FILE* file = 0;
82 #ifndef _MAX_PATH
83 #define _MAX_PATH 1024
84 #endif
85     char abs_file_name[_MAX_PATH];
86     char rel_path[_MAX_PATH];
87
88     int i,j;
89
90     /* source image */
91     IplImage* iplSrc = NULL;
92     CvSize win;
93     int length;
94
95     float alpha,beta,gamma;
96     CvTermCriteria criteria;
97     long lErrors = 0;
98     int progress = 0, test_case_count = numfig_image + numfig_grad;
99     CvPoint* Pts = 0;
100     CvPoint* resPts = 0;
101
102     sprintf( rel_path, "%ssnakes/", ts->get_data_path() );
103
104     criteria.type = CV_TERMCRIT_ITER;
105     win.height = win.width = 3;
106
107     for( i = 0; i < test_case_count; i++ )
108     {
109         int num_pos;
110         int k;
111
112         char tmp[_MAX_PATH];
113
114         ts->update_context( this, i, false );
115
116         /* create full name of bitmap file */
117         strcpy(tmp, rel_path);
118         strcat(tmp, file_name[i]);
119         strcpy( abs_file_name, tmp );
120         strcat( abs_file_name, ".bmp" );
121
122         /* read bitmap with 8u image */
123         iplSrc = cvLoadImage( abs_file_name, -1 );
124
125         if (!iplSrc)
126         {
127             ts->printf( CvTS::LOG, "can not load %s\n", abs_file_name );
128             code = CvTS::FAIL_MISSING_TEST_DATA;
129             goto _exit_;
130         }
131
132         /* init snake reading file with snake */
133         strcpy(tmp, rel_path);
134         strcat(tmp, file_name[i]);
135         strcpy( abs_file_name, tmp );
136         strcat( abs_file_name, ".txt" );
137
138 #if !SCAN
139         file = fopen( abs_file_name, "r" );
140 #else
141         file = fopen( abs_file_name, "r+" );
142 #endif
143
144         if (!file)
145         {
146             ts->printf( CvTS::LOG, "can not load %s\n", abs_file_name );
147             code = CvTS::FAIL_MISSING_TEST_DATA;
148             goto _exit_;
149         }
150
151         /* read snake parameters */
152         fscanf(file, "%d", &length );
153         fscanf(file, "%f", &alpha );
154         fscanf(file, "%f", &beta );
155         fscanf(file, "%f", &gamma );
156
157         /* allocate memory for snakes */
158         Pts = (CvPoint*)cvAlloc( length * sizeof(Pts[0]) );
159         resPts = (CvPoint*)cvAlloc( length * sizeof(resPts[0]) );
160
161         /* get number of snake positions */
162         fscanf(file, "%d", &num_pos );
163
164         /* get number iterations between two positions */
165         fscanf(file, "%d", &criteria.max_iter );
166
167         /* read initial snake position */
168         for ( j = 0; j < length; j++ )
169         {
170             fscanf(file, "%d%d", &Pts[j].x, &Pts[j].y );
171         }
172
173         for ( k = 0; k < num_pos; k++ )
174         {
175             /* Run CVL function to check it */
176             if(i<numfig_image)
177             {
178                  cvSnakeImage( iplSrc, Pts, length,
179                            &alpha, &beta, &gamma, CV_VALUE, win, criteria, 0 );
180             }
181             else
182             {
183                 cvSnakeImage( iplSrc, Pts, length,
184                            &alpha, &beta, &gamma, CV_VALUE, win, criteria, 1 /*usegrad*/ );
185             }
186
187 #if !SCAN
188             for ( j = 0; j < length; j++ )
189             {
190                 fscanf(file, "%d%d", &resPts[j].x, &resPts[j].y );
191
192                 lErrors += (Pts[j].x != resPts[j].x);
193                 lErrors += (Pts[j].y != resPts[j].y);
194             }
195 #else
196             fseek( file, 0, SEEK_CUR );
197             fprintf(file, "\n");
198             for ( j = 0; j < length; j++ )
199             {
200                 fprintf(file, "\n%d %d", Pts[j].x, Pts[j].y );
201             }
202 #endif
203         }
204         fclose(file);
205         file = 0;
206         cvFree(&Pts);
207         cvFree(&resPts);
208         cvReleaseImage(&iplSrc);
209
210         progress = update_progress( progress, i, test_case_count, 0 );
211     }
212
213     if( lErrors > 0 )
214     {
215         ts->printf( CvTS::LOG, "Total fixed %d errors", lErrors );
216         code = CvTS::FAIL_BAD_ACCURACY;
217         goto _exit_;
218     }
219
220 _exit_:
221
222     if( file )
223         fclose(file);
224     cvFree(&Pts);
225     cvFree(&resPts);
226     cvReleaseImage(&iplSrc);
227
228     if( code < 0 )
229         ts->set_failed_test_info( code );
230 }
231
232 CV_SnakeTest snake_test;
233
234 /* End of file. */