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 "test_precomp.hpp"
43#include "opencv2/imgproc/imgproc_c.h"
44#include "opencv2/calib3d/calib3d_c.h"
45
46class CV_ChessboardDetectorTimingTest : public cvtest::BaseTest
47{
48public:
49    CV_ChessboardDetectorTimingTest();
50protected:
51    void run(int);
52};
53
54
55CV_ChessboardDetectorTimingTest::CV_ChessboardDetectorTimingTest()
56{
57}
58
59/* ///////////////////// chess_corner_test ///////////////////////// */
60void CV_ChessboardDetectorTimingTest::run( int start_from )
61{
62    int code = cvtest::TS::OK;
63
64    /* test parameters */
65    std::string   filepath;
66    std::string   filename;
67
68    CvMat*  _v = 0;
69    CvPoint2D32f* v;
70
71    IplImage img;
72    IplImage* gray = 0;
73    IplImage* thresh = 0;
74
75    int  idx, max_idx;
76    int  progress = 0;
77
78    filepath = cv::format("%scv/cameracalibration/", ts->get_data_path().c_str() );
79    filename = cv::format("%schessboard_timing_list.dat", filepath.c_str() );
80    CvFileStorage* fs = cvOpenFileStorage( filename.c_str(), 0, CV_STORAGE_READ );
81    CvFileNode* board_list = fs ? cvGetFileNodeByName( fs, 0, "boards" ) : 0;
82
83    if( !fs || !board_list || !CV_NODE_IS_SEQ(board_list->tag) ||
84        board_list->data.seq->total % 4 != 0 )
85    {
86        ts->printf( cvtest::TS::LOG, "chessboard_timing_list.dat can not be readed or is not valid" );
87        code = cvtest::TS::FAIL_MISSING_TEST_DATA;
88        goto _exit_;
89    }
90
91    max_idx = board_list->data.seq->total/4;
92
93    for( idx = start_from; idx < max_idx; idx++ )
94    {
95        int count0 = -1;
96        int count = 0;
97        CvSize pattern_size;
98        int result, result1 = 0;
99
100        const char* imgname = cvReadString((CvFileNode*)cvGetSeqElem(board_list->data.seq,idx*4), "dummy.txt");
101        int is_chessboard = cvReadInt((CvFileNode*)cvGetSeqElem(board_list->data.seq,idx*4+1), 0);
102        pattern_size.width = cvReadInt((CvFileNode*)cvGetSeqElem(board_list->data.seq,idx*4 + 2), -1);
103        pattern_size.height = cvReadInt((CvFileNode*)cvGetSeqElem(board_list->data.seq,idx*4 + 3), -1);
104
105        ts->update_context( this, idx-1, true );
106
107        /* read the image */
108        filename = cv::format("%s%s", filepath.c_str(), imgname );
109
110        cv::Mat img2 = cv::imread( filename );
111        img = img2;
112
113        if( img2.empty() )
114        {
115            ts->printf( cvtest::TS::LOG, "one of chessboard images can't be read: %s\n", filename.c_str() );
116            if( max_idx == 1 )
117            {
118                code = cvtest::TS::FAIL_MISSING_TEST_DATA;
119                goto _exit_;
120            }
121            continue;
122        }
123
124        ts->printf(cvtest::TS::LOG, "%s: chessboard %d:\n", imgname, is_chessboard);
125
126        gray = cvCreateImage( cvSize( img.width, img.height ), IPL_DEPTH_8U, 1 );
127        thresh = cvCreateImage( cvSize( img.width, img.height ), IPL_DEPTH_8U, 1 );
128        cvCvtColor( &img, gray, CV_BGR2GRAY );
129
130
131        count0 = pattern_size.width*pattern_size.height;
132
133        /* allocate additional buffers */
134        _v = cvCreateMat(1, count0, CV_32FC2);
135        count = count0;
136
137        v = (CvPoint2D32f*)_v->data.fl;
138
139        int64 _time0 = cvGetTickCount();
140        result = cvCheckChessboard(gray, pattern_size);
141        int64 _time01 = cvGetTickCount();
142
143        OPENCV_CALL( result1 = cvFindChessboardCorners(
144                 gray, pattern_size, v, &count, 15 ));
145        int64 _time1 = cvGetTickCount();
146
147        if( result != is_chessboard )
148        {
149            ts->printf( cvtest::TS::LOG, "Error: chessboard was %sdetected in the image %s\n",
150                       result ? "" : "not ", imgname );
151            code = cvtest::TS::FAIL_INVALID_OUTPUT;
152            goto _exit_;
153        }
154        if(result != result1)
155        {
156            ts->printf( cvtest::TS::LOG, "Warning: results differ cvCheckChessboard %d, cvFindChessboardCorners %d\n",
157                       result, result1);
158        }
159
160        int num_pixels = gray->width*gray->height;
161        float check_chessboard_time = float(_time01 - _time0)/(float)cvGetTickFrequency(); // in us
162        ts->printf(cvtest::TS::LOG, "    cvCheckChessboard time s: %f, us per pixel: %f\n",
163                   check_chessboard_time*1e-6, check_chessboard_time/num_pixels);
164
165        float find_chessboard_time = float(_time1 - _time01)/(float)cvGetTickFrequency();
166        ts->printf(cvtest::TS::LOG, "    cvFindChessboard time s: %f, us per pixel: %f\n",
167                   find_chessboard_time*1e-6, find_chessboard_time/num_pixels);
168
169        cvReleaseMat( &_v );
170        cvReleaseImage( &gray );
171        cvReleaseImage( &thresh );
172        progress = update_progress( progress, idx-1, max_idx, 0 );
173    }
174
175_exit_:
176
177    /* release occupied memory */
178    cvReleaseMat( &_v );
179    cvReleaseFileStorage( &fs );
180    cvReleaseImage( &gray );
181    cvReleaseImage( &thresh );
182
183    if( code < 0 )
184        ts->set_failed_test_info( code );
185}
186
187TEST(Calib3d_ChessboardDetector, timing) { CV_ChessboardDetectorTimingTest test; test.safe_run(); }
188
189/* End of file. */
190