Path: blob/master/modules/calib3d/test/test_posit.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of Intel Corporation may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041#include "test_precomp.hpp"42#include "opencv2/calib3d/calib3d_c.h"4344namespace opencv_test { namespace {4546class CV_POSITTest : public cvtest::BaseTest47{48public:49CV_POSITTest();50protected:51void run(int);52};535455CV_POSITTest::CV_POSITTest()56{57test_case_count = 20;58}5960void CV_POSITTest::run( int start_from )61{62int code = cvtest::TS::OK;6364/* fixed parameters output */65/*float rot[3][3]={ 0.49010f, 0.85057f, 0.19063f,66-0.56948f, 0.14671f, 0.80880f,670.65997f, -0.50495f, 0.55629f };6869float trans[3] = { 0.0f, 0.0f, 40.02637f };70*/7172/* Some variables */73int i, counter;7475CvTermCriteria criteria;76CvPoint3D32f* obj_points;77CvPoint2D32f* img_points;78CvPOSITObject* object;7980float angleX, angleY, angleZ;81RNG& rng = ts->get_rng();82int progress = 0;8384CvMat* true_rotationX = cvCreateMat( 3, 3, CV_32F );85CvMat* true_rotationY = cvCreateMat( 3, 3, CV_32F );86CvMat* true_rotationZ = cvCreateMat( 3, 3, CV_32F );87CvMat* tmp_matrix = cvCreateMat( 3, 3, CV_32F );88CvMat* true_rotation = cvCreateMat( 3, 3, CV_32F );89CvMat* rotation = cvCreateMat( 3, 3, CV_32F );90CvMat* translation = cvCreateMat( 3, 1, CV_32F );91CvMat* true_translation = cvCreateMat( 3, 1, CV_32F );9293const float flFocalLength = 760.f;94const float flEpsilon = 0.5f;9596/* Initialization */97criteria.type = CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;98criteria.epsilon = flEpsilon;99criteria.max_iter = 10000;100101/* Allocating source arrays; */102obj_points = (CvPoint3D32f*)cvAlloc( 8 * sizeof(CvPoint3D32f) );103img_points = (CvPoint2D32f*)cvAlloc( 8 * sizeof(CvPoint2D32f) );104105/* Fill points arrays with values */106107/* cube model with edge size 10 */108obj_points[0].x = 0; obj_points[0].y = 0; obj_points[0].z = 0;109obj_points[1].x = 10; obj_points[1].y = 0; obj_points[1].z = 0;110obj_points[2].x = 10; obj_points[2].y = 10; obj_points[2].z = 0;111obj_points[3].x = 0; obj_points[3].y = 10; obj_points[3].z = 0;112obj_points[4].x = 0; obj_points[4].y = 0; obj_points[4].z = 10;113obj_points[5].x = 10; obj_points[5].y = 0; obj_points[5].z = 10;114obj_points[6].x = 10; obj_points[6].y = 10; obj_points[6].z = 10;115obj_points[7].x = 0; obj_points[7].y = 10; obj_points[7].z = 10;116117/* Loop for test some random object positions */118for( counter = start_from; counter < test_case_count; counter++ )119{120ts->update_context( this, counter, true );121progress = update_progress( progress, counter, test_case_count, 0 );122123/* set all rotation matrix to zero */124cvZero( true_rotationX );125cvZero( true_rotationY );126cvZero( true_rotationZ );127128/* fill random rotation matrix */129angleX = (float)(cvtest::randReal(rng)*2*CV_PI);130angleY = (float)(cvtest::randReal(rng)*2*CV_PI);131angleZ = (float)(cvtest::randReal(rng)*2*CV_PI);132133true_rotationX->data.fl[0 *3+ 0] = 1;134true_rotationX->data.fl[1 *3+ 1] = (float)cos(angleX);135true_rotationX->data.fl[2 *3+ 2] = true_rotationX->data.fl[1 *3+ 1];136true_rotationX->data.fl[1 *3+ 2] = -(float)sin(angleX);137true_rotationX->data.fl[2 *3+ 1] = -true_rotationX->data.fl[1 *3+ 2];138139true_rotationY->data.fl[1 *3+ 1] = 1;140true_rotationY->data.fl[0 *3+ 0] = (float)cos(angleY);141true_rotationY->data.fl[2 *3+ 2] = true_rotationY->data.fl[0 *3+ 0];142true_rotationY->data.fl[0 *3+ 2] = -(float)sin(angleY);143true_rotationY->data.fl[2 *3+ 0] = -true_rotationY->data.fl[0 *3+ 2];144145true_rotationZ->data.fl[2 *3+ 2] = 1;146true_rotationZ->data.fl[0 *3+ 0] = (float)cos(angleZ);147true_rotationZ->data.fl[1 *3+ 1] = true_rotationZ->data.fl[0 *3+ 0];148true_rotationZ->data.fl[0 *3+ 1] = -(float)sin(angleZ);149true_rotationZ->data.fl[1 *3+ 0] = -true_rotationZ->data.fl[0 *3+ 1];150151cvMatMul( true_rotationX, true_rotationY, tmp_matrix);152cvMatMul( tmp_matrix, true_rotationZ, true_rotation);153154/* fill translation vector */155true_translation->data.fl[2] = (float)(cvtest::randReal(rng)*(2*flFocalLength-40) + 60);156true_translation->data.fl[0] = (float)((cvtest::randReal(rng)*2-1)*true_translation->data.fl[2]);157true_translation->data.fl[1] = (float)((cvtest::randReal(rng)*2-1)*true_translation->data.fl[2]);158159/* calculate perspective projection */160for ( i = 0; i < 8; i++ )161{162float vec[3];163CvMat Vec = cvMat( 3, 1, CV_32F, vec );164CvMat Obj_point = cvMat( 3, 1, CV_32F, &obj_points[i].x );165166cvMatMul( true_rotation, &Obj_point, &Vec );167168vec[0] += true_translation->data.fl[0];169vec[1] += true_translation->data.fl[1];170vec[2] += true_translation->data.fl[2];171172img_points[i].x = flFocalLength * vec[0] / vec[2];173img_points[i].y = flFocalLength * vec[1] / vec[2];174}175176/*img_points[0].x = 0 ; img_points[0].y = 0;177img_points[1].x = 80; img_points[1].y = -93;178img_points[2].x = 245;img_points[2].y = -77;179img_points[3].x = 185;img_points[3].y = 32;180img_points[4].x = 32; img_points[4].y = 135;181img_points[5].x = 99; img_points[5].y = 35;182img_points[6].x = 247; img_points[6].y = 62;183img_points[7].x = 195; img_points[7].y = 179;184*/185186object = cvCreatePOSITObject( obj_points, 8 );187cvPOSIT( object, img_points, flFocalLength, criteria,188rotation->data.fl, translation->data.fl );189cvReleasePOSITObject( &object );190191Mat _rotation = cvarrToMat(rotation), _true_rotation = cvarrToMat(true_rotation);192Mat _translation = cvarrToMat(translation), _true_translation = cvarrToMat(true_translation);193code = cvtest::cmpEps2( ts, _rotation, _true_rotation, flEpsilon, false, "rotation matrix" );194if( code < 0 )195break;196197code = cvtest::cmpEps2( ts, _translation, _true_translation, flEpsilon, false, "translation vector" );198if( code < 0 )199break;200}201202cvFree( &obj_points );203cvFree( &img_points );204205cvReleaseMat( &true_rotationX );206cvReleaseMat( &true_rotationY );207cvReleaseMat( &true_rotationZ );208cvReleaseMat( &tmp_matrix );209cvReleaseMat( &true_rotation );210cvReleaseMat( &rotation );211cvReleaseMat( &translation );212cvReleaseMat( &true_translation );213214if( code < 0 )215ts->set_failed_test_info( code );216}217218TEST(Calib3d_POSIT, accuracy) { CV_POSITTest test; test.safe_run(); }219220}} // namespace221/* End of file. */222223224