Home | History | Annotate | Download | only in test
      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/calib3d/calib3d_c.h"
     44 
     45 using namespace cv;
     46 using namespace std;
     47 
     48 class CV_POSITTest : public cvtest::BaseTest
     49 {
     50 public:
     51     CV_POSITTest();
     52 protected:
     53     void run(int);
     54 };
     55 
     56 
     57 CV_POSITTest::CV_POSITTest()
     58 {
     59     test_case_count = 20;
     60 }
     61 
     62 void CV_POSITTest::run( int start_from )
     63 {
     64     int code = cvtest::TS::OK;
     65 
     66     /* fixed parameters output */
     67     /*float rot[3][3]={  0.49010f,  0.85057f, 0.19063f,
     68                       -0.56948f,  0.14671f, 0.80880f,
     69                        0.65997f, -0.50495f, 0.55629f };
     70 
     71     float trans[3] = { 0.0f, 0.0f, 40.02637f };
     72     */
     73 
     74     /* Some variables */
     75     int i, counter;
     76 
     77     CvTermCriteria criteria;
     78     CvPoint3D32f* obj_points;
     79     CvPoint2D32f* img_points;
     80     CvPOSITObject* object;
     81 
     82     float angleX, angleY, angleZ;
     83     RNG& rng = ts->get_rng();
     84     int progress = 0;
     85 
     86     CvMat* true_rotationX = cvCreateMat( 3, 3, CV_32F );
     87     CvMat* true_rotationY = cvCreateMat( 3, 3, CV_32F );
     88     CvMat* true_rotationZ = cvCreateMat( 3, 3, CV_32F );
     89     CvMat* tmp_matrix = cvCreateMat( 3, 3, CV_32F );
     90     CvMat* true_rotation = cvCreateMat( 3, 3, CV_32F );
     91     CvMat* rotation = cvCreateMat( 3, 3, CV_32F );
     92     CvMat* translation = cvCreateMat( 3, 1, CV_32F );
     93     CvMat* true_translation = cvCreateMat( 3, 1, CV_32F );
     94 
     95     const float flFocalLength = 760.f;
     96     const float flEpsilon = 0.5f;
     97 
     98     /* Initilization */
     99     criteria.type = CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;
    100     criteria.epsilon = flEpsilon;
    101     criteria.max_iter = 10000;
    102 
    103     /* Allocating source arrays; */
    104     obj_points = (CvPoint3D32f*)cvAlloc( 8 * sizeof(CvPoint3D32f) );
    105     img_points = (CvPoint2D32f*)cvAlloc( 8 * sizeof(CvPoint2D32f) );
    106 
    107     /* Fill points arrays with values */
    108 
    109     /* cube model with edge size 10 */
    110     obj_points[0].x = 0;  obj_points[0].y = 0;  obj_points[0].z = 0;
    111     obj_points[1].x = 10; obj_points[1].y = 0;  obj_points[1].z = 0;
    112     obj_points[2].x = 10; obj_points[2].y = 10; obj_points[2].z = 0;
    113     obj_points[3].x = 0;  obj_points[3].y = 10; obj_points[3].z = 0;
    114     obj_points[4].x = 0;  obj_points[4].y = 0;  obj_points[4].z = 10;
    115     obj_points[5].x = 10; obj_points[5].y = 0;  obj_points[5].z = 10;
    116     obj_points[6].x = 10; obj_points[6].y = 10; obj_points[6].z = 10;
    117     obj_points[7].x = 0;  obj_points[7].y = 10; obj_points[7].z = 10;
    118 
    119     /* Loop for test some random object positions */
    120     for( counter = start_from; counter < test_case_count; counter++ )
    121     {
    122         ts->update_context( this, counter, true );
    123         progress = update_progress( progress, counter, test_case_count, 0 );
    124 
    125         /* set all rotation matrix to zero */
    126         cvZero( true_rotationX );
    127         cvZero( true_rotationY );
    128         cvZero( true_rotationZ );
    129 
    130         /* fill random rotation matrix */
    131         angleX = (float)(cvtest::randReal(rng)*2*CV_PI);
    132         angleY = (float)(cvtest::randReal(rng)*2*CV_PI);
    133         angleZ = (float)(cvtest::randReal(rng)*2*CV_PI);
    134 
    135         true_rotationX->data.fl[0 *3+ 0] = 1;
    136         true_rotationX->data.fl[1 *3+ 1] = (float)cos(angleX);
    137         true_rotationX->data.fl[2 *3+ 2] = true_rotationX->data.fl[1 *3+ 1];
    138         true_rotationX->data.fl[1 *3+ 2] = -(float)sin(angleX);
    139         true_rotationX->data.fl[2 *3+ 1] = -true_rotationX->data.fl[1 *3+ 2];
    140 
    141         true_rotationY->data.fl[1 *3+ 1] = 1;
    142         true_rotationY->data.fl[0 *3+ 0] = (float)cos(angleY);
    143         true_rotationY->data.fl[2 *3+ 2] = true_rotationY->data.fl[0 *3+ 0];
    144         true_rotationY->data.fl[0 *3+ 2] = -(float)sin(angleY);
    145         true_rotationY->data.fl[2 *3+ 0] = -true_rotationY->data.fl[0 *3+ 2];
    146 
    147         true_rotationZ->data.fl[2 *3+ 2] = 1;
    148         true_rotationZ->data.fl[0 *3+ 0] = (float)cos(angleZ);
    149         true_rotationZ->data.fl[1 *3+ 1] = true_rotationZ->data.fl[0 *3+ 0];
    150         true_rotationZ->data.fl[0 *3+ 1] = -(float)sin(angleZ);
    151         true_rotationZ->data.fl[1 *3+ 0] = -true_rotationZ->data.fl[0 *3+ 1];
    152 
    153         cvMatMul( true_rotationX, true_rotationY, tmp_matrix);
    154         cvMatMul( tmp_matrix, true_rotationZ, true_rotation);
    155 
    156         /* fill translation vector */
    157         true_translation->data.fl[2] = (float)(cvtest::randReal(rng)*(2*flFocalLength-40) + 60);
    158         true_translation->data.fl[0] = (float)((cvtest::randReal(rng)*2-1)*true_translation->data.fl[2]);
    159         true_translation->data.fl[1] = (float)((cvtest::randReal(rng)*2-1)*true_translation->data.fl[2]);
    160 
    161         /* calculate perspective projection */
    162         for ( i = 0; i < 8; i++ )
    163         {
    164             float vec[3];
    165             CvMat Vec = cvMat( 3, 1, CV_32F, vec );
    166             CvMat Obj_point = cvMat( 3, 1, CV_32F, &obj_points[i].x );
    167 
    168             cvMatMul( true_rotation, &Obj_point, &Vec );
    169 
    170             vec[0] += true_translation->data.fl[0];
    171             vec[1] += true_translation->data.fl[1];
    172             vec[2] += true_translation->data.fl[2];
    173 
    174             img_points[i].x = flFocalLength * vec[0] / vec[2];
    175             img_points[i].y = flFocalLength * vec[1] / vec[2];
    176         }
    177 
    178         /*img_points[0].x = 0 ; img_points[0].y =   0;
    179         img_points[1].x = 80; img_points[1].y = -93;
    180         img_points[2].x = 245;img_points[2].y =  -77;
    181         img_points[3].x = 185;img_points[3].y =  32;
    182         img_points[4].x = 32; img_points[4].y = 135;
    183         img_points[5].x = 99; img_points[5].y = 35;
    184         img_points[6].x = 247; img_points[6].y = 62;
    185         img_points[7].x = 195; img_points[7].y = 179;
    186         */
    187 
    188         object = cvCreatePOSITObject( obj_points, 8 );
    189         cvPOSIT( object, img_points, flFocalLength, criteria,
    190                  rotation->data.fl, translation->data.fl );
    191         cvReleasePOSITObject( &object );
    192 
    193         Mat _rotation = cvarrToMat(rotation), _true_rotation = cvarrToMat(true_rotation);
    194         Mat _translation = cvarrToMat(translation), _true_translation = cvarrToMat(true_translation);
    195         code = cvtest::cmpEps2( ts, _rotation, _true_rotation, flEpsilon, false, "rotation matrix" );
    196         if( code < 0 )
    197             break;
    198 
    199         code = cvtest::cmpEps2( ts, _translation, _true_translation, flEpsilon, false, "translation vector" );
    200         if( code < 0 )
    201             break;
    202     }
    203 
    204     cvFree( &obj_points );
    205     cvFree( &img_points );
    206 
    207     cvReleaseMat( &true_rotationX );
    208     cvReleaseMat( &true_rotationY );
    209     cvReleaseMat( &true_rotationZ );
    210     cvReleaseMat( &tmp_matrix );
    211     cvReleaseMat( &true_rotation );
    212     cvReleaseMat( &rotation );
    213     cvReleaseMat( &translation );
    214     cvReleaseMat( &true_translation );
    215 
    216     if( code < 0 )
    217         ts->set_failed_test_info( code );
    218 }
    219 
    220 TEST(Calib3d_POSIT, accuracy) { CV_POSITTest test; test.safe_run(); }
    221 
    222 /* End of file. */
    223