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 void show_points( IplImage* gray, CvPoint2D32f* u, int u_cnt, CvPoint2D32f* v, int v_cnt,
45 CvSize etalon_size, int was_found )
50 cvGetImageRawData( gray, 0, 0, &size );
52 IplImage* rgb = cvCreateImage( size, 8, 3 );
53 cvMerge( gray, gray, gray, 0, rgb );
57 for( i = 0; i < v_cnt; i++ )
59 cvCircle( rgb, cvPoint(cvRound(v[i].x), cvRound(v[i].y)), 3, CV_RGB(255,0,0), CV_FILLED);
65 for( i = 0; i < u_cnt; i++ )
67 cvCircle( rgb, cvPoint(cvRound(u[i].x), cvRound(u[i].y)), 3, CV_RGB(0,255,0), CV_FILLED);
71 cvDrawChessboardCorners( rgb, etalon_size, v, v_cnt, was_found );
73 cvvNamedWindow( "test", 0 );
74 cvvShowImage( "test", rgb );
80 class CV_ChessboardDetectorTest : public CvTest
83 CV_ChessboardDetectorTest();
89 CV_ChessboardDetectorTest::CV_ChessboardDetectorTest():
90 CvTest( "chessboard-detector", "cvFindChessboardCorners" )
92 support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
95 /* ///////////////////// chess_corner_test ///////////////////////// */
96 void CV_ChessboardDetectorTest::run( int start_from )
101 const double rough_success_error_level = 2.5;
102 const double precise_success_error_level = 0.2;
103 double err = 0, max_rough_error = 0, max_precise_error = 0;
106 /* test parameters */
117 IplImage* thresh = 0;
122 sprintf( filepath, "%scameracalibration/", ts->get_data_path() );
123 sprintf( filename, "%schessboard_list.dat", filepath );
124 CvFileStorage* fs = cvOpenFileStorage( filename, 0, CV_STORAGE_READ );
125 CvFileNode* board_list = fs ? cvGetFileNodeByName( fs, 0, "boards" ) : 0;
127 if( !fs || !board_list || !CV_NODE_IS_SEQ(board_list->tag) ||
128 board_list->data.seq->total % 2 != 0 )
130 ts->printf( CvTS::LOG, "chessboard_list.dat can not be readed or is not valid" );
131 code = CvTS::FAIL_MISSING_TEST_DATA;
135 max_idx = board_list->data.seq->total/2;
137 for( idx = start_from; idx < max_idx; idx++ )
139 int etalon_count = -1;
141 CvSize etalon_size = { -1, -1 };
144 ts->update_context( this, idx-1, true );
147 sprintf( filename, "%s%s", filepath,
148 cvReadString((CvFileNode*)cvGetSeqElem(board_list->data.seq,idx*2),"dummy.txt"));
150 img = cvLoadImage( filename );
154 ts->printf( CvTS::LOG, "one of chessboard images can't be read: %s", filename );
157 code = CvTS::FAIL_MISSING_TEST_DATA;
163 gray = cvCreateImage( cvSize( img->width, img->height ), IPL_DEPTH_8U, 1 );
164 thresh = cvCreateImage( cvSize( img->width, img->height ), IPL_DEPTH_8U, 1 );
165 cvCvtColor( img, gray, CV_BGR2GRAY );
167 sprintf( filename, "%s%s", filepath,
168 cvReadString((CvFileNode*)cvGetSeqElem(board_list->data.seq,idx*2+1),"dummy.txt"));
170 _u = (CvMat*)cvLoad( filename );
175 ts->printf( CvTS::LOG, "one of chessboard corner files can't be read: %s", filename );
178 code = CvTS::FAIL_MISSING_TEST_DATA;
184 etalon_size.width = _u->cols;
185 etalon_size.height = _u->rows;
186 etalon_count = etalon_size.width*etalon_size.height;
188 /* allocate additional buffers */
189 _v = cvCloneMat( _u );
190 count = etalon_count;
192 u = (CvPoint2D32f*)_u->data.fl;
193 v = (CvPoint2D32f*)_v->data.fl;
195 OPENCV_CALL( result = cvFindChessBoardCornerGuesses(
196 gray, thresh, 0, etalon_size, v, &count ));
198 //show_points( gray, 0, etalon_count, v, count, etalon_size, result );
199 if( !result || count != etalon_count )
201 ts->printf( CvTS::LOG, "chess board is not found" );
202 code = CvTS::FAIL_INVALID_OUTPUT;
208 for( j = 0; j < etalon_count; j++ )
210 double dx = fabs( v[j].x - u[j].x );
211 double dy = fabs( v[j].y - u[j].y );
217 if( err > rough_success_error_level )
219 ts->printf( CvTS::LOG, "bad accuracy of corner guesses" );
220 code = CvTS::FAIL_BAD_ACCURACY;
225 max_rough_error = MAX( max_rough_error, err );
227 OPENCV_CALL( cvFindCornerSubPix( gray, v, count, cvSize( 5, 5 ), cvSize(-1,-1),
228 cvTermCriteria(CV_TERMCRIT_EPS|CV_TERMCRIT_ITER,30,0.1)));
229 //show_points( gray, u + 1, etalon_count, v, count );
233 for( j = 0; j < etalon_count; j++ )
235 double dx = fabs( v[j].x - u[j].x );
236 double dy = fabs( v[j].y - u[j].y );
242 if( err > precise_success_error_level )
244 ts->printf( CvTS::LOG, "bad accuracy of adjusted corners" );
245 code = CvTS::FAIL_BAD_ACCURACY;
250 max_precise_error = MAX( max_precise_error, err );
252 cvSave( filename, _v );
256 cvReleaseImage( &img );
257 cvReleaseImage( &gray );
258 cvReleaseImage( &thresh );
259 progress = update_progress( progress, idx-1, max_idx, 0 );
264 /* release occupied memory */
267 cvReleaseFileStorage( &fs );
268 cvReleaseImage( &img );
269 cvReleaseImage( &gray );
270 cvReleaseImage( &thresh );
273 ts->set_failed_test_info( code );
276 CV_ChessboardDetectorTest chessboard_detector_test;